您的位置:首页 > 编程语言 > Java开发

Java Web(9)struts 2 validation result设置

2015-12-30 17:01 483 查看
1. 使用action.validate后,如果有addFieldError后,struts会自动返回一个result为“input”,在官方的文档中是这样子的解释的Validation

Validation also depends on both the validation and workflow interceptors (both are included in the default interceptor stack). The validation interceptor does the validation itself and creates a list of field-specific errors. Theworkflow interceptor checks for the presence of validation errors: if any are found, it returns the "input" result (by default), taking the user back to the form which contained the validation errors.


2. 如果想设定另外一个resultName的话,可我们可以看一下struts是如何设定的,其中执行这一部分功能的拦截器是 DefaultWorkflowInterceptor 其中关键代码如下

protected String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
ValidationAware validationAwareAction = (ValidationAware) action;
if (validationAwareAction.hasErrors()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Errors on action " + validationAwareAction + ", returning result name 'input'");
}
String resultName = inputResultName;
//如何校验的当前action有实现这个类,
//则可以使用其接口的方法getInputResultName()来返回指定的resultName
if (action instanceof ValidationWorkflowAware) {
resultName = ((ValidationWorkflowAware) action).getInputResultName();
}
//使用注解来指定
InputConfig annotation = action.getClass().getMethod(invocation.getProxy().getMethod(), EMPTY_CLASS_ARRAY).getAnnotation(InputConfig.class);
if (annotation != null) {
if (!annotation.methodName().equals("")) {
Method method = action.getClass().getMethod(annotation.methodName());
resultName = (String) method.invoke(action);
} else {
resultName = annotation.resultName();
}
}
return resultName;
}
}
return invocation.invoke();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: