需求场景: 一个表单提交过程中对应多个验证方法,为了不建立多个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的处理
}
}
}
|