How can i get checkbox values from struts2 checkbo

2019-02-20 04:53发布

I am working on struts2 application in which i am using displaytag for pagination support.

Now i want a check box for each row in the table for that i am doing this.

<display:table name="countryList" export="true" class="table" id="countryList" pagesize="${selectedPageSize}" decorator="org.displaytag.decorator.TotalTableDecorator" >        
    <display:column property="id"  title="ID" paramId="id" />
    <display:column property="name" title="Name"  sortable="true"/>
    <display:column title="Delete All">
        <s:checkbox  id="check" name="check" fieldValue="%{#attr.countryList.id}" theme="simple"/>
    </display:column>
</display:table>  

<s:submit action="deleteall"  value="DeleteSelected" />

till here its work fine. now i want to delete all the countries that are checked through the check box.

for that i want the ids of the countries that are checked .for that i have to take the values in an array.

The problem is how can i send the values from jsp and then get it at the action class

2条回答
Juvenile、少年°
2楼-- · 2019-02-20 05:16

This is how I would do it.

  1. instead of 'id' for all id columns, append a sequence number so that each id has a unique name, such as 'id1', 'id2' etc

  2. create a interceptor that collects values of parameters prefixed with 'id'

  3. configure your action to use the interceptor

My strut fu is rusty though. There probably is a better way.

查看更多
我命由我不由天
3楼-- · 2019-02-20 05:20

If you add a String[] to your action named the same as your checkbox(s) and expose it via accessors (getters/setters) struts 2 should auto populate it.

查看更多
登录 后发表回答