0

I have a table with each rows contain checkbox (Like gmail inbox). I want to select few checkboxes and submit data in one go to action class. Please let me know how can i submit form using checkboxes.

A. Kumar
  • 11
  • 4
  • possible duplicate of [submitting a form when a checkbox is checked](http://stackoverflow.com/questions/10602470/submitting-a-form-when-a-checkbox-is-checked) – Phill Treddenick Jun 16 '15 at 03:59
  • Your question is unclear. Do you mean you want to submit when you check a checkbox; or do you want to check several checkboxes, submit, and have the Jave-side form contain which checkboxes were checked? – Mar Jun 17 '15 at 18:45
  • Martin, i need to do both way. single checkbox submit as well as multi checkbox selection submit. – A. Kumar Jun 22 '15 at 03:26

1 Answers1

0

First of all you must ensure that your form bean and your form action class are already working otherwise even if you prepare your jsp file you will not be able to check its correctness (Struts 1.2 drawback)

Here goes your jsp code

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:html>
    <html:form action="/formAction" method="GET">
        <table>
            <tr>
                <td>Data 1</td>
                <td>
                    <html:checkbox property="chk" value="value1"></html:checkbox>dln</td>
            </tr>
            <tr>
                <td>Data 2</td>
                <td>
                    <html:checkbox property="chk" value="value2"></html:checkbox>dln</td>
            </tr>
            <tr>
                <td>Data 3</td>
                <td>
                    <html:checkbox property="chk" value="value3"></html:checkbox>dln</td>
            </tr>
            <tr>
                <td>Data 4</td>
                <td>
                    <html:checkbox property="chk" value="value4"></html:checkbox>dln</td>
            </tr>
            <tr>
                <td>
                    <html:submit value="Register"></html:submit>
                </td>
            </tr>
        </table>
    </html:form>

Form Bean

public class MyFormBean extends ActionForm {
private String chk;
[...]//other fields
public String getChk() {
    return chk;
}

public void setChkString chk) {
    this.chk= chk;
}

[....] // other getters and setters }

Action class's execute method

[...]
MyFormBean myForm = (MyFormBean) form;
String chkVal = myForm.getChk();
[...]

Form Bean Entry in struts-config.xml

[...]
<form-bean name="myBean" type="pkg.MyFormBean"/>
[...]

Form Action Entry in struts-config.xml

[...]
<action path="/formAction" type="pkg.formAction" name="myBean" scope="request">
[...]
Anand
  • 114
  • 1
  • 2
  • 14
  • Thanks Anand for your detailed answers. I have multiple TD in same TR and I need to capture those TD as well in my form bean. If something is checked in checkbox then submit the form along with all TD associated with TR. I also need to have select all option to submit multiple TR to action class. Please let me know your thoughts. – A. Kumar Jun 21 '15 at 05:10