<s:复选框>不以 struts2 形式自动填充 [英] &lt;s:checkbox&gt; not auto populating in struts2 form

查看:52
本文介绍了<s:复选框>不以 struts2 形式自动填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下如何使用复选框拦截器.这里我的问题是我必须将复选框值保存为 'Y' 如果它在我查看来自数据库的详细信息时同时选中其他明智的 'N' 如果复选框的值为 'Y' 它有要检查,否则不应该检查.

Can any one please explain how to use checkbox interceptor. Here my problem is i have to save the checkbox value as 'Y' if it was checked other wise 'N' at the same time when i am viewing the details which are coming form database if the value of checkbox is 'Y' it has to be checked otherwise it should not checked.

推荐答案

在你的 getter setter 中放入转换代码

In your getter setter put conversion code

private String mycheckbox;

public String execute(){
    //In action you will always get 'Y' or 'N' 
    System.out.println("In action mycheckbox :"+mycheckbox);
    return SUCCESS; 
}

public void setMycheckbox(String mycheckbox) {
    if(mycheckbox.equalsIgnoreCase("false"))
    {
        this.mycheckbox="N";    
    }
    else
    {
    this.mycheckbox = "Y";
    }
}

public String getMycheckbox() {
    if(mycheckbox.equalsIgnoreCase("N"))
    {
        //In JSPs you will always get true or false
        this.mycheckbox="false";    
    }
    else
    {
    this.mycheckbox = "true";
    }
    return mycheckbox;
}

在 JSP 中

<s:checkbox name="mycheckbox" id="mycheckbox"  />

所以在操作中你会得到 'Y''N' 但是对于 struts 标签,它将使用 truefalse.如果 'Y' 存在,复选框将被选中.

So in action you will get 'Y' and 'N' But for struts tags it will use true or false. and check box will get selected if 'Y' is there.

所以上述解决方案可以解决您的问题.

您也可以对拦截器使用相同的逻辑,但我认为 getter setter 是最简单的方法,因为您将强制使用 getter setter.

You can use the same logic with interceptors too but I think getter setter is the easiest way because you will have getter setters compulsorily in action.

写作时

<s:checkbox name="mycheckbox" id="mycheckbox" fieldValue="Y" />

生成的 Html 是

Generated Html is

 <input id="mycheckbox" type="checkbox" value="Y" name="mycheckbox"></input>

 <input id="__checkbox_mycheckbox" type="hidden" value="Y" name="__checkbox_mycheckbox"></input>

现在您可以看到它生成的隐藏字段.因此,您将在复选框的 value="Y" 中获得字段值 Y.

Now you can see it generated hidden field. So the fieldvalue Y you will get in value="Y" of checkbox.

情况 1:如果在 mycheckbox 变量中选中了复选框.

Case 1: if checkbox is selected in action in mycheckbox variable.

在操作中 mycheckbox :Y(因为 <input id="mycheckbox"..> 中的 value="Y")

In action mycheckbox :Y (because of value="Y" in <input id="mycheckbox"..>)

如果未选中复选框

在操作中 mycheckbox :false

In action mycheckbox :false

情况 2:但如果未提交此复选框

CheckBoxInterceptor 将出现在图片中,它将扫描带有 _checkbox 前缀的参数并执行操作 -> 将值设置为 false 所以即使隐藏字段中没有值,动作也会获取一些值,即 false

CheckBoxInterceptor will come in picture it will scan parameter with _checkbox prefix and does what -> it sets value to false So eventhough there were no value in hidden field action will get some value i.e. false

因此,如果选中 fieldValue 将返回到操作变量,如果未选中,则将其设置为 false.

So if selected fieldValue will be returned to action variable and if not selected it will set false to it.

这里你可以指定 setUncheckedValue("N"); 所以你可以在 __checkbox_mycheckbox 中得到 N 而不是在 mycheckbox 变量.mycheckbox 只会返回 false.

Here you can specify setUncheckedValue("N"); So you can get N in __checkbox_mycheckbox but not in mycheckbox variable. mycheckbox will return false only.

但是您需要记住的一件事是复选框值仅转换为布尔值因此即使您将设置此值,您也会在 __checkbox_mycheckbox 中获得值N"而不是在 mycheckbox.

But one thing you need to keep in mind that checkbox value is converted to boolean only So even though you will set this you will get the value "N" in __checkbox_mycheckbox not in mycheckbox.

这篇关于<s:复选框>不以 struts2 形式自动填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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