简体   繁体   中英

how to handle a multi row form without a binding object in spring MVC

I don't know to structure my question perfectly, but Im trying to get data from a html multiple row form and I dont want to use a binding objects and I dont want to have to many @RequestParam parameters on my request handling method. I want the form submission to return an array of int, but i get the following error HTTP Status 400 - Required int[] parameter 'department' is not present . I tried writing the form the same way one would do in php like this:

<form action="processForm" method="POST">
<select name="department[]">
    <option value="NONE">---SELECT---</option>
    <c:forEach items="${departments}" var="department">
    <option value="${department.getId()}">${department.getName()}</option>
     </c:forEach>
</select>
<select name="department[]">
    <option value="NONE">---SELECT---</option>
    <c:forEach items="${departments}" var="department">
    <option value="${department.getId()}">${department.getName()}</option>
     </c:forEach>
</select>
<select name="department[]">
    <option value="NONE">---SELECT---</option>
    <c:forEach items="${departments}" var="department">
    <option value="${department.getId()}">${department.getName()}</option>
     </c:forEach>
</select>
<select name="department[]">
    <option value="NONE">---SELECT---</option>
    <c:forEach items="${departments}" var="department">
    <option value="${department.getId()}">${department.getName()}</option>
    </c:forEach>
</select>
</form>

my controller request mapping methods are as follows

@Request(value = "/form",nethod = RequestMethod.GET)
public String renderForm(){
    return "form"
}

@Request(value = "processForm", method = RequestMethod.POST)
public String processForm(@RequestParam("department") int [] department){
    for (int i=0;i<department.lenth;i++){
        System.out.println(department[i]);
    }
}

thanks you in advance.

Change the @RequestParam to @RequestParam("department[]")

@Request(value = "processForm", method = RequestMethod.POST)
public String processForm(@RequestParam("department[]") int [] department){
    for (int i=0;i<department.lenth;i++){
        System.out.println(department[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM