如何将输入元素绑定到Spring MVC中的arraylist元素?
视图模型:
public class AssigneesViewModel {
private int evaluatorId;
private int evaluatedId;
private String evaluatorName;
private String evalueatedName;
//getters and setters
}
模型属性:
public class AssignEvaluationForm{
private ArrayList<AssigneesViewModel> options;
public ArrayList<AssigneesViewModel> getOptions() {
return options;
}
public void setOptions(ArrayList<AssigneesViewModel> options) {
this.options = options;
}
}
控制器
@RequestMapping(value="addAssignment", method = RequestMethod.GET)
public String addAssignment(Model model){
model.addAttribute("addAssignment", new AssignEvaluationForm());
return "addAssignment";
}
然后在jsp中我有4个隐藏的输入,它们代表evaluateId,evaluatorId,evaluatorName,evaluateName - &gt;的字段。选项[0]。 我将如何编写jsp代码以使用arrayList的元素映射这些输入?
更新
<form:form commandName="addAssignment" modelAttribute="addAssignment" id="addAssignment" method="POST">
//..........
<c:forEach items="${addAssignment.options}" var="option" varStatus="vs">
<div id="assigneesOptions" >
<form:input path="addAssignment.options[${vs.index}].evaluatedId" value="1"></form:input>
</div>
</c:forEach>
//..............
</form:form>
通过此更新,我收到以下错误:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'options[]' available as request attribute
答案 0 :(得分:0)
<form:input path="addAssignment.options[${vs.index}].evaluatedId" value="1"></form:input>
而不是addAssignment.options[${vs.index}].evaluatedId
使用此 - &gt; option.evaluatedId
或者你可以通过arraylist get获得价值 - &gt; ${addAssignment.options.get(vs.index).evaluatedId}
,试着证明$ {} jstl调用大括号。顺便说一句,我不确定这最后一个例子是否适用于path =&#34;&#34;属性。