Spring mvc how to bind a domain object that has a collection as its property

I have a domain object called Order, and it has a collection attribute called serviceOrders that stores the m: m relationship relationships of the service relationship.

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;

there is also a way to add an 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 do I use commandName to set this collection using "path", I think that it will only call the get set method of the Command object. how should I add serviceOrder to this Object command. I have no idea about this problem. any help would be much appreciated

+3
source share
2 answers

, ServiceOrder , #add ( ).

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/>

, spring.

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

varStatus bean , .

index jpel foreach jsp , , . , , . , , .

, , - , , .

0

Source: https://habr.com/ru/post/1759773/


All Articles