Java 如何使用注释验证Struts2中的布尔字段?

Java 如何使用注释验证Struts2中的布尔字段?,java,validation,struts2,ognl,struts2-convention-plugin,Java,Validation,Struts2,Ognl,Struts2 Convention Plugin,在像这样的action类中给定一个布尔字段 @Namespace("/admin_side") @ResultPath("/WEB-INF/content") @ParentPackage(value="struts-default") public final class TestAction extends ActionSupport implements Serializable, ValidationAware, ModelDriven<Entity> { priva

在像这样的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.
}
@名称空间(“/admin\u-side”)
@结果路径(“/WEB-INF/content”)
@ParentPackage(value=“struts default”)
公共最终类TestAction扩展了ActionSupport,实现了可序列化、验证软件和模型驱动
{
私有布尔布尔场;
公共布尔getBoolField()
{
返回布尔菲尔德;
}
公共布尔setBoolField(布尔布尔字段)
{
这个.boolField=boolField;
}
@验证(requiredFields={@RequiredFieldValidator(fieldName=“boolField”,type=ValidatorType.FIELD,key=“delete.row.confirm”))
@动作(value=“testAction”,
结果={
@结果(name=ActionSupport.SUCCESS,type=“redirectAction”,location=“Test.action”),
@结果(name=ActionSupport.INPUT,location=“Test.jsp”)},
公共字符串testAction()
{            
返回ActionSupport.SUCCESS;
}
//动作课的其他部分。
}
操作类
布尔字段
中的字段只有在设置为
true
时才应进行验证。它可能是一个隐藏字段
,也可能是通过查询字符串参数设置的

问题使用XML配置进行布尔字段验证,但没有说明任何注释

如何使用注释验证此类布尔字段


我已经避免使用拦截器和其他东西来缩短代码。

您可以使用
@FieldExpressionValidator
来实现这一点

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

当值设置为
true
时,它会验证
布尔类型字段。但当动作类中的
布尔类型字段设置为
false
时,它会导致引发事件,尽管该字段也有一个
@RequiredFieldValidator
。这是因为该值不是
null
e更新。哦,是的!我确实没有注意到。愚蠢的我:)。