emeh
emeh

Reputation: 823

To get multiple checkboxes value

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,

Upvotes: 0

Views: 3739

Answers (2)

Nate
Nate

Reputation: 2456

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

Upvotes: 1

Trick
Trick

Reputation: 3859

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.

Upvotes: 0

Related Questions