给定 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>
或者可以通过查询字符串参数设置。
This和 this问题使用 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/