struts2 校验器概述

来源:互联网 发布:网络层ddos 编辑:程序博客网 时间:2024/04/25 00:35
在Struts2应用中,我们发现,如果通过验证框架验证录入后,如果出现错误,应用会自动回到录入页面,这是为什么?在Struts2中, Action、Interceptor 需要返回String类型的result,框架才能通过配置好的转向来决定下一个视图是哪个页面。那么验证框架是怎么做到的呢?源代码分析:第一、通过struts2-core-2.0.11.jar中的struts-default.xml文件我们可以看到程序代码<package name="struts-default" abstract="true"><interceptor-stack name="defaultStack">...<interceptor-ref name="validation"/> 验证框架Interceptor<interceptor-ref name="workflow"/> 工作流Interceptor</interceptor-stack>此处的指出的两个Interceptor是和我们讨论的问题有关系的两个Interceptor。程序代码<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/><interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>由此我们知道了需要看的源代码的路径。第二、分析AnnotationValidationInterceptor:AnnotationValidationInterceptor继承自ValidationInterceptor(package com.opensymphony.xwork2.validator)在AnnotationValidationInterceptor中的doIntercept方法中我们看到程序代码protected String doIntercept(ActionInvocation invocation) throws Exception {                 Object action = invocation.getAction();     //此处框架只是用了Annotation的方式来查看,现在触发Action方式是否是不需要验证的,如果不需要就直接触发     //否则使用父类的方法         if (action != null) {             Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod());             SkipValidation skip = (SkipValidation) method.getAnnotation(SkipValidation.class);             if (skip != null) {                 return invocation.invoke();//直接触发             }         }         return super.doIntercept(invocation);//使用父类的方法}那么我们继续来看父类的方法在ValidationInterceptor中程序代码protected String doIntercept(ActionInvocation invocation) throws Exception {         doBeforeInvocation(invocation);                 return invocation.invoke();}此处有个核心方法doBeforeInvocation,注意是在触发Action之前执行的方法,这也是为什么验证框架不通过就进不了Action方法的原因所在。程序代码     protected void doBeforeInvocation(ActionInvocation invocation) throws Exception {         Object action = invocation.getAction();         String context = invocation.getProxy().getActionName();         String method = invocation.getProxy().getMethod();         if (log.isDebugEnabled()) {             log.debug("Validating "                     + invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName() + " with method "+ method +".");         }     //此处是初始化AnnotationActionValidatorManager类(静态初始化)     //通过这个类的validate方法来验证的     //此方法不具体解释,主要是根据已经配置好的验证规则(XML)来验证,如果出现错误就会addFieldError         if (validateAnnotatedMethodOnly) {             ActionValidatorManagerFactory.getInstance().validate(action, context, method);         } else {             ActionValidatorManagerFactory.getInstance().validate(action, context);         }     }第三、     好,到此为止我们已经知道验证器是如何工作的,如何添加验证错误的了,那么回到我们的问题,Struts2是怎么回到原有页面的?我们还有一个类没有解释,就是DefaultWorkflowInterceptor,来看代码:程序代码     public class DefaultWorkflowInterceptor extends MethodFilterInterceptor {     ...         private String inputResultName = Action.INPUT;//"input"     ...         protected String doIntercept(ActionInvocation invocation) throws Exception {         Object action = invocation.getAction();         if (action instanceof Validateable) {             //这里会触发Action中validate和validateDo开头的方法,来进行验证         ...         }                 if (action instanceof ValidationAware) {             ValidationAware validationAwareAction = (ValidationAware) action;             if (validationAwareAction.hasErrors()) {             //如果之前addFieldError后,这里就会捕捉到                 if (_log.isDebugEnabled()) {                     _log.debug("Errors on action "+validationAwareAction+", returning result name 'input'");                 }                 return inputResultName;//关键在此处             }         }         return invocation.invoke();     }     }
0 0
原创粉丝点击