验证struts2复选框已选中 [英] Validate struts2 checkbox is checked

查看:215
本文介绍了验证struts2复选框已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证一个Struts2复选框被选中(布尔属性),但验证总是失败的。这里是我有:

I would like to validate that a Struts2 checkbox is selected (boolean property) but the validation always fails at the moment. here is what I have:

<s:checkbox key="agreetc" fieldValue="agreedtc"
            label="I have read and agree to the terms and conditions"/>

<field name="agreetc">
    <field-validator type="fieldexpression">
        <param name="expression">agreetc == true</param>
        <message>You must agree to the terms and conditions</message>
    </field-validator>
</field>

操作中 agreetc 始终为 false ,因为字段的setter不会被调用。如果我用getter / setter和 s:textfield
添加一个名为 test 被选中,但我不能从 s:radio s:select s:checkbox 。其他操作我已成功使用 s:radio s:select

The value of agreetc in the action is always false because the setter for the field never gets called. If I add a String called test with a getter/setter and a s:textfield then that value is picked but I can't get the values from s:radio, s:select or s:checkbox in this action. Other actions I have been successful with s:radio and s:select.

有人有任何想法吗?

推荐答案

如果你想从 fieldValue 属性,然后使您的 agreedtc 变量String类型不是布尔值。

If you want to receive value as String from fieldValue attribute then make your agreedtc variable String type not boolean. Then your validation as it is now should work.

但是如果你希望它在你的action类中是一个布尔值,那么删除 fieldValue < s:checkbox> 标记并将您的验证表达式更改为:

But if you want it to be a boolean in your action class then remove the fieldValue attribute from <s:checkbox> tag and change your validation expression to:

<field name="agreetc">
    <field-validator type="fieldexpression">
        <param name="expression">agreetc</param>
        <message>You must agree to the terms and conditions</message>
    </field-validator>
</field>

这篇关于验证struts2复选框已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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