java 如何从 displaytag 中的 struts2 复选框获取复选框值到操作类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:51:15  来源:igfitidea点击:

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

javastruts2displaytag

提问by harshalb

I am working on struts2application in which i am using displaytagfor pagination support.

我正在研究struts2应用程序,其中我使用displaytag进行分页支持。

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.

为此,我想要检查的国家/地区的 ID。为此,我必须采用数组中的值。

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

问题是如何从 jsp 发送值然后在操作类中获取它

回答by Rich Kroll

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.

如果将 String[] 添加到与复选框名称相同的操作中并通过访问器(getter/setter)公开它,struts 2 应该自动填充它。

回答by Journeyman Programmer

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

  1. 代替所有 id 列的 'id',附加一个序列号,以便每个 id 都有一个唯一的名称,例如 'id1'、'id2' 等

  2. 创建一个拦截器来收集以“id”为前缀的参数值

  3. 配置您的操作以使用拦截器

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

不过,我的支柱 fu 生锈了。可能有更好的方法。