I want to implement delete functionality using checkboxes in my struts2 application. I’m using display tag in my jsp page to show the reports. I need to provide checkboxes with reports to delete the selected reports. I have tried this code in my jsp page :
<s:form name="listPage">
<display:table name="egnreports" class="table" id="egnreports" requestURI="" export="true" pagesize="30">
<display:column>
<s:checkbox name="checked" fieldValue='%{egnreports.id}' />
</display:column>
<display:column value="<%=egnreports_rowNum%>" title="S.No." />
<display:column property="createDate" sortable="true" titleKey="egnreport.createDate" style="width:13.5%"/>
<-- Other Table data here -->
</display:table>
</s:form>
<c:out value="${buttons}" escapeXml="false" />
In my action class, I have used this :
private String[] checked;
public String[] getChecked() {
return checked;
}
public void setChecked(String[] checked) {
this.checked=checked;
}
When I try to retrive the selected values to delete the corresponding reports, I get a null pointer exception :
for (String code : checked) {
System.out.println(code + " is selected");
System.out.println("id="+egnreport.getId());
egnreportManager.remove(egnreport.getId());
saveMessage(getText("ireport.deleted"));
}
If I use
<display:column>
<input type="checkbox" name="checked" value="${egnreports.id}"/>
</display:column>
in place of s:checkbox tag, I’m able to see the values in the reports page (using firebug), but still they do not get populated in the action class.
using
<input type="checkbox" name="checked" value="${egnreports.id}"/>
i get
<td> <input type="checkbox" value="5" name="checked"> </td>
as rendered html, while with
<s:checkbox name="checked" fieldValue='%{egnreports.id}' />
I get
<td>
<div id="wwgrp_egnDeskReports_checked" class="wwgrp">
<div id="wwctrl_egnDeskReports_checked" class="wwctrl">
<input id="egnDeskReports_checked" type="checkbox" value="" name="checked">
<input type="hidden" value="" name="__checkbox_checked">
</div>
</div>
</td>
Also I can’t use "${egnreports.id}" with s:checkbox to get the values of id.
Where did I go wrong? can anybody help me with this?
i have got the solution : i have to use fieldValue=”%{#attr.egnreports.id}” i.e,
to pass the checked values.
Also remember to include buttons inside form tag, i didn’t pay attention to this before:
🙂