views:

596

answers:

2

Hi All,

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,

A: 

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.

Trick
A: 

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: http://stackoverflow.com/questions/798118/how-can-i-get-checkbox-values-from-struts2-checkbox-in-displaytag-to-action-class

Nate