Pages Navigation Menu

Coding is much easier than you think

How to get checkbox values from struts2 checkbox in displaytag to action class

How to get checkbox values from struts2 checkbox in displaytag to action class

 
Consider a scenario, such that we have a list of items and each item can be selected by checking its checkbox. If a submit button is clicked after selecting all necessary checkboxes. So now, in our Action class, we could get the values of checkboxes which had been selected, by which we can implement delete functionality.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

The following snippet of code is used to retrieve the value from a checkbox used inside displaytag.

 

<display:table name="productList" pagesize="10" requestURI="productAction" id="row">

<display:column title="Delete">
<s:checkbox name="productList[%{#attr.row_rowNum - 1}].chkBox" 
  id="check%{#attr.row_rowNum - 1}" value="%{#attr.row.chkBox}"/>
</display:column>

<display:column property="product" title="Product Name"/>
<display:column property="price" title="Price"/>

</display:table>
<s:submit action="removeProduct" value="Delete"
  onClick="return confirm('Do you want to delete these items?');"/>

 
In our Action class, we can get the values of checkbox by creating getter & setter for the field “chkBox”.
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.