Spring mvc,如何绑定具有集合作为其属性的域对象

发布于 2024-09-14 13:57:09 字数 782 浏览 1 评论 0原文

我有一个名为 Order 的域对象,它有一个名为 serviceOrders 的集合属性,其中保存了服务集合 --- 订单 m:m 关联关系。

public class Order implements Serializable {

 private Long id = null;
 private BigDecimal amountPaid;
 private BigDecimal accountReceivable; 
 private User user;
 private Set serviceOrders = new HashSet();
 private Date closed;
 private Date created = new Date();
 private String status;

还有一种用于添加名为 addServiceOrder 的关联的方法,

public void addServiceOrder(ServiceOrder serviceOrder) {
  if (serviceOrder == null)
   throw new IllegalArgumentException("Can't add a null serviceOrder.");
  this.getServiceOrders().add(serviceOrder);
 }

我应该如何使用 commandName 用“path”设置此集合,我认为它只会调用命令对象的 get set 方法。我应该如何将 serviceOrder 添加到此命令对象。我不知道这个问题。任何帮助将不胜感激

I have a domain object called Order, and it has a collection attribute called serviceOrders where a collection of service --- order m:m association relationships are hold.

public class Order implements Serializable {

 private Long id = null;
 private BigDecimal amountPaid;
 private BigDecimal accountReceivable; 
 private User user;
 private Set serviceOrders = new HashSet();
 private Date closed;
 private Date created = new Date();
 private String status;

also there is a method for adding the association called addServiceOrder

public void addServiceOrder(ServiceOrder serviceOrder) {
  if (serviceOrder == null)
   throw new IllegalArgumentException("Can't add a null serviceOrder.");
  this.getServiceOrders().add(serviceOrder);
 }

how should I use commandName to set this collection with "path", I think it would only call its get set method of the Command Object. how should I add serviceOrder to this command Object. I have no idea about this problem. any help would be highly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-09-21 13:57:09

假设您的 ServiceOrder 实例具有唯一的 id,您的服务方法应该是 #add(Long id)。

Assuming your ServiceOrder instances have unique ids your service method should be #add(Long id).

三五鸿雁 2024-09-21 13:57:09

好吧,请耐心听我说,但解决方案很简单,同时也很烦人。几个月前我遇到了这个问题。我将向您展示我的解决方案,在我看来,使用 jstl 库来处理集合。

<c:forEach items="${Questions}" var="quest" varStatus="itemsIndex">
        <fieldset>
          <legend>${quest.section}</legend>
          <form:form id="group${itemsIndex.index}" modelAttribute="ChoiceList" action="" method="POST" onsubmit="javascript:ajaxSave($(this).serialize()); return false;">
            <a id="Group${quest.id}"></a>
            <c:forEach items="${quest.qisQuestionsCollection}" var="quest2" varStatus="itemsRow">
              <div style="font-weight: bold; margin: 10px 0px">${quest2.shortText}</div>
              ( ${quest2.qisQuestionTypes.description} )<br/>
          ( ${quest2.helpText} )<br/>
              <a id="Question${quest2.id}"></a>
              <c:choose>
                <c:when test="${quest2.qisQuestionTypes.questionType == 'CHOOSEANY'}">
                  <c:forEach items="${quest2.qisChoicesCollection}" var="quest3" varStatus="indexStatus">
                    <c:forEach items="${ChoiceFields}" var="CField">
                      <c:set scope="request" value="${quest3}" var="ChoiceData"/>
                      <c:set scope="request" value="${CField}" var="ChoiceProperty"/>
                      <%
                                answerMap = (HashMap<QisChoice, Answer>) request.getAttribute("AnswerList");
                                choice = (QisChoice) request.getAttribute("ChoiceData");
                                if (answerMap.containsKey(choice.getChoiceID())) {
                                  Answer theAnswer = (Answer) answerMap.get(choice.getChoiceID());
                                  if (theAnswer != null) {
                                    if (theAnswer.getChoiceValue() != null) {
                                      request.setAttribute("itemValue", theAnswer.getChoiceValue());
                                      request.setAttribute("itemSelected", true);
                                    } else {
                                      request.setAttribute("itemSelected", false);
                                      request.setAttribute("itemValue", getReflectedValue(
                                              (QisChoice) request.getAttribute("ChoiceData"),
                                              (AccessorStruct) request.getAttribute("ChoiceProperty")));
                                    }
                                  }
                                } else {
                                  request.setAttribute("itemSelected", false);
                                  request.setAttribute("itemValue", getReflectedValue(
                                          (QisChoice) request.getAttribute("ChoiceData"),
                                          (AccessorStruct) request.getAttribute("ChoiceProperty")));
                                }
                                request.setAttribute("itemValue2", getReflectedValue(
                                        (QisChoice) request.getAttribute("ChoiceData"),
                                        (AccessorStruct) request.getAttribute("ChoiceProperty")));
                      %>
                      <c:choose>
                        <c:when test="${CField.visible == 'HIDDEN'}">
                          <form:hidden value="${itemValue2}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" />
                        </c:when>
                        <c:otherwise>
                          <c:choose>
                            <c:when test="${itemSelected}">
                              <form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" checked="true" /><br/>
                            </c:when>
                            <c:otherwise>
                              <form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" /><br/>
                            </c:otherwise>
                          </c:choose>

                        </c:otherwise>
                      </c:choose>
                    </c:forEach>
                  </c:forEach>
                </c:when>

            <input type="submit" value="Save Section"
                   class="button-main" />
          </fieldset>
        </form:form>
      </c:forEach>`

关键位在这一行中

<form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" checked="true" /><br/>

要链接命令对象及其集合以进行回发,您必须将元素的索引显示为弹簧路径的一部分。就我而言,我有两个级别的集合来跟踪

<c:forEach items="${quest.qisQuestionsCollection}" var="quest2" varStatus="itemsRow">

varStatus 使您可以访问带有索引属性的 bean 对象,您可以利用它来发挥自己的优势。

在您的情况下,您可以像我一样使用 jsp 中 foreach jstl 函数的索引属性来生成索引,并将其附加到命令对象的数组索引表示法中。命令对象当然必须遵循与路径集合名称相同的流程。这适用于无限数量的关卡,但随着关卡的进行,会变得越来越烦人。

这是一个大型的现场示例,因此如果您需要较小的东西,请向我展示您的标记,我将引导您完成它。

Ok bear with me on this one but the solution is simple an annoying at the same time. I ran into this a couple of months ago. I am going to show you my solution using the jstl libraries in my view for handling the collections.

<c:forEach items="${Questions}" var="quest" varStatus="itemsIndex">
        <fieldset>
          <legend>${quest.section}</legend>
          <form:form id="group${itemsIndex.index}" modelAttribute="ChoiceList" action="" method="POST" onsubmit="javascript:ajaxSave($(this).serialize()); return false;">
            <a id="Group${quest.id}"></a>
            <c:forEach items="${quest.qisQuestionsCollection}" var="quest2" varStatus="itemsRow">
              <div style="font-weight: bold; margin: 10px 0px">${quest2.shortText}</div>
              ( ${quest2.qisQuestionTypes.description} )<br/>
          ( ${quest2.helpText} )<br/>
              <a id="Question${quest2.id}"></a>
              <c:choose>
                <c:when test="${quest2.qisQuestionTypes.questionType == 'CHOOSEANY'}">
                  <c:forEach items="${quest2.qisChoicesCollection}" var="quest3" varStatus="indexStatus">
                    <c:forEach items="${ChoiceFields}" var="CField">
                      <c:set scope="request" value="${quest3}" var="ChoiceData"/>
                      <c:set scope="request" value="${CField}" var="ChoiceProperty"/>
                      <%
                                answerMap = (HashMap<QisChoice, Answer>) request.getAttribute("AnswerList");
                                choice = (QisChoice) request.getAttribute("ChoiceData");
                                if (answerMap.containsKey(choice.getChoiceID())) {
                                  Answer theAnswer = (Answer) answerMap.get(choice.getChoiceID());
                                  if (theAnswer != null) {
                                    if (theAnswer.getChoiceValue() != null) {
                                      request.setAttribute("itemValue", theAnswer.getChoiceValue());
                                      request.setAttribute("itemSelected", true);
                                    } else {
                                      request.setAttribute("itemSelected", false);
                                      request.setAttribute("itemValue", getReflectedValue(
                                              (QisChoice) request.getAttribute("ChoiceData"),
                                              (AccessorStruct) request.getAttribute("ChoiceProperty")));
                                    }
                                  }
                                } else {
                                  request.setAttribute("itemSelected", false);
                                  request.setAttribute("itemValue", getReflectedValue(
                                          (QisChoice) request.getAttribute("ChoiceData"),
                                          (AccessorStruct) request.getAttribute("ChoiceProperty")));
                                }
                                request.setAttribute("itemValue2", getReflectedValue(
                                        (QisChoice) request.getAttribute("ChoiceData"),
                                        (AccessorStruct) request.getAttribute("ChoiceProperty")));
                      %>
                      <c:choose>
                        <c:when test="${CField.visible == 'HIDDEN'}">
                          <form:hidden value="${itemValue2}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" />
                        </c:when>
                        <c:otherwise>
                          <c:choose>
                            <c:when test="${itemSelected}">
                              <form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" checked="true" /><br/>
                            </c:when>
                            <c:otherwise>
                              <form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" /><br/>
                            </c:otherwise>
                          </c:choose>

                        </c:otherwise>
                      </c:choose>
                    </c:forEach>
                  </c:forEach>
                </c:when>

            <input type="submit" value="Save Section"
                   class="button-main" />
          </fieldset>
        </form:form>
      </c:forEach>`

The Key bit is in this line

<form:checkbox value="${itemValue}" label="${quest3.description}" path="question[${itemsRow.index}].choice[${indexStatus.index}].${CField.beanName}" checked="true" /><br/>

To link up the command object with its collection for the postback you have to show the indice of the element as part of the spring path. In my case I have two levels of collections to track

<c:forEach items="${quest.qisQuestionsCollection}" var="quest2" varStatus="itemsRow">

varStatus gives you access to a bean object with the index property you can use to your advantage.

In your case you can do just use the index property of the foreach jstl function in the jsp to generate the indice like I did and append it to the array index notation of your command object. The command object must of course follow the same flow as the path collection names. This works for an infinite number of levels but gets more annoying as we go.

This is a large live example so if you need something smaller show me your markup and I will walk you throgh it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文