Pages Navigation Menu

Coding is much easier than you think

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

Posted by on Dec 25, 2013 in Display Tag, Struts 2 Tutorial, Struts-2 | 6 comments

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 property="product" title="Product Name"/>
<display:column property="price" title="Price"/>

<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”.

Read More