Collections SpringMVC,如何绑定具有集合作为其属性的域对象
我有一个名为Order的域对象,它有一个名为serviceOrders的集合属性,其中包含一个服务集合——Order m:m关联关系Collections SpringMVC,如何绑定具有集合作为其属性的域对象,collections,spring-mvc,command,Collections,Spring Mvc,Command,我有一个名为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 service
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”,我想它只会调用Command对象的get set方法。如何将serviceOrder添加到此命令对象。我对这个问题一无所知。如果您的ServiceOrder实例具有唯一的id,您的服务方法应该是#add(长id),我们将非常感谢您的帮助 好的,请接受我的意见,但解决方法很简单,同时也很烦人。几个月前我遇到了这个。我将向您展示在我的视图中使用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>`
${quest.section}
${quest2.shortText}
(${quest2.qisQuestionTypes.description})
(${quest2.helpText})
`
关键点在这一行
<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对象,您可以使用该属性发挥优势
在您的例子中,您可以使用jsp中foreach jstl函数的index属性来生成索引,就像我所做的那样,并将其附加到命令对象的数组索引符号中。命令对象当然必须遵循与路径集合名称相同的流程。这适用于无限多个级别,但随着我们的进行,它会变得更加烦人
这是一个大型的实时示例,所以如果您需要更小的东西,请向我展示您的标记,我将带您通过它