天天看点

JFinal validator一个校验类对应多个验证的方法

需求场景:

一个表单提交过程中对应多个验证方法,为了不建立多个validatorXXX.class,如何将每个方法前的校验类具体到方法?

解决办法:

public class ControllerXXX extends Controller {
@Before(ValidatorXXX.class)
public method1(){}

@Before(ValidatorXXX.class)
public method2(){}
}

public class ValidatorXXX extends Validator {

protected void validate(Controller controller) {
String methodName = getActionMethod().getName();
if (methodName.equals(“method1”)) {
//method1的验证
} else if (methodName.equals(“method2”)) {
//method2的验证
}

}

protected void handleError(Controller controller) {
String methodName = getActionMethod().getName();
if (methodName.equals(“method1”)) {
//method1的处理
} else if (methodName.equals(“method2”)) {
//method2的处理
}
}
}
           

版权声明:本文为CSDN博主「weixin_33858485」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_33858485/article/details/91725793