java - 如何使用注释验证 Struts2 中的 boolean 字段?

标签 java validation struts2 ognl struts2-convention-plugin

给定 Boolean像这样的 Action 类中的字段。

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class TestAction extends ActionSupport implements Serializable, ValidationAware, ModelDriven<Entity>
{
    private Boolean boolField;

    public Boolean getBoolField()
    {
        return boolField;
    }

    public Boolean setBoolField(Boolean boolField)
    {
        this.boolField=boolField;
    }

    @Validations(requiredFields={@RequiredFieldValidator(fieldName="boolField", type= ValidatorType.FIELD, key="delete.row.confirm")})
    @Action(value = "testAction",
            results = {
                @Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Test.action"),
                @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    public String testAction()
    {            
        return ActionSupport.SUCCESS;
    }

    // The rest of the action class.
}

操作类中的字段boolField仅当设置为 true 时才应验证。它可能是一个隐藏字段<s:hidden>或者可以通过查询字符串参数设置。

Thisthis问题使用 XML 配置进行 boolean 字段验证,但不提及任何有关注释的内容。

如何使用注释验证此类 boolean 字段?

我避免了拦截器和其他东西来缩短代码。

最佳答案

您可以使用@FieldExpressionValidator来完成此操作。例如

@Validations(fieldExpressions = @FieldExpressionValidator(fieldName="boolField", expression="boolField == true", message="boolField should be set"))

关于java - 如何使用注释验证 Struts2 中的 boolean 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903044/

相关文章:

jquery - Struts2+Jquery+JSON - 一些麻烦

java - 在struts 2中上传视频

java - 有没有像 Observable ConcurrentLinkedDeque 这样的东西?

java - 如何在java中合并两个复杂的对象

java - Java中的条件运算符是从哪个版本开始的

ruby-on-rails-3 - 未知的验证器,即使它在验证器和回调的 Rails 指南中

c# - 验证上传的文件扩展名

android - 检查 Android strings.xml 变量的工具?

java - 隐藏显示:column from display:table struts and export in Excel

Java 从嵌套 JSON 中提取内容