Question

I am using Struts 2, JSP and JPA in my project. I have nearly 50 checkboxes in a single page. If i want to get the value of each checkbox in Action class, then i have write 50 getters and setters. Is there any easy way to get the values of all checkboxes in Action class.

Thank you,

Was it helpful?

Solution

If the checkboxes all have the same name, you can have a getter/setter for a String[].

So, you have this in the jsp:

<s:checkbox name="cb" fieldValue="whatever"/>
<s:checkbox name="cb" fieldValue="whatever2"/>
<s:checkbox name="cb" fieldValue="whatever3"/>

In your action, you'd have the following:

private String[] cb;
public String[] getCb() {
    return cb;
}

public void setCb(String[] cb) {
   this.cb = cb;
}

Similar SO question: How can i get checkbox values from struts2 checkbox in displaytag to action class

OTHER TIPS

What about checkboxlist? But you will have to change ftl template (if you use freemarker) to make the checkboxes vertical or any other way. I have it, if you need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top