3

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 employee = Employee.findById(Integer
                            .parseInt(text));
                    setValue(employee);
                }
            });

which only takes 1 employee detail at a time. Is that a problem here???

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, i tried as u adviced, but i might be wrong somewhr, cud u plz guide me?
public class EmployeeForm { private static final Logger LOG = Logger.getLogger(EmployeeForm.class); private List<Candidate> candidates = new ArrayList<Candidate>(); public List<Candidate> getCandidates() { return candidates; } public void setCandidates(List<Candidate> candidates) { this.candidates = candidates; } } @RequestMapping(value = "{id}/empl", method = RequestMethod.POST) public String getEmployeeDetails( @ModelAttribute("candidateAddAttribute") @Valid EmployeeForm candidates, BindingResult result, @PathVariable("id") int requestId, Model model) {//code}
when i debug the above code, candidates is null when i try to use it in my controller... shud i change something in the code?
also, when i try to see LOG.error(candidates.getCandidates().get(0).getCandidateBlurb()); in my controller, i get ERROR - SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/panel-requests] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] with root cause java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
what does the form data sent in the request look like? IIRC you should have a list of items like candidates[0].name, candidates[0].age, candidates[1].name etc
|
0

you can try this :

List<Candidate> candidates = ListUtils.lazyList(new ArrayList<Candidate>(),FactoryUtils.instantiateFactory(Candidate.class));

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.