1

给定一个Boolean动作类中的字段,像这样。

@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>,也可以通过查询字符串参数设置。

这个问题和这个问题使用 XML 配置进行布尔字段验证,但没有说明任何关于注释的内容。

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

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

4

1 回答 1

2

您可以使用@FieldExpressionValidator. 例如

@Validations(fieldExpressions = @FieldExpressionValidator(fieldName="boolField", expression="boolField == true", message="boolField should be set"))
于 2014-01-03T13:40:21.960 回答