【发布时间】:2015-03-18 07:07:06
【问题描述】:
在 Struts 2 中,我们可以开发@CustomValidator,它可以在应用程序范围内使用
@CustomValidator(type = "CustomerNumberValidator", fieldName = "customerNo")
为了验证更多,我们使用@ExpressionValidator
@ExpressionValidator(expression =
"( (!''.equals(account.firstName) && (!''.equals(account.lastName) )
|| (presonalAccount == false)",
key = "validate.account.name")
如果表达式过于复杂,需要处理更多个字段,我们使用OGNL来调用静态方法。静态方法将进行验证并返回一个 boolean 例如
@ExpressionValidator(expression = "@foo.bar.CalendarUtil@compareDates(fromDate,toDate)", key = "validate.date.before")
以上是自定义表达式验证器的一些方法!
我们在应用程序范围内使用@foo.bar.CalendarUtil@compareDates 为我们进行此验证。
还有其他方法可以让我们使用自定义范围验证器吗?!
是否有任何自定义表达式验证器可以添加到 Struts 中,我们可以像使用 @CustomValidator 一样在 Action 中调用它?
【问题讨论】:
标签: java validation struts2 struts-validation struts2-convention-plugin