在Struts 2中重用自定义表达式验证器 [英] Reusing Custom Expression Validator in Struts 2

查看:35
本文介绍了在Struts 2中重用自定义表达式验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Struts 2中,我们可以开发 @CustomValidator ,可以在整个应用程序中使用

In Struts 2 we can develop @CustomValidator which can be used in application wide

@CustomValidator(type = "CustomerNumberValidator", fieldName = "customerNo")

对于验证,除了一个字段以外,我们使用 @ExpressionValidator

For validation more than one field we use @ExpressionValidator

@ExpressionValidator(expression = 
"( (!''.equals(account.firstName) && (!''.equals(account.lastName) )
   || (presonalAccount == false)", 
   key = "validate.account.name")

如果表达式过于复杂并且需要在一个字段上进行更多操作,则可以使用OGNL调用静态方法.静态方法将进行验证,并返回一个 boolean 例如

If the expression is too complicated and needs to work on more than one field we use OGNL to call the static method. The static method will do the validation and return a boolean for example

@ExpressionValidator(expression = "@foo.bar.CalendarUtil@compareDates(fromDate,toDate)", key = "validate.date.before")

上面是自定义表达式验证器的一些方式!

Above is some how a Custom Expression Validator !

我们在应用程序范围内使用 @ foo.bar.CalendarUtil @ compareDates 为我们进行此验证.

And we use @foo.bar.CalendarUtil@compareDates in application wide to make this validation for us.

还有另一种方法可以使我们使用自定义宽幅验证器吗?!

Is there another approach which enables us to use a custom wide validator?!

是否可以将任何自定义表达式验证器添加到Struts,并且可以使用 @CustomValidator 的方式在 Action 中调用它?

Is there any custom expression validator which can be added to Struts and we can call it in Action in the way we use @CustomValidator?

推荐答案

创建自定义验证器(与字段无关):

Create a Custom Validator (not field related):

public final class CompareDatesValidator extends ValidatorSupport {
    private String fromDate; // getter and setter
    private String toDate;   // getter and setter    

    @Override
    public void validate(Object o) throws ValidationException {
        Date d1 = (Date)parse(fromDate, Date.class);
        Date d2 = (Date)parse(toDate, Date.class);

        if (d1==null || d2==null || d2.before(d1)){
            addActionError(getDefaultMessage());
        }
    }
}

validators.xml 文件中注册自定义验证器:

Register the custom validator in validators.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
     "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
     "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="compareDatesValidator" 
              class="org.foo.bar.CompareDatesValidator"/>
</validators>

在操作中使用验证器:

private Date startDate; // getter and setter
private Date endDate;   // getter and setter

@Validations(
    customValidators={
        @CustomValidator(type="compareDatesValidator", 
            message="Dates provided are not valid."
            parameters={
                @ValidationParameter(name="fromDate", value="${startDate}"), 
                @ValidationParameter(name="toDate",   value="${endDate}")})})
public String execute(){
    return SUCCESS;
}

这篇关于在Struts 2中重用自定义表达式验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆