Ajay. A
Ajay. A

Reputation: 71

Reading values from arrayOfCheckboxes checked in JSP page and pass it to a method in the Spring controller

Here is my JSP code

<% /* {"Group1","Group2","Group3","Group4"}; */
String[] groupSelection = (String[])request.getAttribute("group");      
for(int i = 0; i < groupSelection.length; i++)
{
%>
<input type="checkbox" name="<%= groupSelection[i]%>"> <%= groupSelection[i]%>
<%
}
%>

Now I wanted to read the array of groups selected and pass this array to a method in my Spring controller. Here is my java code:

@RequestMapping(value = Constants.RESULT_RETURN, method = RequestMethod.GET)
    public ModelAndView result(Locale locale, Model model,
            @RequestParam(value="groupSelection") String[] groupsEleted) {
......

But this is not working, comes up with exception "HTTP Status 400 - Required String[] parameter 'groupSelection' is not present" Please let me know how to access array of checkboxes selected in JSP from java. Thanks.

Upvotes: 0

Views: 216

Answers (1)

Harshal Patil
Harshal Patil

Reputation: 6759

You can use jQuery attribute selector like.

$('input:checked');

So you will get object then make it as serialize like

$('input:checked').serialize();

Then you will get serialize values.

For more details check this example

Upvotes: 1

Related Questions