struts2请求过程源代码分析

struts2请求过程源代码分析

  Struts2是Struts社区和WebWork社区的共同成果。我们甚至能够说,Struts2是WebWork的升级版。他採用的正是WebWork的核心,所以。Struts2并非一个不成熟的产品,相反。构建在WebWork基础之上的Struts2是一个执行稳定、性能优异、设计成熟的WEB框架。

  我这里的struts2源代码是从官网下载的一个最新的struts-2.3.15.1-src.zip。将其解压就可以。

里面的文件夹页文件很的多,我们仅仅须要定位到struts-2.3.15.1srccoresrcmainjavaorgapachestruts2查看源文件。

文件夹结构例如以下图

  Struts2框架的正常执行。除了占核心地位的xwork的支持以外。Struts2本身也提供了很多类。这些类被分门别类组织到不同的包中。从源码中发现,基本上每个Struts2类都訪问了WebWork提供的功能。从而也能够看出Struts2与WebWork千丝万缕的联系。但不管怎样,Struts2的核心功能比方将请求托付给哪个Action处理都是由xwork完毕的,Struts2仅仅是在WebWork的基础上做了适当的简化、加强和封装,并少量保留Struts1.x中的习惯。

下面是包说明:

org.apache.struts2. components

该包封装视图组件,Struts2在视图组件上有了非常大加强,不仅添加了组件的属性个数,更新增了几个非常实用的组件,如updownselect、doubleselect、datetimepicker、token、tree等。

 另外,Struts2可视化视图组件開始支持主题(theme),缺省情况下。使用自带的缺省主题。假设要自己定义页面效果,须要将组件的theme属性设置为simple。

org.apache.struts2. config 该包定义与配置相关的接口和类。实际上。project中的xml和properties文件的读取和解析都是由WebWork完毕的,Struts仅仅做了少量的工作。
org.apache.struts2.dispatcher Struts2的核心包,最重要的类都放在该包中。
org.apache.struts2.impl 该包仅仅定义了3个类。他们是StrutsActionProxy、StrutsActionProxyFactory、StrutsObjectFactory,这三个类都是对xwork的扩展。
org.apache.struts2.interceptor 定义内置的截拦器。
org.apache.struts2.servlet 用HttpServletRequest相关方法实现principalproxy接口。

org.apache.struts2.util 有用包。
org.apache.struts2.views 提供freemarker、jsp、velocity等不同类型的页面呈现。

 

 根文件夹下的5个文件说明:

StrutsStatics Struts常数。常数能够用来获取或设置对象从行动中或其它集合。
RequestUtils 请求处理程序类。此类仅仅有一个方法getServletPath。作用检索当前请求的servlet路径
ServletActionContext 站点的特定的上下文信息
StrutsConstants 该类提供了框架配置键的中心位置用于存储和检索配置设置。

StrutsException 通用执行时异常类

 

   

 

 

 

struts2 架构图例如以下图所看到的:

按照上图,我们能够看出一个请求在struts的处理大概有例如以下步骤:

  1、client初始化一个指向Servlet容器(比如Tomcat)的请求;

  2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器。这个过滤器对于Struts2和其它框架的集成非常有帮助。比如:SiteMesh Plugin);

  3、接着StrutsPrepareAndExecuteFilter被调用。StrutsPrepareAndExecuteFilter询问ActionMapper来决定这个请求是否须要调用某个Action。

  4、假设ActionMapper决定须要调用某个Action。FilterDispatcher把请求的处理交给ActionProxy;

  5、ActionProxy通过Configuration Manager询问框架的配置文件。找到须要调用的Action类;

  6、ActionProxy创建一个ActionInvocation的实例。

  7、ActionInvocation实例使用命名模式来调用。在调用Action的过程前后。涉及到相关拦截器(Intercepter)的调用。

  8、一旦Action运行完成。ActionInvocation负责依据struts.xml中的配置找到相应的返回结果。返回结果一般是(但不总是,也可能是另外的一个Action链)一个须要被表示的JSP或者FreeMarker的模版。在表示的过程中能够使用Struts2 框架中继承的标签。在这个过程中须要涉及到ActionMapper。

 

strut2源代码分析:

  首先我们使用struts2框架都会在web.xml中注冊和映射struts2。配置内容例如以下:

复制代码
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中。都是使用FilterDispathcer。从Struts 2.1.3開始,它已不推荐使用。假设你使用的Struts的版本号 >= 2.1.3,推荐升级到新的Filter,StrutsPrepareAndExecuteFilter。

在此研究的是StrutsPrepareAndExecuteFilter。

  StrutsPrepareAndExecuteFilter中的方法:

void init(FilterConfig filterConfig)  继承自Filter,过滤器的初始化
doFilter(ServletRequest req, ServletResponse res, FilterChain chain)  继承自Filter。运行过滤器
void destroy() 继承自Filter,用于资源释放
void postInit(Dispatcher dispatcher, FilterConfig filterConfig)  Callback for post initialization(一个空的方法,用于方法回调初始化)

 web容器一启动,就会初始化核心过滤器StrutsPrepareAndExecuteFilter。并运行初始化方法,初始化方法例如以下:

复制代码
 1 public void init(FilterConfig filterConfig) throws ServletException {
 2         InitOperations init = new InitOperations();
 3         Dispatcher dispatcher = null;
 4         try {
 5             //封装filterConfig。当中有个主要方法getInitParameterNames将參数名字以String格式存储在List中
 6             FilterHostConfig config = new FilterHostConfig(filterConfig);
 7             //初始化struts内部日志
 8             init.initLogging(config);
 9             //创建dispatcher ,并初始化
10             dispatcher = init.initDispatcher(config);
11             init.initStaticContentLoader(config, dispatcher);
12             //初始化类属性:prepare 、execute
13             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
14             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
15             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
16             //回调空的postInit方法
17             postInit(dispatcher, filterConfig);
18         } finally {
19             if (dispatcher != null) {
20                 dispatcher.cleanUpAfterInit();
21             }
22             init.cleanup();
23         }
24     }
复制代码

 关于封装filterConfig,首先看下FilterHostConfig ,源代码例如以下:

复制代码
 1 public class FilterHostConfig implements HostConfig {
 2 
 3     private FilterConfig config;
 4     //构造方法
 5     public FilterHostConfig(FilterConfig config) {
 6         this.config = config;
 7     }
 8     //依据init-param配置的param-name获取param-value的值  
 9     public String getInitParameter(String key) {
10         return config.getInitParameter(key);
11     }
12     //返回初始化參数名的迭代器 
13     public Iterator<String> getInitParameterNames() {
14         return MakeIterator.convert(config.getInitParameterNames());
15     }
16     //返回Servlet上下文
17     public ServletContext getServletContext() {
18         return config.getServletContext();
19     }
20 }
复制代码

  仅仅有短短的几行代码,getInitParameterNames是这个类的核心,将Filter初始化參数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。

  接下来,看下StrutsPrepareAndExecuteFilter中init方法中dispatcher = init.initDispatcher(config);这是初始化dispatcher的,是通过init对象的initDispatcher方法来初始化的,init是InitOperations类的对象,我们看看InitOperations中initDispatcher方法:

1  public Dispatcher initDispatcher( HostConfig filterConfig ) {
2         Dispatcher dispatcher = createDispatcher(filterConfig);
3         dispatcher.init();
4         return dispatcher;
5     }

  创建Dispatcher。会读取 filterConfig 中的配置信息,将配置信息解析出来。封装成为一个Map,然后根绝servlet上下文和參数Map构造Dispatcher :

复制代码
 1 private Dispatcher createDispatcher( HostConfig filterConfig ) {
 2         //存放參数的Map
 3         Map<String, String> params = new HashMap<String, String>();
 4         //将參数存放到Map
 5         for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
 6             String name = (String) e.next();
 7             String value = filterConfig.getInitParameter(name);
 8             params.put(name, value);
 9         }
10         //依据servlet上下文和參数Map构造Dispatcher 
11         return new Dispatcher(filterConfig.getServletContext(), params);
12     }
复制代码

  这样dispatcher对象创建完毕,接着就是dispatcher对象的初始化,打开Dispatcher类。看到它的init方法例如以下:

复制代码
 1 public void init() {
 2 
 3         if (configurationManager == null) {
 4             configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
 5         }
 6 
 7         try {
 8             init_FileManager();
 9             //载入org/apache/struts2/default.properties
10             init_DefaultProperties(); // [1]
11             //载入struts-default.xml,struts-plugin.xml,struts.xml
12             init_TraditionalXmlConfigurations(); // [2]
13             init_LegacyStrutsProperties(); // [3]
14             //用户自己实现的ConfigurationProviders类 
15             init_CustomConfigurationProviders(); // [5]
16             //Filter的初始化參数 
17             init_FilterInitParameters() ; // [6]
18             init_AliasStandardObjects() ; // [7]
19 
20             Container container = init_PreloadConfiguration();
21             container.inject(this);
22             init_CheckWebLogicWorkaround(container);
23 
24             if (!dispatcherListeners.isEmpty()) {
25                 for (DispatcherListener l : dispatcherListeners) {
26                     l.dispatcherInitialized(this);
27                 }
28             }
29         } catch (Exception ex) {
30             if (LOG.isErrorEnabled())
31                 LOG.error("Dispatcher initialization failed", ex);
32             throw new StrutsException(ex);
33         }
34     }
复制代码

  这里主要是载入一些配置文件的,将依照顺序逐一载入:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……关于文件是怎样载入的。大家能够自己取看源文件,主要是由xwork核心类载入的。代码在xwork-coresrcmainjavacomopensymphonyxwork2configproviders包里面。

  如今,我们回到StrutsPrepareAndExecuteFilter类中,刚才我们分析了StrutsPrepareAndExecuteFilter类的init方法,该方法在web容器一启动就会调用的,当用户訪问某个action的时候,首先调用核心过滤器StrutsPrepareAndExecuteFilter的doFilter方法。该方法内容例如以下:

复制代码
 1 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
 2 
 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             prepare.assignDispatcherToThread();
12             if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
13                 chain.doFilter(request, response);
14             } else {
15                 request = prepare.wrapRequest(request);
16                 ActionMapping mapping = prepare.findActionMapping(request, response, true);
17                 //假设mapping为空。则觉得不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
18                 if (mapping == null) {
19                     boolean handled = execute.executeStaticResourceRequest(request, response);
20                     if (!handled) {
21                         chain.doFilter(request, response);
22                     }
23                 } else {
24                     //运行action
25                     execute.executeAction(request, response, mapping);
26                 }
27             }
28         } finally {
29             prepare.cleanupRequest(request);
30         }
31     }
复制代码

  以下对doFilter方法中的重点部分一一解说:

(1)prepare.setEncodingAndLocale(request, response);

  第8行是调用prepare对象的setEncodingAndLocale方法。prepare是PrepareOperations类的对象,PrepareOperations类是用来做请求准备工作的。我们看下PrepareOperations类中的setEncodingAndLocale方法:

1 public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
2         dispatcher.prepare(request, response);
3     }

  在这方法里面我们能够看到它仅仅是调用了dispatcher的prepare方法而已,以下我们看看dispatcher的prepare方法:

复制代码
 1 public void prepare(HttpServletRequest request, HttpServletResponse response) {
 2         String encoding = null;
 3         if (defaultEncoding != null) {
 4             encoding = defaultEncoding;
 5         }
 6         // check for Ajax request to use UTF-8 encoding strictly http://www.w3.org/TR/XMLHttpRequest/#the-send-method
 7         if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
 8             encoding = "UTF-8";
 9         }
10 
11         Locale locale = null;
12         if (defaultLocale != null) {
13             locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
14         }
15 
16         if (encoding != null) {
17             applyEncoding(request, encoding);
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     }
复制代码

  我们能够看到该方法仅仅是简单的设置了encoding 和locale ,做的仅仅是一些辅助的工作。

(2)prepare.createActionContext(request, response)

  我们回到StrutsPrepareAndExecuteFilter的doFilter方法,看到第10行代码:prepare.createActionContext(request, response);这是action上下文的创建,ActionContext是一个容器。这个easy主要存储request、session、application、parameters等相关信 息.ActionContext是一个线程的本地变量。这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问 题。

事实上质是一个Map,key是标示request、session、……的字符串,值是其相应的对象,我们能够看到com.opensymphony.xwork2.ActionContext类中时例如以下定义的:

1 static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();


我们看下PrepareOperations类的createActionContext方法:

复制代码
 1 /**
 2      * Creates the action context and initializes the 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 stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
18             stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
19             //stack.getContext()返回的是一个Map<String,Object>,依据此Map构造一个ActionContext
20             ctx = new ActionContext(stack.getContext());
21         }
22         request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
23         //将ActionContext存到ThreadLocal 
24         ActionContext.setContext(ctx);
25         return ctx;
26     }
复制代码

    上面第18行代码中dispatcher.createContextMap,怎样封装相关參数:

复制代码
 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 
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     }
复制代码

(3)request = prepare.wrapRequest(request)

  我们再次回到StrutsPrepareAndExecuteFilter的doFilter方法中,看到第15行:request = prepare.wrapRequest(request);这一句是对request进行包装的。我们看下prepare的wrapRequest方法:

复制代码
 1 public HttpServletRequest wrapRequest(HttpServletRequest oldRequest) throws ServletException {
 2         HttpServletRequest request = oldRequest;
 3         try {
 4             // Wrap request first, just in case it is multipart/form-data
 5             // parameters might not be accessible through before encoding (ww-1278)
 6             request = dispatcher.wrapRequest(request, servletContext);
 7         } catch (IOException e) {
 8             throw new ServletException("Could not wrap servlet request with MultipartRequestWrapper!", e);
 9         }
10         return request;
11     }
复制代码

  由第6行我们能够看到它里面调用的是dispatcher的wrapRequest方法。而且将servletContext对象也传进去了,我们看下dispatcher的wrapRequest:

复制代码
 1 public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
 2         // don't wrap more than once
 3         if (request instanceof StrutsRequestWrapper) {
 4             return request;
 5         }
 6 
 7         String content_type = request.getContentType();
 8         //假设content_type是multipart/form-data类型,则将request包装成MultiPartRequestWrapper对象,否则包装成StrutsRequestWrapper对象
 9         if (content_type != null && content_type.contains("multipart/form-data")) {
10             MultiPartRequest mpr = getMultiPartRequest();
11             LocaleProvider provider = getContainer().getInstance(LocaleProvider.class);
12             request = new MultiPartRequestWrapper(mpr, request, getSaveDir(servletContext), provider);
13         } else {
14             request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
15         }
16 
17         return request;
18     }
复制代码

  此次包装依据请求内容的类型不同。返回不同的对象。假设为multipart/form-data类型,则返回MultiPartRequestWrapper类型的对象,该对象服务于文件上传。否则返回StrutsRequestWrapper类型的对象,MultiPartRequestWrapper是StrutsRequestWrapper的子类。而这两个类都是HttpServletRequest接口的实现。

(4)ActionMapping mapping = prepare.findActionMapping(request, response, true)

  包装request后。通过ActionMapper的getMapping()方法得到请求的Action。Action的配置信息存储在ActionMapping对象中,如StrutsPrepareAndExecuteFilter的doFilter方法中第16行:ActionMapping mapping = prepare.findActionMapping(request, response, true);我们找到prepare对象的findActionMapping方法:

复制代码
 1 public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup) {
 2         //首先从request对象中取mapping对象。看是否存在
 3         ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY);
 4         //不存在就创建一个
 5         if (mapping == null || forceLookup) {
 6             try {
 7                 //首先创建ActionMapper对象,通过ActionMapper对象创建mapping对象
 8                 mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager());
 9                 if (mapping != null) {
10                     request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping);
11                 }
12             } catch (Exception ex) {
13                 dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
14             }
15         }
16 
17         return mapping;
18     }
复制代码

  以下是ActionMapper接口的实现类DefaultActionMapper的getMapping()方法的源码:

复制代码
 1 public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
 2         ActionMapping mapping = new ActionMapping();
 3         //获得请求的uri,即请求路径URL中project名以后的部分。如/userAction.action
 4         String uri = getUri(request);
 5         //修正url的带;jsessionid 时找不到的bug
 6         int indexOfSemicolon = uri.indexOf(";");
 7         uri = (indexOfSemicolon > -1) ?

uri.substring(0, indexOfSemicolon) : uri; 8 //删除扩展名。如.action或者.do 9 uri = dropExtension(uri, mapping); 10 if (uri == null) { 11 return null; 12 } 13 //从uri中分离得到请求的action名、命名空间。 14 parseNameAndNamespace(uri, mapping, configManager); 15 //处理特殊的请求參数 16 handleSpecialParameters(request, mapping); 17 //假设同意动态方法调用,即形如/userAction!getAll.action的请求,分离action名和方法名 18 return parseActionName(mapping); 19 }

复制代码

  以下对getMapping方法中的重要部分一一解说:

  ①:parseNameAndNamespace(uri, mapping, configManager)

  我们主要看下第14行的parseNameAndNamespace(uri, mapping, configManager);这种方法的主要作用是分离出action名和命名空间:

复制代码
 1 protected void parseNameAndNamespace(String uri, ActionMapping mapping, ConfigurationManager configManager) {
 2         String namespace, name;
 3         int lastSlash = uri.lastIndexOf("/"); //最后的斜杆的位置
 4         if (lastSlash == -1) {
 5             namespace = "";
 6             name = uri;
 7         } else if (lastSlash == 0) {
 8             // ww-1046, assume it is the root namespace, it will fallback to
 9             // default
10             // namespace anyway if not found in root namespace.
11             namespace = "/";
12             name = uri.substring(lastSlash + 1);
13         //同意採用完整的命名空间,即设置命名空间是否必须进行精确匹配
14         } else if (alwaysSelectFullNamespace) {
15             // Simply select the namespace as everything before the last slash
16             namespace = uri.substring(0, lastSlash);
17             name = uri.substring(lastSlash + 1);
18         } else {
19             // Try to find the namespace in those defined, defaulting to ""
20             Configuration config = configManager.getConfiguration();
21             String prefix = uri.substring(0, lastSlash); //暂时的命名空间。将会用来进行匹配
22             namespace = "";//将命名空间暂时设为""
23             boolean rootAvailable = false;//rootAvailable作用是推断配置文件里是否配置了命名空间"/"
24             // Find the longest matching namespace, defaulting to the default
25             for (Object cfg : config.getPackageConfigs().values()) { //循环遍历配置文件里的package标签
26                 String ns = ((PackageConfig) cfg).getNamespace();    //获取每一个package标签的namespace属性
27                 //进行匹配
28                 if (ns != null && prefix.startsWith(ns) && (prefix.length() == ns.length() || prefix.charAt(ns.length()) == '/')) {
29                     if (ns.length() > namespace.length()) {
30                         namespace = ns;
31                     }
32                 }
33                 if ("/".equals(ns)) {
34                     rootAvailable = true;
35                 }
36             }
37 
38             name = uri.substring(namespace.length() + 1);
39 
40             // Still none found, use root namespace if found
41             if (rootAvailable && "".equals(namespace)) {
42                 namespace = "/";
43             }
44         }
45 
46         if (!allowSlashesInActionNames) {
47             int pos = name.lastIndexOf('/');
48             if (pos > -1 && pos < name.length() - 1) {
49                 name = name.substring(pos + 1);
50             }
51         }
52         //将分离后的acion名和命名空间保存到mapping对象
53         mapping.setNamespace(namespace);
54         mapping.setName(cleanupActionName(name));
55     }
复制代码

  看到上面代码的第14行,參数alwaysSelectFullNamespace我们能够通过名字就能大概猜出来"同意採用完整的命名空间"。即设置命名空间是否必须进行精确匹配,true必须,false能够模糊匹配,默认是false。

进行精确匹配时要求请求url中的命名空间必须与配置文件里配置的某个命名空间必须同样。假设没有找到同样的则匹配失败。这个參数可通过struts2的"struts.mapper.alwaysSelectFullNamespace"常量配置,如:<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />。

alwaysSelectFullNamespace为true时,将uri以lastSlash为切割。左边的为namespace,右边的为name。

如:http://localhost:8080/myproject/home/actionName!method.action,此时uri为/home/actionName!method.action(只是前面把后缀名去掉了,变成/home/actionName!method),lastSlash的,当前值是5,这样namespace为"/home", name为actionName!method。

  我们再看到上面代码第18行到第44行:当上面的全部条件都不满足时。当中包含alwaysSelectFullNamespace 为false(命名空间进行模糊匹配),将由此部分处理。进行模糊匹配。第1句,通过configManager.getConfiguration()从配置管理器中获得配置对象Configuration,Configuration中存放着struts2的全部配置,形式是将xml文档的对应元素封装为java bean。如<package>元素被封装到PackageConfig类中,这个一会儿会用到。第2句按lastSlash将uri截取出prefix,这是一个暂时的命名空间,之后将会拿prefix进行模糊匹配。

第3句namespace = ""。将命名空间临时设为""。

第4句创建并设置rootAvailable,rootAvailable作用是推断配置文件里是否配置了命名空间"/",true为配置了,false未配置。以下for语句将会遍历我们配置的全部包(<package>),同一时候设置rootAvailable。

第5句for,通过config.getPackageConfigs()获得全部已经配置的包。然后遍历。String ns = ((PackageConfig) cfg).getNamespace()获得当前包的命名空间ns,之后的if句是进行模糊匹配的核心,我摘出来单独说。例如以下:

1 if (ns != null && prefix.startsWith(ns) && (prefix.length() == ns.length() || prefix.charAt(ns.length()) == '/')) {
2                     if (ns.length() > namespace.length()) {
3                         namespace = ns;
4                     }
5                 }

  ns != null && prefix.startsWith(ns)这部分推断当ns不等于空而且ns是prefix的前缀。prefix.length() == ns.length()当二者长度相等时,结合前面部分就是ns是prefix的前缀而且二者长度相等,终于结论就是ns和prefix相等。假设前面的条件不成立,则说明prefix的长度大于ns。prefix.charAt(ns.length()) == '/')意思是prefix中与ns不相等的字符中的第一个字符必须是"/",也就是说,在命名空间採用斜杠分级的形式中。ns必须是prefix的某一子集,如:/common/home 是用户配置的命名空间。则在http的请求url中。/common/home/index1、/common/home/index2/common/home/index/aaa 都是正确的。都能够成功的匹配到/common/home,而/common/homeaa、/common/homea/aaa都是错误的。接着if (ns.length() > namespace.length()) 句。目的是找出字符长度最长的。

由于命名空间採用的是分级的,则长度越长所表示的越精确,如/common/home/index比/common/home精确。之后将namespace = ns。

  我们接着往下看if ("/".equals(ns)) 当我们配置了"/"这个命名空间时。将rootAvailable = true。

name = uri.substring(namespace.length() + 1)句不涉及到命名空间就不说了。

if (rootAvailable && "".equals(namespace))假设通过上面的for循环没有找到匹配的命名空间即namespace的值仍然是当初设置的""。但却配置了"/"时,将命名空间设为"/"。

  我们再看到第46到51行那个if语句:

1 if (!allowSlashesInActionNames) {
2      int pos = name.lastIndexOf('/');
3      if (pos > -1 && pos < name.length() - 1) {
4           name = name.substring(pos + 1);
5      }
6 }

  allowSlashesInActionNames代表是否同意"/"出如今Action的名称中。默觉得false,假设不同意"/"出如今Action名中。而且这时的Action名中有"/",则取"/"后面的部分。

以下是命名空间匹配规则的总结:

(1). 假设请求url中没有命名空间时。将採用"/"作为命名空间。

(2). 当我们将常量 struts.mapper.alwaysSelectFullNamespace设为true时,那么请求url的命名空间必须和配置文件配置的全然同样才干匹配。

当将常量 struts.mapper.alwaysSelectFullNamespace设为false时,那么请求url的命名空间和配置文件配置的可按模糊匹配。规则:

  a.假设配置文件里配置了/common 而url中的命名空间/common、/common/home、/common/home/index等等都是可匹配的。即子命名空间可匹配父命名空间。

  b.假设对于某个url请求中的命名空间同一时候匹配了俩个或俩个以上的配置文件里配置的命名空间,则选字符最长的,如:当前请求的命名空间为/common/home/index/aaaa,  而我们在配置时同一时候配置               了/common/home、/common/home/index  则将会匹配命名空间最长的。即/common/home/index。

(3).最后,假设请求的命名空间在配置中没有匹配到时,将採用""作为命名空间。假设没有设置为""的命名空间将抛出404错误。

  ②:parseActionName(mapping)

  好了,到这里parseNameAndNamespace方法已经分析完了,我们再次回到getMapping方法中去,看到16行:handleSpecialParameters(request, mapping);好像是处理特殊參数的函数吧,里面有点看不懂,临时就无论,以后有时间再研究。我们看到18行:return parseActionName(mapping);主要是用来处理形如/userAction!getAll.action的请求,分离action名和方法名:

复制代码
 1 protected ActionMapping parseActionName(ActionMapping mapping) {
 2         if (mapping.getName() == null) {
 3             return null;
 4         }
 5         //假设同意动态方法调用
 6         if (allowDynamicMethodCalls) {
 7             // handle "name!method" convention.
 8             String name = mapping.getName();
 9             int exclamation = name.lastIndexOf("!");
10             //假设包括"!"就进行分离
11             if (exclamation != -1) {
12                 //分离出action名
13                 mapping.setName(name.substring(0, exclamation));
14                 //分离出方法名
15                 mapping.setMethod(name.substring(exclamation + 1));
16             }
17         }
18         return mapping;
19     }
复制代码

  到此为止getMapping方法已经分析结束了!

(5)execute.executeAction(request, response, mapping)

  上面我们分析完了mapping的获取,继续看doFilter方法:

复制代码
 1 //假设mapping为空,则觉得不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
 2                 if (mapping == null) {
 3                     boolean handled = execute.executeStaticResourceRequest(request, response);
 4                     if (!handled) {
 5                         chain.doFilter(request, response);
 6                     }
 7                 } else {
 8                     //运行action
 9                     execute.executeAction(request, response, mapping);
10                 }
复制代码

  假设mapping对象不为空,则会运行action,我们看到上面代码第9行:execute是ExecuteOperations类的对象,ExecuteOperations类在包org.apache.struts2.dispatcher.ng以下,我们找到它里面的executeAction方法:

1 public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
2         dispatcher.serviceAction(request, response, servletContext, mapping);
3     }

我们能够看到它里面仅仅是简单的调用了dispatcher的serviceAction方法:我们找到dispatcher的serviceAction方法:

复制代码
 1 public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
 2                               ActionMapping mapping) throws ServletException {
 3         //封转上下文环境,主要将requestMap、params、session等Map封装成为一个上下文Map
 4         Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
 5 
 6         // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
 7         ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
 8         boolean nullStack = stack == null;
 9         if (nullStack) {
10             ActionContext ctx = ActionContext.getContext();
11             if (ctx != null) {
12                 stack = ctx.getValueStack();
13             }
14         }
15         if (stack != null) {
16             extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
17         }
18 
19         String timerKey = "Handling request from Dispatcher";
20         try {
21             UtilTimerStack.push(timerKey);
22             String namespace = mapping.getNamespace();//从mapping对象获取命名空间
23             String name = mapping.getName();          //获取请求的action名
24             String method = mapping.getMethod();      //获取请求方法
25             //得到配置对象
26             Configuration config = configurationManager.getConfiguration();
27             //依据运行上下文參数,命名空间。名称等创建用户自己定义Action的代理对象  
28             ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
29                     namespace, name, method, extraContext, true, false);
30 
31             request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
32 
33             // if the ActionMapping says to go straight to a result, do it!
34             //假设配置文件里运行的这个action配置了result,就直接转到result
35             if (mapping.getResult() != null) {
36                 Result result = mapping.getResult();
37                 result.execute(proxy.getInvocation());
38             } else {
39                 proxy.execute();
40             }
41 
42             // If there was a previous value stack then set it back onto the request
43             if (!nullStack) {
44                 request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
45             }
46         } catch (ConfigurationException e) {
47             // WW-2874 Only log error if in devMode
48             if (devMode) {
49                 String reqStr = request.getRequestURI();
50                 if (request.getQueryString() != null) {
51                     reqStr = reqStr + "?

" + request.getQueryString(); 52 } 53 LOG.error("Could not find action or result " + reqStr, e); 54 } else { 55 if (LOG.isWarnEnabled()) { 56 LOG.warn("Could not find action or result", e); 57 } 58 } 59 sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); 60 } catch (Exception e) { 61 if (handleException || devMode) { 62 sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); 63 } else { 64 throw new ServletException(e); 65 } 66 } finally { 67 UtilTimerStack.pop(timerKey); 68 } 69 }

复制代码

  最后通过Result完毕页面跳转!

 

总结:曾经总是仅仅会用struts2框架。对里面的原理没有一个非常清晰的认识。这两天花时间把struts2框架的源代码分析了一下。对它的工作原理有个更深的认识。既然是开源框架,有时间久得去研究研究它的源代码,不然开源就失去了意义了,不要仅仅停留在会用的层面上。

原文地址:https://www.cnblogs.com/claireyuancy/p/6906807.html