[英]How do I bind item from List to form modelAttribute using Spring MVC
[英]How to bind a List using @ModelAttribute to controller from a JSP form
我正在嘗試使用 @modelAttribute 及其各自的 id 和簡介獲取我的 controller 的選定候選人列表。 我能夠正確地帶上一個候選人,但我不知道如何通過...我嘗試添加 List<>,如下所示,但我得到了
錯誤 - 嚴重:servlet [dispatcher] 在路徑 [/panel-requests] 的上下文中的 Servlet.service() 引發異常 [請求處理失敗; 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]: 指定 class 是一個接口
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 個候選人的詳細信息,螢火蟲正確發送它就像 - 參數 CandidateBlurb BLURB sar CandidateBlurb BLURB dann 員工 1 員工 2
所以它可能是我使用的 initBinder 中的一個問題, binder.registerCustomEditor(Employee.class, new PropertyEditorSupport() { public String getAsText() { return Long.toString(((Employee) getValue()).getId()) ; }
public void setAsText(final String text) {
Employee employee = Employee.findById(Integer
.parseInt(text));
setValue(employee);
}
});
一次只需要 1 個員工詳細信息。 這里有問題嗎???
創建一個 POJO class ,其中包含 List 作為屬性,例如
public class EmployeeForm {
private List<Candidate> candidates;
public List<Candidate> getCandidates() { ... }
public void setCandidates(List<Candidates>) { ... }
}
並在您的 @RequestMapping 方法的簽名中使用它,而不是直接使用List<Candidate>
。
您收到的錯誤消息是 Spring 抱怨它不知道如何實例化 List 接口,以便 Spring 將表單中的請求參數綁定到它。 您想提供一個簡單的表單/命令 class 將表單參數數據綁定到,Spring 知道如何從那里處理列表。
我遇到了同樣的問題,一半的字段綁定正確,一半沒有。 我注意到沒有正確綁定的一半(因此在控制器中給了我 NULL)被包裹在一個 DIV 中。
當我將這些字段移到 DIV 之外時,賓果游戲,它們在 controller 中正確綁定。
我看到您也有一些字段包含在 DIV 中,我建議將它們移出並查看它們是否對您的 controller 可見。
你可以試試這個:
List<Candidate> candidates = ListUtils.lazyList(new ArrayList<Candidate>(),FactoryUtils.instantiateFactory(Candidate.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.