0

I am having problems posting my ArrayList back to my controller on a Form Submit. I can get my values by using something like this:

@RequestBody MultiValueMap<String, String> formData

But this is not what I want, and this also provides my CSRF values which I don't care to evaluate on my form Submit.

I have a wrapper model class that looks like this:

public class CustomPayCodesWrapper {

    public ArrayList<CustomPayCodesModel> customCodes;

    public ArrayList<CustomPayCodesModel> getCustomCodes() {
        return customCodes;
    }

    public void setCustomCodes(ArrayList<CustomPayCodesModel> customCodes) {
        this.customCodes = customCodes;
    }
}

And my Model:

public class CustomPayCodesModel {

    public String codeName;
    public String codeValue;

    public String getCodeName() {
        return codeName;
    }

    public void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    public String getCodeValue() {
        return codeValue;
    }

    public void setCodeValue(String codeValue) {
        this.codeValue = codeValue;
    }
}

My .jsp:

<c:forEach var="item" items="${payCodes.customCodes}">
    <div class="row col-md-9">

         <div class="form-group row">
             <label for="payCode" class="col-md-2 col-form-label">${item.codeName}</label>
         <div class="col-md-10">
                 <input type="text" class="form-control" id="payCode" name="${item.codeName}" value="${item.codeValue}">
             </div>
         </div>

     </div>
 </c:forEach>

I have tried the following Controller bindings without any success:

@RequestMapping(value = "updateCustomPaymentCodes", method = RequestMethod.POST)
public ModelAndView updateCustomPayCodes(ModelAndView modelAndView, @ModelAttribute(value = "payCodes") CustomPayCodesWrapper userListWrapper, 
        ArrayList<CustomPayCodesModel> customCodes, @RequestBody MultiValueMap<String, String> formData, CustomPayCodesWrapper userListWrapper2) {


    return showCustomPaymentCodes(modelAndView);
}

1 Answer 1

1
+50

SpringMVC looks for specific field format in the html posted to parse parameters. In the case of an array it is looking for the name="customCodes[${status.index}].codeValue" field. This will be turned into the HTML that is name="customCodes[0].codeValue" and name="customCodes[1].codeValue" and so on. These fields will be found and used to bind to a List or Arrary element. I was able to get just the list with the following:

@PostMapping("/")
public ModelAndView updateCustomPayCodes(@ModelAttribute CustomPayCodesWrapper payCodes) {
    payCodes.customCodes.forEach(m->System.out.println(""+m.codeName+":"+m.codeValue));
    return buildModelAndView();
}

and

    <c:forEach items="${payCodes.customCodes}" var="customCode" varStatus="status">
        <div class="row col-md-9">        
             <div class="form-group row">
                 <label for="payCode" class="col-md-2 col-form-label">${customCode.codeName}</label>
                 <input type="hidden" name="customCodes[${status.index}].codeName" value="${customCode.codeName}"/>
             <div class="col-md-10">
                    <input type="text" class="form-control" name="customCodes[${status.index}].codeValue" value="${customCode.codeValue}"/>
                 </div>
             </div>
         </div>
     </c:forEach>

This based on the tutorial Spring MVC: Multiple Row Form Submit using List of Beans about doing the same

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

2 Comments

Thank you, you are right. Using the hidden field + the index was the key. Without the hidden field the bindings do not work, go figure!
Thanks for accepting the answer. The hidden field didn't have anything to do with the binding. The name="customCodes[${status.index}].codeName" field and its format is what handles the binding. See updates.

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.