如何在Struts 2操作类中检索复选框值? [英] How to retrieve checkbox values in Struts 2 action class ?

查看:61
本文介绍了如何在Struts 2操作类中检索复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsp页面中具有动态的复选框,如下所示.

I have dynamic number of checkboxes in jsp page as given below.

<s:iterator value="details"  status="stat">
         <s:checkbox name="checkbox"  theme="simple" />
         <s:hidden name="hidden" value="%{top}" /> 
</s:iterator>

比方说4个复选框.

动作类中的二传手是

public void setCheckbox(boolean s[])
{
    System.out.println(s.length);
}

如果我未选中任何复选框,则s.length为零. 如果我选择第三个复选框,则s.lenght为1,并且正在使用true初始化s[0]. 但问题在于,如何找到第三个复选框或另一个复选框.

If I don't select any checkbox, s.length is zero. If I select 3rd checkbox, s.lenght is 1 and it is initializing s[0] with true. But problem is here how can I find whether 3rd checkbox is selected or another checkbox is selected.

如果我选择第三个复选框,我认为二传手会初始化s[]={false,false,true,false}.但是它正在初始化s[]={true}.

I thought that setter would initialize s[]={false,false,true,false}, if I select third checkbox. But it is initializing s[]={true}.

那些选定框的相应隐藏值是执行数据库中所需操作所必需的.但是为此,我需要找到选择复选框.
请给我建议任何解决方案.

Corresponding hidden values of those selected boxes are required to perform required action in database. But for that I need to find select checkboxes.
Please suggest me any solution.

推荐答案

由于您的复选框都被命名为相同(checkbox),因此Struts2只是传递了以下内容:

Because your checkboxes are all named the same (checkbox), Struts2 is just passing the following:

checkbox=true&checkbox=true&checkbox=true

这是否意味着您省略了第二,第三或第四复选框?

Does that mean that you omitted the second, third, or fourth checkbox?

您真正想要的不是布尔数组,而是整数到布尔的映射.这是一个示例:

What you actually want is not an array of booleans, but a map of Integer to Boolean. Here's an example:

public class MyAction extends ActionSupport {
  private Map<Integer, Boolean> checkboxes;

  ...

  public Map<Integer, Boolean> getCheckboxes() {
    return checkboxes;
  }

  public void setCheckboxes(Map<Integer, Boolean> checkboxes) {
    this.checkboxes = checkboxes;
  }
}

示例JSP

<s:iterator value="details" status="stat">
  <%-- this outputs checkboxes[0], checkboxes[1], etc. --%>
  <s:checkbox name="checkboxes[%{#stat.index}]"  theme="simple" />
</s:iterator>

示例结果

  • 0->是
  • 1->假
  • 2->是
  • 3->是
  • Example Result

    • 0 -> true
    • 1 -> false
    • 2 -> true
    • 3 -> true
    • 这篇关于如何在Struts 2操作类中检索复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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