StrutsPrepareAndExecuteFilter源码剖析

转自:

http://blog.csdn.net/izard999/article/details/40143439


在面试的时候,很多人经常会被问到:Struts2与Struts1的区别..我只想说, 最根本的区别是Struts2基于Filter,Struts1基于Servlet, 在Web容器中, Filter的优先级是高于Servlet的

那么上一篇文章中, 我给大家呈现那个官方的大图上面有个FilterDispatcher, 没错.  它就是Struts2的核心过滤器. 但是后来被遗弃

Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one

为什么遗弃我之前有所提到.  所以下面会研究StrutsPrepareAndExecuteFilter , 而不会再提及FilterDispatcher了.

在web.xml中我们指定Struts2的Filter为:

[html]  view plain  copy
  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>struts2</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8. </filter-mapping>  

从这里看得出来, Struts2官方起名字还是挺有意思的.. ng是什么.?   ng是next generation 的缩写, 下一代产品

下面正题开始, StrutsPrepareAndExecuteFilter源码剖析,  有三个属性:

[java]  view plain  copy
  1. protected PrepareOperations prepare; //准备的一些操作  
  2. protected ExecuteOperations execute; //执行的一些操作  
  3. protected List<Pattern> excludedPatterns; //排斥的一些匹配路径  

StrutsPrepareAndExecuteFilter与普通的Filter并无区别,方法除继承自Filter外,仅有一个回调方法,我们将按照Filter方法调用顺序,由init—>doFilter—>destroy顺序地分析源码。

1、init()

[java]  view plain  copy
  1. public void init(FilterConfig filterConfig) throws ServletException {  
  2.         InitOperations init = new InitOperations();  
  3.         Dispatcher dispatcher = null;  
  4.         try {  
  5. <span style="white-space:pre">    </span>    //封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中   
  6.             FilterHostConfig config = new FilterHostConfig(filterConfig);  
  7.             // 初始化struts内部日志    
  8.             init.initLogging(config);  
  9.         //创建dispatcher ,并初始化,这部分下面我们重点分析,初始化时加载哪些些资源   
  10.             dispatcher = init.initDispatcher(config);  
  11.         // 初始化静态资源(静态资源后面会有讲解)  
  12.             init.initStaticContentLoader(config, dispatcher);  
  13.   
  14.         //初始化类属性:prepare 、execute     
  15.             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);  
  16.             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);  
  17.         //创建排斥路径列表  
  18.             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);  
  19.         //回调空的postInit方法    
  20.             postInit(dispatcher, filterConfig);  
  21.         } finally {  
  22.             if (dispatcher != null) {  
  23.                 dispatcher.cleanUpAfterInit();  
  24.             }  
  25.             init.cleanup();  
  26.         }  
  27.     }  
首先看下FilterHostConfig ,源码如下:

[java]  view plain  copy
  1. /** 
  2.  * Host configuration that wraps FilterConfig 
  3.  */  
  4. public class FilterHostConfig implements HostConfig {  
  5.   
  6.     private FilterConfig config;  
  7.   
  8.     public FilterHostConfig(FilterConfig config) {  
  9.         this.config = config;  
  10.     }  
  11.     /**  
  12.      *  根据init-param配置的param-name获取param-value的值  
  13.      */     
  14.     public String getInitParameter(String key) {  
  15.         return config.getInitParameter(key);  
  16.     }  
  17.   
  18.     /**  
  19.      *  返回初始化参数名的List  
  20.      */     
  21.     public Iterator<String> getInitParameterNames() {  
  22.         return MakeIterator.convert(config.getInitParameterNames());  
  23.     }  
  24.   
  25.     public ServletContext getServletContext() {  
  26.         return config.getServletContext();  
  27.     }  
  28. }  
 只有短短的几行代码,getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。

重点来了,创建并初始化Dispatcher   
[java]  view plain  copy
  1. public Dispatcher initDispatcher( HostConfig filterConfig ) {  
  2.        Dispatcher dispatcher = createDispatcher(filterConfig);  
  3.        dispatcher.init();  
  4.        return dispatcher;  
  5.    }  

创建Dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根据ServletContext和参数Map构造Dispatcher :  
[java]  view plain  copy
  1. private Dispatcher createDispatcher( HostConfig filterConfig ) {  
  2.         Map<String, String> params = new HashMap<String, String>();  
  3.         for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {  
  4.             String name = (String) e.next();  
  5.             String value = filterConfig.getInitParameter(name);  
  6.             params.put(name, value);  
  7.         }  
  8.         return new Dispatcher(filterConfig.getServletContext(), params);  
  9.     }  
很多人问Struts2配置文件的加载顺序是什么? 不用死记硬背, 源码里面就有.
Dispatcher初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……
[java]  view plain  copy
  1. </pre><pre name="code" class="java">/** 
  2.  *初始化过程中依次加载如下配置文件 
  3.  */  
  4. public void init() {  
  5.   
  6.         if (configurationManager == null) {  
  7.             configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);  
  8.         }  
  9.   
  10.         try {  
  11.             //加载org/apache/struts2/default.properties  
  12.             init_DefaultProperties(); // [1]  
  13.             //加载struts-default.xml,struts-plugin.xml,struts.xml  
  14.             init_TraditionalXmlConfigurations(); // [2]  
  15.             init_LegacyStrutsProperties(); // [3]  
  16.             //用户自己实现的ConfigurationProviders类              
  17.             init_CustomConfigurationProviders(); // [5]  
  18.             //Filter的初始化参数  
  19.             init_FilterInitParameters() ; // [6]  
  20.             init_AliasStandardObjects() ; // [7]  
  21.   
  22.             Container container = init_PreloadConfiguration();  
  23.             container.inject(this);  
  24.             init_CheckConfigurationReloading(container);  
  25.             init_CheckWebLogicWorkaround(container);  
  26.   
  27.             if (!dispatcherListeners.isEmpty()) {  
  28.                 for (DispatcherListener l : dispatcherListeners) {  
  29.                     l.dispatcherInitialized(this);  
  30.                 }  
  31.             }  
  32.         } catch (Exception ex) {  
  33.             if (LOG.isErrorEnabled())  
  34.                 LOG.error("Dispatcher initialization failed", ex);  
  35.             throw new StrutsException(ex);  
  36.         }  
  37.     }  
初始化default.properties,具体的初始化操作在DefaultPropertiesProvider类中
[java]  view plain  copy
  1. private void init_DefaultProperties() {  
  2.        configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());  
  3.    }  
下面我们看下DefaultPropertiesProvider类源码:
[java]  view plain  copy
  1. public void register(ContainerBuilder builder, LocatableProperties props)  
  2.             throws ConfigurationException {  
  3.           
  4.         Settings defaultSettings = null;  
  5.         try {  
  6.             defaultSettings = new PropertiesSettings("org/apache/struts2/default");  
  7.         } catch (Exception e) {  
  8.             throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);  
  9.         }  
  10.           
  11.         loadSettings(props, defaultSettings);  
  12.     }  

其他的我们再次省略,大家可以浏览下各个初始化操作都加载了哪些文件
注意,很多人有问过我, Struts2的配置文件名可以不可以不叫struts.xml, 类似Struts1那种自己定义文件名.?  在此,我明确的告诉大家是不可以的.
因为 Dispatcher类中指定了个常量:
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
意思是, 我们的配置文件只能是这几个名字, Struts2才会识别, 否则不识别,  读配置的顺序就是这个常量的顺序去解读的.  下面附上加载这个配置的源码
[java]  view plain  copy
  1. private void init_TraditionalXmlConfigurations() {  
  2.         String configPaths = initParams.get("config");  
  3.         if (configPaths == null) {  
  4.             configPaths = DEFAULT_CONFIGURATION_PATHS;  
  5.         }  
  6.         String[] files = configPaths.split("\\s*[,]\\s*");  
  7.         for (String file : files) {  
  8.             if (file.endsWith(".xml")) {  
  9.                 if ("xwork.xml".equals(file)) {  
  10.                     configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));  
  11.                 } else {  
  12.                     configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));  
  13.                 }  
  14.             } else {  
  15.                 throw new IllegalArgumentException("Invalid configuration file name");  
  16.             }  
  17.         }  
  18.     }  
看到了吧?  循环, 所以如果几个配置文件里面有相同的常量名或者其他一些配置, 那么后面的会覆盖掉前面的

2、doFilter()
     doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,源码如下:
[java]  view plain  copy
  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
  2.         //父类向子类转:强转为http请求、响应  
  3.         HttpServletRequest request = (HttpServletRequest) req;  
  4.         HttpServletResponse response = (HttpServletResponse) res;  
  5.   
  6.         try {  
  7.             //设置编码和国际化  
  8.             prepare.setEncodingAndLocale(request, response);  
  9.              //创建Action上下文(重点)  
  10.             prepare.createActionContext(request, response);  
  11.         //把当前Dispatcher实例放入ThreadLocal里面  
  12.             prepare.assignDispatcherToThread();  
  13.             if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
  14.                 //如果当前路径是排斥的路径,那么此过滤器不拦截  
  15.                 chain.doFilter(request, response);  
  16.             } else {  
  17.                 //包装这个Request(如果是multipart/form-data,包装成MultiPartRequestWrapper, 否则包装成StrutsRequestWrapper)  
  18.                 request = prepare.wrapRequest(request);  
  19.                 //寻找Action的映射  
  20.                 ActionMapping mapping = prepare.findActionMapping(request, response, true);  
  21.                 if (mapping == null) {  
  22.                     //如果没有映射,则需要判断是否是静态资源  
  23.                     boolean handled = execute.executeStaticResourceRequest(request, response);  
  24.                     if (!handled) {  
  25.                         chain.doFilter(request, response);  
  26.                     }  
  27.                 } else {  
  28.                     execute.executeAction(request, response, mapping);  
  29.                 }  
  30.             }  
  31.         } finally {  
  32.             prepare.cleanupRequest(request);  
  33.         }  
  34.     }  
setEncodingAndLocale调用了dispatcher方法的prepare方法:
[java]  view plain  copy
  1. /** 
  2.  * Sets the request encoding and locale on the response 
  3.  */  
  4. public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {  
  5.     dispatcher.prepare(request, response);  
  6. }  

下面我们看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:
[java]  view plain  copy
  1. public void prepare(HttpServletRequest request, HttpServletResponse response) {  
  2.         String encoding = null;  
  3.         if (defaultEncoding != null) {  
  4.             encoding = defaultEncoding;  
  5.         }  
  6.   
  7.         Locale locale = null;  
  8.         if (defaultLocale != null) {  
  9.             locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());  
  10.         }  
  11.   
  12.         if (encoding != null) {  
  13.             try {  
  14.                 request.setCharacterEncoding(encoding);  
  15.             } catch (Exception e) {  
  16.                 LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);  
  17.             }  
  18.         }  
  19.   
  20.         if (locale != null) {  
  21.             response.setLocale(locale);  
  22.         }  
  23.   
  24.         if (paramsWorkaroundEnabled) {  
  25.             request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request  
  26.         }  
  27.     }  

ActionContext创建(重点中的重点)

ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象:
[java]  view plain  copy
  1. static ThreadLocal actionContext = new ThreadLocal();  
  2. Map<String, Object> context;  
下面我们看下如何创建action上下文的,代码如下:
[java]  view plain  copy
  1. /** 
  2. *创建Action上下文,初始化thread local 
  3. */  
  4. public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {  
  5.     ActionContext ctx;  
  6.     Integer counter = 1;  
  7.     Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);  
  8.     if (oldCounter != null) {  
  9.         counter = oldCounter + 1;  
  10.     }  
  11.     //注意此处是从ThreadLocal中获取此ActionContext变量  
  12.     ActionContext oldContext = ActionContext.getContext();  
  13.     if (oldContext != null) {  
  14.         // detected existing context, so we are probably in a forward  
  15.         ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));  
  16.     } else {  
  17.         //创建ValueStack(很重要的一个东东,后面会讲到)  
  18.         ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();  
  19.         stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));  
  20.         //stack.getContext()返回的是一个Map<String,Object>,根据此Map构造一个ActionContext  
  21.         ctx = new ActionContext(stack.getContext());  
  22.     }  
  23.     request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);  
  24.     //将ActionContext存如ThreadLocal  
  25.     ActionContext.setContext(ctx);  
  26.     return ctx;  
  27. }  
面试题常见问题:  为什么在Struts2中我们不用考虑线程安全? 为什么交给Spring管理的时候我们要把Bean的scope属性设置成prototype?
答: 从上述源码中我们看到了, 每一次被Struts2的这个过滤器拦截的时候, 都会调用createActionContext方法,  无论当前是否有ActionContext实例存在, 都会new一个ActionContext, 只不过是如果是action之间转发的请求,那么new的时候会把之前的那个ActionContext放进去..  而每个ActionContext都只与自己的ThreadLocal挂钩,所以是不用考虑线程安全的. 然而后面读源码也会读到, 每次请求都会产生一个Action的实例, 而Spring默认创建实例是单例的.

上面代码中dispatcher.createContextMap,如何封装相关参数:
[java]  view plain  copy
  1. public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,  
  2.             ActionMapping mapping, ServletContext context) {  
  3.   
  4.         // request map wrapping the http request objects  
  5.         Map requestMap = new RequestMap(request);  
  6.   
  7.         // parameters map wrapping the http parameters.  ActionMapping parameters are now handled and applied separately  
  8.         Map params = new HashMap(request.getParameterMap());  
  9.   
  10.         // session map wrapping the http session  
  11.         Map session = new SessionMap(request);  
  12.   
  13.         // application map wrapping the ServletContext  
  14.         Map application = new ApplicationMap(context);  
  15.     //requestMap、params、session等Map封装成为一个上下文Map,逐个调用了map.put(Map p).  
  16.         Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);  
  17.   
  18.         if (mapping != null) {  
  19.             extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);  
  20.         }  
  21.         return extraContext;  
  22. }  

我们简单看下RequestMap,其他的省略。RequestMap类实现了抽象Map,故其本身是一个Map,主要方法实现:
[java]  view plain  copy
  1. //map的get实现  
  2. public Object get(Object key) {  
  3.     return request.getAttribute(key.toString());  
  4. }  
  5. //map的put实现  
  6. public Object put(Object key, Object value) {  
  7.     Object oldValue = get(key);  
  8.     entries = null;  
  9.     request.setAttribute(key.toString(), value);  
  10.     return oldValue;  
  11. }  
下面是源码展示了如何执行Action控制器:
[java]  view plain  copy
  1. public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {  
  2.     dispatcher.serviceAction(request, response, servletContext, mapping);  
  3. }  
  4.   
  5.     public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,  
  6.                               ActionMapping mapping) throws ServletException {  
  7.     //封装执行的上下文环境,主要讲相关信息存储入map  
  8.         Map<String, Object> extraContext = createContextMap(request, response, mapping, context);  
  9.   
  10.         // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action  
  11.         ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);  
  12.         boolean nullStack = stack == null;  
  13.     //这里再次强调下ValueStack的重要性(Struts2是获取--->有就直接put,没有就新建一个再put)  
  14.         if (nullStack) {  
  15.             ActionContext ctx = ActionContext.getContext();  
  16.             if (ctx != null) {  
  17.                 stack = ctx.getValueStack();  
  18.             }  
  19.         }  
  20.         if (stack != null) {  
  21.             extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));  
  22.         }  
  23.   
  24.         String timerKey = "Handling request from Dispatcher";  
  25.         try {  
  26.             UtilTimerStack.push(timerKey);  
  27.             //获取命名空间  
  28.             String namespace = mapping.getNamespace();  
  29.             //获取action配置的name属性  
  30.             String name = mapping.getName();  
  31.             //获取action配置的method属性  
  32.             String method = mapping.getMethod();  
  33.   
  34.             Configuration config = configurationManager.getConfiguration();  
  35.             //根据执行上下文参数,命名空间,名称等创建用户自定义Action的代理对象  
  36.             ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(  
  37.                     namespace, name, method, extraContext, truefalse);  
  38.   
  39.             request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());  
  40.   
  41.             // if the ActionMapping says to go straight to a result, do it!  
  42.                     //执行execute方法,并转向结果  
  43.             if (mapping.getResult() != null) {  
  44.                 Result result = mapping.getResult();  
  45.                 result.execute(proxy.getInvocation());  
  46.             } else {  
  47.                 proxy.execute();  
  48.             }  
  49.   
  50.             // If there was a previous value stack then set it back onto the request  
  51.             if (!nullStack) {  
  52.                 request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);  
  53.             }  
  54.         } catch (ConfigurationException e) {  
  55.             // WW-2874 Only log error if in devMode  
  56.             if(devMode) {  
  57.                 String reqStr = request.getRequestURI();  
  58.                 if (request.getQueryString() != null) {  
  59.                     reqStr = reqStr + "?" + request.getQueryString();  
  60.                 }  
  61.                 LOG.error("Could not find action or result\n" + reqStr, e);  
  62.             }  
  63.             else {  
  64.                 LOG.warn("Could not find action or result", e);  
  65.             }  
  66.             sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);  
  67.         } catch (Exception e) {  
  68.             sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);  
  69.         } finally {  
  70.             UtilTimerStack.pop(timerKey);  
  71.         }  
  72.     }  
也许有人会有疑问了,  框架是如何解析Struts.xml的?如何将URL与action映射匹配?
上文有提到struts配置文件的加载顺序, 再次拿出Dispatcher.init_TraditionalXmlConfigurations()方法源码
[java]  view plain  copy
  1. private void init_TraditionalXmlConfigurations() {  
  2.         String configPaths = initParams.get("config");  
  3.         if (configPaths == null) {  
  4.             configPaths = DEFAULT_CONFIGURATION_PATHS;  
  5.         }  
  6.         String[] files = configPaths.split("\\s*[,]\\s*");  
  7.         for (String file : files) {  
  8.             if (file.endsWith(".xml")) {  
  9.                 if ("xwork.xml".equals(file)) {  
  10.                     configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));  
  11.                 } else {  
  12.                     configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));  
  13.                 }  
  14.             } else {  
  15.                 throw new IllegalArgumentException("Invalid configuration file name");  
  16.             }  
  17.         }  
  18.     }  
请大家注意两个地方  createXmlConfigurationProvider和createStrutsXmlConfigurationProvider.  根据经验判断我要去读两个类的源码

XmlConfigurationProvider和StrutsXmlConfigurationProvider,  从源码中我看到
public class StrutsXmlConfigurationProvider extends XmlConfigurationProvider
是继承的关系, 所以我们先研究StrutsXmlConfigurationProvider
我看到了register方法
[java]  view plain  copy
  1. public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {  
  2.         if (servletContext != null && !containerBuilder.contains(ServletContext.class)) {  
  3.             containerBuilder.factory(ServletContext.classnew Factory<ServletContext>() {  
  4.                 public ServletContext create(Context context) throws Exception {  
  5.                     return servletContext;  
  6.                 }  
  7.             });  
  8.         }  
  9.         super.register(containerBuilder, props);  
  10.     }  

这个里面调用了父类的

[java]  view plain  copy
  1. public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {  
  2.         if (LOG.isInfoEnabled()) {  
  3.             LOG.info("Parsing configuration file [" + configFileName + "]");  
  4.         }  
  5.         Map<String, Node> loadedBeans = new HashMap<String, Node>();  
  6.         for (Document doc : documents) {  
  7.             Element rootElement = doc.getDocumentElement();  
  8.             NodeList children = rootElement.getChildNodes();  
  9.             int childSize = children.getLength();  
  10.   
  11.             for (int i = 0; i < childSize; i++) {  
  12.                 Node childNode = children.item(i);  
  13.   
  14.                 if (childNode instanceof Element) {  
  15.                     Element child = (Element) childNode;  
  16.   
  17.                     final String nodeName = child.getNodeName();  
  18.                     if ("bean".equals(nodeName)) {  
  19.                         //获取bean  
  20.                         String type = child.getAttribute("type");  
  21.                         String name = child.getAttribute("name");  
  22.                         String impl = child.getAttribute("class");  
  23.                         String onlyStatic = child.getAttribute("static");  
  24.                         String scopeStr = child.getAttribute("scope");  
  25.                         boolean optional = "true".equals(child.getAttribute("optional"));  
  26.                         Scope scope = Scope.SINGLETON;  
  27.                         if ("default".equals(scopeStr)) {  
  28.                             scope = Scope.DEFAULT;  
  29.                         } else if ("request".equals(scopeStr)) {  
  30.                             scope = Scope.REQUEST;  
  31.                         } else if ("session".equals(scopeStr)) {  
  32.                             scope = Scope.SESSION;  
  33.                         } else if ("singleton".equals(scopeStr)) {  
  34.                             scope = Scope.SINGLETON;  
  35.                         } else if ("thread".equals(scopeStr)) {  
  36.                             scope = Scope.THREAD;  
  37.                         }  
  38.   
  39.                         if (StringUtils.isEmpty(name)) {  
  40.                             name = Container.DEFAULT_NAME;  
  41.                         }  
  42.   
  43.                         try {  
  44.                             Class cimpl = ClassLoaderUtil.loadClass(impl, getClass());  
  45.                             Class ctype = cimpl;  
  46.                             if (StringUtils.isNotEmpty(type)) {  
  47.                                 ctype = ClassLoaderUtil.loadClass(type, getClass());  
  48.                             }  
  49.                             if ("true".equals(onlyStatic)) {  
  50.                                 // Force loading of class to detect no class def found exceptions  
  51.                                 cimpl.getDeclaredClasses();  
  52.                                 containerBuilder.injectStatics(cimpl);  
  53.                             } else {  
  54.                                 if (containerBuilder.contains(ctype, name)) {  
  55.                                     Location loc = LocationUtils.getLocation(loadedBeans.get(ctype.getName() + name));  
  56.                                     if (throwExceptionOnDuplicateBeans) {  
  57.                                         throw new ConfigurationException("Bean type " + ctype + " with the name " +  
  58.                                                 name + " has already been loaded by " + loc, child);  
  59.                                     }  
  60.                                 }  
  61.   
  62.                                 // Force loading of class to detect no class def found exceptions  
  63.                                 cimpl.getDeclaredConstructors();  
  64.   
  65.                                 if (LOG.isDebugEnabled()) {  
  66.                                     LOG.debug("Loaded type:" + type + " name:" + name + " impl:" + impl);  
  67.                                 }  
  68.                                 containerBuilder.factory(ctype, name, new LocatableFactory(name, ctype, cimpl, scope, childNode), scope);  
  69.                             }  
  70.                             loadedBeans.put(ctype.getName() + name, child);  
  71.                         } catch (Throwable ex) {  
  72.                             if (!optional) {  
  73.                                 throw new ConfigurationException("Unable to load bean: type:" + type + " class:" + impl, ex, childNode);  
  74.                             } else {  
  75.                                 if (LOG.isDebugEnabled()) {  
  76.                                     LOG.debug("Unable to load optional class: #0", impl);  
  77.                                 }  
  78.                             }  
  79.                         }  
  80.                     } else if ("constant".equals(nodeName)) {  
  81.                 //获取常量  
  82.                         String name = child.getAttribute("name");  
  83.                         String value = child.getAttribute("value");  
  84.                         props.setProperty(name, value, childNode);  
  85.                     } else if (nodeName.equals("unknown-handler-stack")) {  
  86.                         List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<UnknownHandlerConfig>();  
  87.                         NodeList unknownHandlers = child.getElementsByTagName("unknown-handler-ref");  
  88.                         int unknownHandlersSize = unknownHandlers.getLength();  
  89.   
  90.                         for (int k = 0; k < unknownHandlersSize; k++) {  
  91.                             Element unknownHandler = (Element) unknownHandlers.item(k);  
  92.                             unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler.getAttribute("name")));  
  93.                         }  
  94.   
  95.                         if (!unknownHandlerStack.isEmpty())  
  96.                             configuration.setUnknownHandlerStack(unknownHandlerStack);  
  97.                     }  
  98.                 }  
  99.             }  
  100.         }  
  101.     }  

父类的register主要是解析bean标签和常量标签的, XmlConfigurationProvider类里面有一系列的load方法.就是解析struts.xml等配置文件的package信息,action信息.这里就不累赘的贴代码了. 大家自己下去看看就行了! 关键的我带着大家看看addAction方法
[java]  view plain  copy
  1.   protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {  
  2.        String name = actionElement.getAttribute("name");//获取name属性  
  3.        String className = actionElement.getAttribute("class");//获取class属性  
  4.        String methodName = actionElement.getAttribute("method");//获取method属性  
  5.        Location location = DomHelper.getLocationObject(actionElement);//获取location  
  6.   
  7.        if (location == null) {  
  8.            LOG.warn("location null for " + className);  
  9.        }  
  10.        //methodName should be null if it's not set  
  11.        methodName = (methodName.trim().length() > 0) ? methodName.trim() : null;  
  12.   
  13.        // if there isnt a class name specified for an <action/> then try to  
  14.        // use the default-class-ref from the <package/>  
  15. //为什么不配class默认是ActionSupport.?这里一目了然  
  16.        if (StringUtils.isEmpty(className)) {  
  17.            // if there is a package default-class-ref use that, otherwise use action support  
  18.           /* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) { 
  19.                className = packageContext.getDefaultClassRef(); 
  20.            } else { 
  21.                className = ActionSupport.class.getName(); 
  22.            }*/  
  23.   
  24.        } else {  
  25.            if (!verifyAction(className, name, location)) {  
  26.                if (LOG.isErrorEnabled())  
  27.                    LOG.error("Unable to verify action [#0] with class [#1], from [#2]", name, className, location.toString());  
  28.                return;  
  29.            }  
  30.        }  
  31.   
  32.   
  33.   
  34.        Map<String, ResultConfig> results;  
  35.        try {  
  36.            results = buildResults(actionElement, packageContext);  
  37.        } catch (ConfigurationException e) {  
  38.            throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);  
  39.        }  
  40.        //拦截器列表  
  41.        List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);  
  42.        //异常映射列表  
  43.        List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);  
  44.        ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className)  
  45.                .methodName(methodName)  
  46.                .addResultConfigs(results)  
  47.                .addInterceptors(interceptorList)  
  48.                .addExceptionMappings(exceptionMappings)  
  49.                .addParams(XmlHelper.getParams(actionElement))  
  50.                .location(location)  
  51.                .build();  
  52.        packageContext.addActionConfig(name, actionConfig);  
  53.   
  54.        if (LOG.isDebugEnabled()) {  
  55.            LOG.debug("Loaded " + (StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "") + name + " in '" + packageContext.getName() + "' package:" + actionConfig);  
  56.        }  
  57.    }  
strtus2-core-2.3.16.3.jar中看到struts-2.3.dtd
[java]  view plain  copy
  1. <!ELEMENT action ((param|result|interceptor-ref|exception-mapping)*,allowed-methods?)>  
  2. <!ATTLIST action  
  3.     name CDATA #REQUIRED  
  4.     class CDATA #IMPLIED  
  5.     method CDATA #IMPLIED  
  6.     converter CDATA #IMPLIED  
  7. >  

到此为止, Struts2核心源码解读工作已经基本OK了.大家也大致了解了Struts2的工作原理.  后面有关运用Struts2做程序的时候,还会根据实际情况解读相关的源码的.敬请期待.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值