2010-08-15 23 views
3

我有一个名为Order的域对象,它有一个名为serviceOrders的集合属性,其中服务集合--- order m:m关联关系是保持。Spring mvc,如何绑定一个集合作为其属性的域对象

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); 
} 

我应该如何使用命令名来设置这个集合与“路径”,我认为这将只是调用其get set方法的关联方法命令对象。我应该如何将serviceOrder添加到此命令Object中。我不知道这个问题。任何帮助将不胜感激

回答

0

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

0

好吧,在这一个承担,但解决方案是简单的一个恼人的同时。几个月前我遇到了这个问题。我将使用视图中的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函数的index属性来生成像我一样的指示,并将它追加到命令对象的数组索引表示法中。命令对象当然必须遵循与路径集合名称相同的流程。这适用于无限级别,但在我们走的时候变得更加烦人。

这是一个很大的现场例子,所以如果你需要更小的东西给我看你的标记,我会走你throgh它。

相关问题