【问题标题】:Spring mvc, how to bind a domain object that has a collection as its propertySpring mvc,如何绑定具有集合作为其属性的域对象
【发布时间】:2011-03-30 02:52:04
【问题描述】:

我有一个名为 Order 的域对象,它有一个名为 serviceOrders 的集合属性,其中包含一个服务集合 --- order 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来设置这个带有“路径”的集合,我认为它只会调用它的命令对象的get set方法。我应该如何将 serviceOrder 添加到这个命令对象。我不知道这个问题。任何帮助将不胜感激

【问题讨论】:

    标签: collections spring-mvc command


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在这个问题上请耐心等待,但解决方案既简单又烦人。几个月前我遇到了这个问题。我将向您展示我的解决方案,在我的视图中使用 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 使您可以访问具有 index 属性的 bean 对象,您可以利用它来发挥自己的优势。

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

      这是一个大型的现场示例,因此如果您需要更小的内容,请向我展示您的标记,我会引导您完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        相关资源
        最近更新 更多