I am trying to get a list of selected candidates to my controller using @modelAttribute with their respective id and blurb. I am able to bring one Candidate correctly but i don't know how to bring a list of candidates thru... I tried to add List<> as i have shown below, but i get
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>
The above form at first displays list of employee and when user selects employee, enters blurb and hits add candidate button, i take data to 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
}
How do I implement List in this case? Thanks in advance.
EDITED PART I tried sending 2 candidate's details, firebug sends it correctly like - Parameters candidateBlurb BLURB sar candidateBlurb BLURB dann employee 1 employee 2
so it can be a problem in initBinder that i ma using, binder.registerCustomEditor(Employee.class, new PropertyEditorSupport() { public String getAsText() { return Long.toString(((Employee) getValue()).getId()); }
public void setAsText(final String text) {
Employee 开发者_JAVA百科employee = Employee.findById(Integer
.parseInt(text));
setValue(employee);
}
});
which only takes 1 employee detail at a time. Is that a problem here???
Create a POJO class which contains the List as a property, such as
public class EmployeeForm {
private List<Candidate> candidates;
public List<Candidate> getCandidates() { ... }
public void setCandidates(List<Candidates>) { ... }
}
and use this in your @RequestMapping method's signature rather than a List<Candidate>
directly.
The error message you get is Spring complaining that it doesn't know how to instantiate a List interface, in order for Spring to bind the request parameters in the form to it. You want to provide a simple form/command class to data-bind the form parameters to, Spring knows how to handle a list from there.
you can try this :
List<Candidate> candidates = ListUtils.lazyList(new ArrayList<Candidate>(),FactoryUtils.instantiateFactory(Candidate.class));
I've run into this same issue, half of the fields are binding correctly and half aren't. I noticed that the half that are not binding correctly (thus giving me NULL in the controller) were wrapped in a DIV.
When I moved these fields outside the DIV, bingo, they were bound properly in the controller.
I see that you have some fields wrapped in a DIV too, I'd recommend moving them out and seeing if they become visible to your controller.
精彩评论