使用Struts2中的复选框删除多个行 [英] Deleting multiple rows using checkboxes in Struts2

查看:194
本文介绍了使用Struts2中的复选框删除多个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML表格中显示行,并在表格的每一行的列中都有一个复选框,如下所示。

I display rows in an HTML table with a checkbox in a column in each row of the table something like the following.

<s:iterator value="objects" status="loopStatus">

    <td>
        <s:label for="%{id}" value="%{#loopStatus.index+1}"/>
        <s:checkbox id="%{id}" name="chk" fieldValue="%{id}" value="false"/>
    </td>

    <!--Other columns-->

</s:iterator>

objects 在操作类中初始化的JPA实体 List< Entity> id 是对应于数据库中主键列的 Long 类型的值。

objects is is a list of objects of a JPA entity, List<Entity> initialized in an action class. id is a value of type Long that corresponds to a primary key column in the database.

提交表单时,这些复选框的值设置为操作类中的相应属性。

The values of these checkboxes are set to its corresponding property in an action class, when the form is submitted.

private List<Long>chk;

//setters & getters.

所选复选框的值已正确设置为 chk 。这个值列表用于从数据库中删除行。

The values of selected checkboxes are correctly set to this property chk. This list of values is then used to delete rows from the database.

当表中只有一行时会出现问题。在这种情况下,发生不必要的转换错误,而执行除了删除(如​​更新/插入行)之外的操作,这需要选择这个复选框,这是非常不必要的,并且只有在执行删除时才会发生。

This works just fine. A problem occurs, when there is only one row in the table. In which case, an unnecessary conversion error occurs, while performing operations other than deletion (like updating/inserting a row) which requires this checkbox to be selected which is quite unnecessary and should happen only while performing deletion.


字段chk的字段值无效。

Invalid field value for field "chk".

添加一个与 chk 同名的额外复选框,则此转换错误消失。

If I deliberately add an extra checkbox with the same name chk then, this conversion error disappears.

据推测,复选框未正确映射到 Long List< Long> 类型的值列表。

Presumably it appears that a single checkbox is not correctly mapped to a list of values of type Long, List<Long>.

当在HTML表中只显示一行时,如何摆脱此错误?

What is the way to get rid of this error, when there is only a single row to be displayed in an HTML table?

推荐答案

如果不选中单个复选框,则默认值为 false 。您可以在复选框拦截器 uncheckedValue 属性中设置默认值。

If you don't check a single checkbox it value by default is set to false. You can set the default in checkbox interceptor uncheckedValue property.

此外,您可以考虑添加隐藏字段,以便 checkbox 拦截器将认为有多个chekboxes,当只有一个,并且不会添加默认值。

Additionally you may consider to add hidden field so that checkbox interceptor will think there are multiple chekboxes when there is only one and won't add default value.

对于您的复选框,隐藏的字段名称将为 __ checkbox_chk

For your checkbox the hidden field name will be __checkbox_chk.

<s:hidden name="__checkbox_chk"/>

这篇关于使用Struts2中的复选框删除多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆