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
2
  • Similar questions here. [Follow this link][1] [Alternate this one also][2] [1]: stackoverflow.com/questions/9617207/… [2]: stackoverflow.com/questions/15480397/… Commented Apr 20, 2015 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. Commented Apr 20, 2015 at 12:08

1 Answer 1

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.

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

2 Comments

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

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.