我正在尝试使用@modelAttribute 及其各自的 id 和 blurb 向我的 Controller 获取选定候选人的列表。我能够正确地带来一个候选人,但我不知道如何通过......我尝试添加列表<>,但我得到了
ERROR - SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/panel-requests] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
JSP-
<form:form modelAttribute="candidateAddAttribute"
action="/panel-requests/requests/${panelRequestForId.id}/empl" method="post">
<c:forEach items="${candidates}" var="candidates">
<select name="employee" id="employee" disabled="disabled">
<option value="default" selected="selected">${candidates.candidateName}</option>
</select>
<textarea rows="3" cols="40" id="candidateBlurb" name="candidateBlurb"
disabled="disabled">${candidates.candidateBlurb}</textarea>
<textarea rows="2" cols="20" id="candidateCV" name="candidateCV"
disabled="disabled">${candidates.candidateCV}</textarea>
</c:forEach>
<div id="candidateDiv" id="candidateDiv">
<select name="employee" id="employee">
<option value="default" selected="selected">Select Employee</option>
<c:forEach items="${employees}" var="employee">
<option value="${employee.id}" id="${employee.id}">
${employee.employeeName}- ${employee.employeeCV}<
/option>
</c:forEach>
</select>
<textarea rows="3" cols="40" id="candidateBlurb"
name="candidateBlurb">BLURB</textarea>
<div id="employeeCv"></div>
<input type="submit" value="Add Candidate" />
</div>
</form:form>
上面的表单首先显示员工列表,当用户选择员工、输入简介并点击添加候选人按钮时,我将数据发送到 Controller 。
Controller :
@RequestMapping(value = "{id}/empl", method = RequestMethod.POST)
public String getEmployeeDetails(
@ModelAttribute("candidateAddAttribute") @Valid List<Candidate> candidates,
BindingResult result, @PathVariable("id") int requestId, Model model) {
//implementation goes here
}
在这种情况下我该如何实现 List?提前致谢。
编辑部分 我尝试发送 2 个候选人的详细信息,firebug 正确发送它,例如 - 参数 候选人Blurb BLURB sar candidateBlurb BLURB 丹恩 员工 1 员工 2
所以这可能是我使用的 initBinder 中的一个问题, binder.registerCustomEditor(员工类, 新的 PropertyEditorSupport() { 公共(public)字符串 getAsText() { 返回 Long.toString(((Employee) getValue()).getId());
public void setAsText(final String text) {
Employee employee = Employee.findById(Integer
.parseInt(text));
setValue(employee);
}
});
一次只需要 1 个员工详细信息。这是这里的问题吗???
最佳答案
创建一个包含 List 作为属性的 POJO 类,例如
public class EmployeeForm {
private List<Candidate> candidates;
public List<Candidate> getCandidates() { ... }
public void setCandidates(List<Candidates>) { ... }
}
并在您的@RequestMapping 方法的签名中使用它而不是 List<Candidate>
直接。
您收到的错误消息是 Spring 提示它不知道如何实例化 List 接口(interface),以便 Spring 将表单中的请求参数绑定(bind)到它。你想提供一个简单的表单/命令类来将表单参数数据绑定(bind)到,Spring 知道如何从那里处理列表。
关于java - 如何使用 @ModelAttribute 将列表绑定(bind)到 JSP 表单中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604301/