Java Web(9)struts 2 validation result设置
2015-12-30 17:01
543 查看
1. 使用action.validate后,如果有addFieldError后,struts会自动返回一个result为“input”,在官方的文档中是这样子的解释的Validation
2. 如果想设定另外一个resultName的话,可我们可以看一下struts是如何设定的,其中执行这一部分功能的拦截器是 DefaultWorkflowInterceptor 其中关键代码如下
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(); }
相关文章推荐
- JVM(Java虚拟机)优化大全和案例实战
- java内存模型的三个代
- Java 泛型通配符?解惑
- Java总结篇系列:Java泛型
- spring 与 mybatis整合,mybatis注解不起作用
- AIDL中使用java类
- CSDN博客文章之Java资源
- 关于 "java中常量定义在interface中好还是定义在class中好" 的一些思考
- JAVA常见问题总结
- Spring各jar包的作用(转载)
- Java设计模式_行为型_观察者模式_任意行为的监听
- Java中判断String不为空的问题
- Java类集学习(五)集合的输出
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
- ubuntu启动eclipse时出错cannot open display
- jsonp和java示例
- 希望能结识更多热爱Java的小伙伴
- Java 1.7 语法新特性
- java集合:线性表
- eclipse重新设置workspace路径