Can I use Calendar in a Struts2 Validator Annotation?
Is it possible to use the java.util.Calendar in a Struts2 IntRangeFieldValidator?
The following code does not produce any OGNL errors but does not work either. I'm trying to create a validation rule for a year range, min= 1970 and max= current calendar year.
@RequiredFieldValidator(message="Year cannot be blank")
@IntRangeFieldValidator(message="Year must be greater than 1970", min="1970", max="ja开发者_运维技巧va.util.Calendar.getInstance().get(java.util.Calendar.YEAR)", fieldName="year")
public Integer getYear() {
return year;
}
Is there a better way to do this without writing a custom validator? Any/All replies are appreciated. Thanks in advance!
I think it should be:
@java.util.Calendar@getInstance().get(@java.util.Calendar@YEAR)
To be confirmed.
Are you able to use the DateRangeFieldValidator?
http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/validator/annotations/DateRangeFieldValidator.html
Here's a list of additional validators:
http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/validator/annotations/package-summary.html
Try using @FieldExpressionValidator in such a manner:
@FieldExpressionValidator(fieldName = "year", message="Year must be greater than 1970",
expression = "year < @java.util.Calendar@getInstance().get(@java.util.Calendar@YEAR)") })
精彩评论