0

How to bind input elements to an arraylist element in Spring MVC?

The view model:

public class AssigneesViewModel {

    private int evaluatorId;
    private int evaluatedId;
    private String evaluatorName;
    private String evalueatedName;
    //getters and setters
}

The model attribute:

public class AssignEvaluationForm{
   private ArrayList<AssigneesViewModel> options;
   public ArrayList<AssigneesViewModel> getOptions() {
      return options;
   }

   public void setOptions(ArrayList<AssigneesViewModel> options) {
      this.options = options;
   }
}

Controller

@RequestMapping(value="addAssignment", method = RequestMethod.GET)
public String addAssignment(Model model){
     model.addAttribute("addAssignment", new AssignEvaluationForm());
     return "addAssignment";
}

Then in the jsp i have 4 hidden inputs which represent the fields for the evaluatedId, evaluatorId, evaluatorName, evaluatedName -> options[0]. How i am going to write the jsp code to map those inputs with an element of the arrayList?

Update:

<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>

With this update i get the following error:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'options[]' available as request attribute
laura
  • 2,085
  • 13
  • 36
  • 68
  • Similar questions here. [Follow this link][1] [Alternate this one also][2] [1]: http://stackoverflow.com/questions/9617207/listfoo-as-form-backing-object-using-spring-3-mvc-correct-syntax [2]: http://stackoverflow.com/questions/15480397/how-to-send-list-of-object-to-view-and-back-to-post-method-in-controller – Mizanur Rahman Mojumder Apr 20 '15 at 11:31
  • @MizanurRahmanMojumder Thank you for your suggestion, but i have already implemented the answers from the related posts. I get Neither BindingResult exception. I updated the question. Clearly i make a mistake somewhere. – laura Apr 20 '15 at 12:08

1 Answers1

0
<form:input path="addAssignment.options[${vs.index}].evaluatedId" value="1"></form:input>

Instead of this addAssignment.options[${vs.index}].evaluatedId

Use this -> option.evaluatedId

Or you might reach value with arraylist get -> ${addAssignment.options.get(vs.index).evaluatedId} , try to turn out that ${} jstl's call curly brackets. BTW i'm not sure this last example work on path="" attribute.

İlker Korkut
  • 3,129
  • 3
  • 30
  • 51
  • The array list of options is empty on GET method. Then the user is adding some inputs and on POST i want to save those inputs as options[0]. I am not sure that it is very clear. It is even possible? – laura Apr 20 '15 at 13:35
  • 1
    Your question in your comment is irrelevant to your main question. If you want to send data as an array, in name attribute name="options[]" normally use on checkboxes and radio buttons. But you have a problem getting evaluatedId in your jsp. – İlker Korkut Apr 20 '15 at 13:39