Java struts2請(qǐng)求源碼分析案例詳解
Struts2是Struts社區(qū)和WebWork社區(qū)的共同成果,我們甚至可以說,Struts2是WebWork的升級(jí)版,他采用的正是WebWork的核心,所以,Struts2并不是一個(gè)不成熟的產(chǎn)品,相反,構(gòu)建在WebWork基礎(chǔ)之上的Struts2是一個(gè)運(yùn)行穩(wěn)定、性能優(yōu)異、設(shè)計(jì)成熟的WEB框架。
我這里的struts2源碼是從官網(wǎng)下載的一個(gè)最新的struts-2.3.15.1-src.zip,將其解壓即可。里面的目錄頁文件非常的多,我們只需要定位到struts-2.3.15.1\src\core\src\main\java\org\apache\struts2查看源文件。目錄結(jié)構(gòu)如下圖
Struts2框架的正常運(yùn)行,除了占核心地位的xwork的支持以外,Struts2本身也提供了許多類,這些類被分門別類組織到不同的包中。從源代碼中發(fā)現(xiàn),基本上每一個(gè)Struts2類都訪問了WebWork提供的功能,從而也可以看出Struts2與WebWork千絲萬縷的聯(lián)系。但無論如何,Struts2的核心功能比如將請(qǐng)求委托給哪個(gè)Action處理都是由xwork完成的,Struts2只是在WebWork的基礎(chǔ)上做了適當(dāng)?shù)暮?jiǎn)化、加強(qiáng)和封裝,并少量保留Struts1.x中的習(xí)慣。
以下是包說明:
org.apache.struts2. components | 該包封裝視圖組件,Struts2在視圖組件上有了很大加強(qiáng),不僅增加了組件的屬性個(gè)數(shù),更新增了幾個(gè)非常有用的組件,如updownselect、doubleselect、datetimepicker、token、tree等。 另外,Struts2可視化視圖組件開始支持主題(theme),缺省情況下,使用自帶的缺省主題,如果要自定義頁面效果,需要將組件的theme屬性設(shè)置為simple。 |
org.apache.struts2. config | 該包定義與配置相關(guān)的接口和類。實(shí)際上,工程中的xml和properties文件的讀取和解析都是由WebWork完成的,Struts只做了少量的工作。 |
org.apache.struts2.dispatcher | Struts2的核心包,最重要的類都放在該包中。 |
org.apache.struts2.impl | 該包只定義了3個(gè)類,他們是StrutsActionProxy、StrutsActionProxyFactory、StrutsObjectFactory,這三個(gè)類都是對(duì)xwork的擴(kuò)展。 |
org.apache.struts2.interceptor | 定義內(nèi)置的截?cái)r器。 |
org.apache.struts2.servlet | 用HttpServletRequest相關(guān)方法實(shí)現(xiàn)principalproxy接口。 |
org.apache.struts2.util | 實(shí)用包。 |
org.apache.struts2.views | 提供freemarker、jsp、velocity等不同類型的頁面呈現(xiàn)。 |
根目錄下的5個(gè)文件說明:
StrutsStatics | Struts常數(shù)。常數(shù)可以用來獲取或設(shè)置對(duì)象從行動(dòng)中或其他集合。 |
RequestUtils | 請(qǐng)求處理程序類。此類只有一個(gè)方法getServletPath,作用檢索當(dāng)前請(qǐng)求的servlet路徑 |
ServletActionContext | 網(wǎng)站的特定的上下文信息 |
StrutsConstants | 該類提供了框架配置鍵的中心位置用于存儲(chǔ)和檢索配置設(shè)置。 |
StrutsException | 通用運(yùn)行時(shí)異常類 |
struts2 架構(gòu)圖如下圖所示:
依照上圖,我們可以看出一個(gè)請(qǐng)求在struts的處理大概有如下步驟:
- 客戶端初始化一個(gè)指向Servlet容器(例如Tomcat)的請(qǐng)求;
- 這個(gè)請(qǐng)求經(jīng)過一系列的過濾器(Filter)(這些過濾器中有一個(gè)叫做ActionContextCleanUp的可選過濾器,這個(gè)過濾器對(duì)于Struts2和其他框架的集成很有幫助,例如:SiteMesh Plugin);
- 接著StrutsPrepareAndExecuteFilter被調(diào)用,StrutsPrepareAndExecuteFilter詢問ActionMapper來決定這個(gè)請(qǐng)求是否需要調(diào)用某個(gè)Action;
- 如果ActionMapper決定需要調(diào)用某個(gè)Action,F(xiàn)ilterDispatcher把請(qǐng)求的處理交給ActionProxy;
- ActionProxy通過Configuration Manager詢問框架的配置文件,找到需要調(diào)用的Action類;
- ActionProxy創(chuàng)建一個(gè)ActionInvocation的實(shí)例。
- ActionInvocation實(shí)例使用命名模式來調(diào)用,在調(diào)用Action的過程前后,涉及到相關(guān)攔截器(Intercepter)的調(diào)用。
- 一旦Action執(zhí)行完畢,ActionInvocation負(fù)責(zé)根據(jù)struts.xml中的配置找到對(duì)應(yīng)的返回結(jié)果。返回結(jié)果通常是(但不總是,也可能是另外的一個(gè)Action鏈)一個(gè)需要被表示的JSP或者FreeMarker的模版。在表示的過程中可以使用Struts2 框架中繼承的標(biāo)簽。在這個(gè)過程中需要涉及到ActionMapper。
strut2源碼分析:
首先我們使用struts2框架都會(huì)在web.xml中注冊(cè)和映射struts2,配置內(nèi)容如下:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注:在早期的struts2中,都是使用FilterDispathcer,從Struts 2.1.3開始,它已不推薦使用。如果你使用的Struts的版本 >= 2.1.3,推薦升級(jí)到新的Filter,StrutsPrepareAndExecuteFilter。在此研究的是StrutsPrepareAndExecuteFilter。
StrutsPrepareAndExecuteFilter中的方法:
void init(FilterConfig filterConfig) | 繼承自Filter,過濾器的初始化 |
doFilter(ServletRequest req, ServletResponse res, FilterChain chain) | 繼承自Filter,執(zhí)行過濾器 |
void destroy() | 繼承自Filter,用于資源釋放 |
void postInit(Dispatcher dispatcher, FilterConfig filterConfig) | Callback for post initialization(一個(gè)空的方法,用于方法回調(diào)初始化) |
web容器一啟動(dòng),就會(huì)初始化核心過濾器StrutsPrepareAndExecuteFilter,并執(zhí)行初始化方法,初始化方法如下:
public void init(FilterConfig filterConfig) throws ServletException { InitOperations init = new InitOperations(); Dispatcher dispatcher = null; try { //封裝filterConfig,其中有個(gè)主要方法getInitParameterNames將參數(shù)名字以String格式存儲(chǔ)在List中 FilterHostConfig config = new FilterHostConfig(filterConfig); //初始化struts內(nèi)部日志 init.initLogging(config); //創(chuàng)建dispatcher ,并初始化 dispatcher = init.initDispatcher(config); init.initStaticContentLoader(config, dispatcher); //初始化類屬性:prepare 、execute prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher); this.excludedPatterns = init.buildExcludedPatternsList(dispatcher); //回調(diào)空的postInit方法 postInit(dispatcher, filterConfig); } finally { if (dispatcher != null) { dispatcher.cleanUpAfterInit(); } init.cleanup(); } }
關(guān)于封裝filterConfig,首先看下FilterHostConfig ,源碼如下:
public class FilterHostConfig implements HostConfig { private FilterConfig config; //構(gòu)造方法 public FilterHostConfig(FilterConfig config) { this.config = config; } //根據(jù)init-param配置的param-name獲取param-value的值 public String getInitParameter(String key) { return config.getInitParameter(key); } //返回初始化參數(shù)名的迭代器 public Iterator<String> getInitParameterNames() { return MakeIterator.convert(config.getInitParameterNames()); } //返回Servlet上下文 public ServletContext getServletContext() { return config.getServletContext(); } }
只有短短的幾行代碼,getInitParameterNames是這個(gè)類的核心,將Filter初始化參數(shù)名稱有枚舉類型轉(zhuǎn)為Iterator。此類的主要作為是對(duì)filterConfig 封裝。
接下來,看下StrutsPrepareAndExecuteFilter中init方法中dispatcher = init.initDispatcher(config);這是初始化dispatcher的,是通過init對(duì)象的initDispatcher方法來初始化的,init是InitOperations類的對(duì)象,我們看看InitOperations中initDispatcher方法:
public Dispatcher initDispatcher( HostConfig filterConfig ) { Dispatcher dispatcher = createDispatcher(filterConfig); dispatcher.init(); return dispatcher; }
創(chuàng)建Dispatcher,會(huì)讀取 filterConfig 中的配置信息,將配置信息解析出來,封裝成為一個(gè)Map,然后根絕servlet上下文和參數(shù)Map構(gòu)造Dispatcher :
private Dispatcher createDispatcher( HostConfig filterConfig ) { //存放參數(shù)的Map Map<String, String> params = new HashMap<String, String>(); //將參數(shù)存放到Map for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) { String name = (String) e.next(); String value = filterConfig.getInitParameter(name); params.put(name, value); } //根據(jù)servlet上下文和參數(shù)Map構(gòu)造Dispatcher return new Dispatcher(filterConfig.getServletContext(), params); }
這樣dispatcher對(duì)象創(chuàng)建完成,接著就是dispatcher對(duì)象的初始化,打開Dispatcher類,看到它的init方法如下:
public void init() { if (configurationManager == null) { configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME); } try { init_FileManager(); //加載org/apache/struts2/default.properties init_DefaultProperties(); // [1] //加載struts-default.xml,struts-plugin.xml,struts.xml init_TraditionalXmlConfigurations(); // [2] init_LegacyStrutsProperties(); // [3] //用戶自己實(shí)現(xiàn)的ConfigurationProviders類 init_CustomConfigurationProviders(); // [5] //Filter的初始化參數(shù) init_FilterInitParameters() ; // [6] init_AliasStandardObjects() ; // [7] Container container = init_PreloadConfiguration(); container.inject(this); init_CheckWebLogicWorkaround(container); if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { l.dispatcherInitialized(this); } } } catch (Exception ex) { if (LOG.isErrorEnabled()) LOG.error("Dispatcher initialization failed", ex); throw new StrutsException(ex); } }
這里主要是加載一些配置文件的,將按照順序逐一加載:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……關(guān)于文件是如何加載的,大家可以自己取看源文件,主要是由xwork核心類加載的,代碼在xwork-core\src\main\java\com\opensymphony\xwork2\config\providers包里面。
現(xiàn)在,我們回到StrutsPrepareAndExecuteFilter類中,剛才我們分析了StrutsPrepareAndExecuteFilter類的init方法,該方法在web容器一啟動(dòng)就會(huì)調(diào)用的,當(dāng)用戶訪問某個(gè)action的時(shí)候,首先調(diào)用核心過濾器StrutsPrepareAndExecuteFilter的doFilter方法,該方法內(nèi)容如下:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; try { //設(shè)置編碼和國(guó)際化 prepare.setEncodingAndLocale(request, response); //創(chuàng)建action上下文 prepare.createActionContext(request, response); prepare.assignDispatcherToThread(); if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { chain.doFilter(request, response); } else { request = prepare.wrapRequest(request); ActionMapping mapping = prepare.findActionMapping(request, response, true); //如果mapping為空,則認(rèn)為不是調(diào)用action,會(huì)調(diào)用下一個(gè)過濾器鏈,直到獲取到mapping才調(diào)用action if (mapping == null) { boolean handled = execute.executeStaticResourceRequest(request, response); if (!handled) { chain.doFilter(request, response); } } else { //執(zhí)行action execute.executeAction(request, response, mapping); } } } finally { prepare.cleanupRequest(request); } }
下面對(duì)doFilter方法中的重點(diǎn)部分一一講解:
(1)prepare.setEncodingAndLocale(request, response);
第8行是調(diào)用prepare對(duì)象的setEncodingAndLocale方法,prepare是PrepareOperations類的對(duì)象,PrepareOperations類是用來做請(qǐng)求準(zhǔn)備工作的。我們看下PrepareOperations類中的setEncodingAndLocale方法:
public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) { dispatcher.prepare(request, response); }
在這方法里面我們可以看到它只是調(diào)用了dispatcher的prepare方法而已,下面我們看看dispatcher的prepare方法:
public void prepare(HttpServletRequest request, HttpServletResponse response) { String encoding = null; if (defaultEncoding != null) { encoding = defaultEncoding; } // check for Ajax request to use UTF-8 encoding strictly http://www.w3.org/TR/XMLHttpRequest/#the-send-method if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { encoding = "UTF-8"; } Locale locale = null; if (defaultLocale != null) { locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale()); } if (encoding != null) { applyEncoding(request, encoding); } if (locale != null) { response.setLocale(locale); } if (paramsWorkaroundEnabled) { request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request } }
我們可以看到該方法只是簡(jiǎn)單的設(shè)置了encoding 和locale ,做的只是一些輔助的工作。
(2)prepare.createActionContext(request, response)
我們回到StrutsPrepareAndExecuteFilter的doFilter方法,看到第10行代碼:prepare.createActionContext(request, response);這是action上下文的創(chuàng)建,ActionContext是一個(gè)容器,這個(gè)容易主要存儲(chǔ)request、session、application、parameters等相關(guān)信 息.ActionContext是一個(gè)線程的本地變量,這意味著不同的action之間不會(huì)共享ActionContext,所以也不用考慮線程安全問 題。其實(shí)質(zhì)是一個(gè)Map,key是標(biāo)示request、session、……的字符串,值是其對(duì)應(yīng)的對(duì)象,我們可以看到com.opensymphony.xwork2.ActionContext類中時(shí)如下定義的:
static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
我們看下PrepareOperations類的createActionContext方法:
/** * Creates the action context and initializes the thread local */ public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) { ActionContext ctx; Integer counter = 1; Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER); if (oldCounter != null) { counter = oldCounter + 1; } //此處是從ThreadLocal中獲取此ActionContext變量 ActionContext oldContext = ActionContext.getContext(); if (oldContext != null) { // detected existing context, so we are probably in a forward ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap())); } else { ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext)); //stack.getContext()返回的是一個(gè)Map<String,Object>,根據(jù)此Map構(gòu)造一個(gè)ActionContext ctx = new ActionContext(stack.getContext()); } request.setAttribute(CLEANUP_RECURSION_COUNTER, counter); //將ActionContext存到ThreadLocal ActionContext.setContext(ctx); return ctx; }
上面第18行代碼中dispatcher.createContextMap,如何封裝相關(guān)參數(shù):
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping, ServletContext context) { // request map wrapping the http request objects Map requestMap = new RequestMap(request); // parameters map wrapping the http parameters. ActionMapping parameters are now handled and applied separately Map params = new HashMap(request.getParameterMap()); // session map wrapping the http session Map session = new SessionMap(request); // application map wrapping the ServletContext Map application = new ApplicationMap(context); //requestMap、params、session等Map封裝成為一個(gè)上下文Map Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context); if (mapping != null) { extraContext.put(ServletActionContext.ACTION_MAPPING, mapping); } return extraContext; }
(3)request = prepare.wrapRequest(request)
我們?cè)俅位氐絊trutsPrepareAndExecuteFilter的doFilter方法中,看到第15行:request = prepare.wrapRequest(request);這一句是對(duì)request進(jìn)行包裝的,我們看下prepare的wrapRequest方法:
public HttpServletRequest wrapRequest(HttpServletRequest oldRequest) throws ServletException { HttpServletRequest request = oldRequest; try { // Wrap request first, just in case it is multipart/form-data // parameters might not be accessible through before encoding (ww-1278) request = dispatcher.wrapRequest(request, servletContext); } catch (IOException e) { throw new ServletException("Could not wrap servlet request with MultipartRequestWrapper!", e); } return request; }
由第6行我們可以看到它里面調(diào)用的是dispatcher的wrapRequest方法,并且將servletContext對(duì)象也傳進(jìn)去了,我們看下dispatcher的wrapRequest:
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException { // don't wrap more than once if (request instanceof StrutsRequestWrapper) { return request; } String content_type = request.getContentType(); //如果content_type是multipart/form-data類型,則將request包裝成MultiPartRequestWrapper對(duì)象,否則包裝成StrutsRequestWrapper對(duì)象 if (content_type != null && content_type.contains("multipart/form-data")) { MultiPartRequest mpr = getMultiPartRequest(); LocaleProvider provider = getContainer().getInstance(LocaleProvider.class); request = new MultiPartRequestWrapper(mpr, request, getSaveDir(servletContext), provider); } else { request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup); } return request; }
此次包裝根據(jù)請(qǐng)求內(nèi)容的類型不同,返回不同的對(duì)象,如果為multipart/form-data類型,則返回MultiPartRequestWrapper類型的對(duì)象,該對(duì)象服務(wù)于文件上傳,否則返回StrutsRequestWrapper類型的對(duì)象,MultiPartRequestWrapper是StrutsRequestWrapper的子類,而這兩個(gè)類都是HttpServletRequest接口的實(shí)現(xiàn)。
(4)ActionMapping mapping = prepare.findActionMapping(request, response, true)
包裝request后,通過ActionMapper的getMapping()方法得到請(qǐng)求的Action,Action的配置信息存儲(chǔ)在ActionMapping對(duì)象中,如StrutsPrepareAndExecuteFilter的doFilter方法中第16行:ActionMapping mapping = prepare.findActionMapping(request, response, true);我們找到prepare對(duì)象的findActionMapping方法:
public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup) { //首先從request對(duì)象中取mapping對(duì)象,看是否存在 ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY); //不存在就創(chuàng)建一個(gè) if (mapping == null || forceLookup) { try { //首先創(chuàng)建ActionMapper對(duì)象,通過ActionMapper對(duì)象創(chuàng)建mapping對(duì)象 mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager()); if (mapping != null) { request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping); } } catch (Exception ex) { dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); } } return mapping; }
下面是ActionMapper接口的實(shí)現(xiàn)類DefaultActionMapper的getMapping()方法的源代碼:
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) { ActionMapping mapping = new ActionMapping(); //獲得請(qǐng)求的uri,即請(qǐng)求路徑URL中工程名以后的部分,如/userAction.action String uri = getUri(request); //修正url的帶;jsessionid 時(shí)找不到的bug int indexOfSemicolon = uri.indexOf(";"); uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri; //刪除擴(kuò)展名,如.action或者.do uri = dropExtension(uri, mapping); if (uri == null) { return null; } //從uri中分離得到請(qǐng)求的action名、命名空間。 parseNameAndNamespace(uri, mapping, configManager); //處理特殊的請(qǐng)求參數(shù) handleSpecialParameters(request, mapping); //如果允許動(dòng)態(tài)方法調(diào)用,即形如/userAction!getAll.action的請(qǐng)求,分離action名和方法名 return parseActionName(mapping); }
下面對(duì)getMapping方法中的重要部分一一講解:
?、伲簆arseNameAndNamespace(uri, mapping, configManager)
我們主要看下第14行的parseNameAndNamespace(uri, mapping, configManager);這個(gè)方法的主要作用是分離出action名和命名空間:
protected void parseNameAndNamespace(String uri, ActionMapping mapping, ConfigurationManager configManager) { String namespace, name; int lastSlash = uri.lastIndexOf("/"); //最后的斜桿的位置 if (lastSlash == -1) { namespace = ""; name = uri; } else if (lastSlash == 0) { // ww-1046, assume it is the root namespace, it will fallback to // default // namespace anyway if not found in root namespace. namespace = "/"; name = uri.substring(lastSlash + 1); //允許采用完整的命名空間,即設(shè)置命名空間是否必須進(jìn)行精確匹配 } else if (alwaysSelectFullNamespace) { // Simply select the namespace as everything before the last slash namespace = uri.substring(0, lastSlash); name = uri.substring(lastSlash + 1); } else { // Try to find the namespace in those defined, defaulting to "" Configuration config = configManager.getConfiguration(); String prefix = uri.substring(0, lastSlash); //臨時(shí)的命名空間,將會(huì)用來進(jìn)行匹配 namespace = "";//將命名空間暫時(shí)設(shè)為"" boolean rootAvailable = false;//rootAvailable作用是判斷配置文件中是否配置了命名空間"/" // Find the longest matching namespace, defaulting to the default for (Object cfg : config.getPackageConfigs().values()) { //循環(huán)遍歷配置文件中的package標(biāo)簽 String ns = ((PackageConfig) cfg).getNamespace(); //獲取每個(gè)package標(biāo)簽的namespace屬性 //進(jìn)行匹配 if (ns != null && prefix.startsWith(ns) && (prefix.length() == ns.length() || prefix.charAt(ns.length()) == '/')) { if (ns.length() > namespace.length()) { namespace = ns; } } if ("/".equals(ns)) { rootAvailable = true; } } name = uri.substring(namespace.length() + 1); // Still none found, use root namespace if found if (rootAvailable && "".equals(namespace)) { namespace = "/"; } } if (!allowSlashesInActionNames) { int pos = name.lastIndexOf('/'); if (pos > -1 && pos < name.length() - 1) { name = name.substring(pos + 1); } } //將分離后的acion名和命名空間保存到mapping對(duì)象 mapping.setNamespace(namespace); mapping.setName(cleanupActionName(name)); }
看到上面代碼的第14行,參數(shù)alwaysSelectFullNamespace我們可以通過名字就能大概猜出來"允許采用完整的命名空間",即設(shè)置命名空間是否必須進(jìn)行精確匹配,true必須,false可以模糊匹配,默認(rèn)是false。進(jìn)行精確匹配時(shí)要求請(qǐng)求url中的命名空間必須與配置文件中配置的某個(gè)命名空間必須相同,如果沒有找到相同的則匹配失敗。這個(gè)參數(shù)可通過struts2的"struts.mapper.alwaysSelectFullNamespace"常量配置,如:<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />。當(dāng)alwaysSelectFullNamespace為true時(shí),將uri以lastSlash為分割,左邊的為namespace,右邊的為name。如:http://localhost:8080/myproject/home/actionName!method.action,此時(shí)uri為/home/actionName!method.action(不過前面把后綴名去掉了,變成/home/actionName!method),lastSlash的,當(dāng)前值是5,這樣namespace為"/home", name為actionName!method。
我們?cè)倏吹缴厦娲a第18行到第44行:當(dāng)上面的所有條件都不滿足時(shí),其中包括alwaysSelectFullNamespace 為false(命名空間進(jìn)行模糊匹配),將由此部分處理,進(jìn)行模糊匹配。第1句,通過configManager.getConfiguration()從配置管理器中獲得配置對(duì)象Configuration,Configuration中存放著struts2的所有配置,形式是將xml文檔的相應(yīng)元素封裝為java bean,如<package>元素被封裝到PackageConfig類中,這個(gè)一會(huì)兒會(huì)用到。第2句按lastSlash將uri截取出prefix,這是一個(gè)臨時(shí)的命名空間,之后將會(huì)拿prefix進(jìn)行模糊匹配。第3句namespace = "",將命名空間暫時(shí)設(shè)為""。第4句創(chuàng)建并設(shè)置rootAvailable,rootAvailable作用是判斷配置文件中是否配置了命名空間"/",true為配置了,false未配置,下面for語句將會(huì)遍歷我們配置的所有包(<package>),同時(shí)設(shè)置rootAvailable。第5句for,通過config.getPackageConfigs()獲得所有已經(jīng)配置的包,然后遍歷。String ns = ((PackageConfig) cfg).getNamespace()獲得當(dāng)前包的命名空間ns,之后的if句是進(jìn)行模糊匹配的核心,我摘出來單獨(dú)說,如下:
if (ns != null && prefix.startsWith(ns) && (prefix.length() == ns.length() || prefix.charAt(ns.length()) == '/')) { if (ns.length() > namespace.length()) { namespace = ns; } }
ns != null && prefix.startsWith(ns)這部分判斷當(dāng)ns不等于空并且ns是prefix的前綴。prefix.length() == ns.length()當(dāng)二者長(zhǎng)度相等時(shí),結(jié)合前面部分就是ns是prefix的前綴并且二者長(zhǎng)度相等,最終結(jié)論就是ns和prefix相等。如果前面的條件不成立,則說明prefix的長(zhǎng)度大于ns。prefix.charAt(ns.length()) == '/')意思是prefix中與ns不相等的字符中的第一個(gè)字符必須是"/",也就是說,在命名空間采用斜杠分級(jí)的形式中,ns必須是prefix的某一子集,如:/common/home 是用戶配置的命名空間,則在http的請(qǐng)求url中,/common/home/index1、/common/home/index2、/common/home/index/aaa 都是正確的,都可以成功的匹配到/common/home,而/common/homeaa、/common/homea/aaa都是錯(cuò)誤的。接著if (ns.length() > namespace.length()) 句,目的是找出字符長(zhǎng)度最長(zhǎng)的。因?yàn)槊臻g采用的是分級(jí)的,則長(zhǎng)度越長(zhǎng)所表示的越精確,如/common/home/index比/common/home精確。之后將namespace = ns。
我們接著往下看if ("/".equals(ns)) 當(dāng)我們配置了"/"這個(gè)命名空間時(shí),將rootAvailable = true。name = uri.substring(namespace.length() + 1)句不涉及到命名空間就不說了。if (rootAvailable && "".equals(namespace))如果通過上面的for循環(huán)沒有找到匹配的命名空間即namespace的值仍然是當(dāng)初設(shè)置的"",但卻配置了"/"時(shí),將命名空間設(shè)為"/"。
我們?cè)倏吹降?6到51行那個(gè)if語句:
if (!allowSlashesInActionNames) { int pos = name.lastIndexOf('/'); if (pos > -1 && pos < name.length() - 1) { name = name.substring(pos + 1); } }
allowSlashesInActionNames代表是否允許"/"出現(xiàn)在Action的名稱中,默認(rèn)為false,如果不允許"/"出現(xiàn)在Action名中,并且這時(shí)的Action名中有"/",則取"/"后面的部分。
下面是命名空間匹配規(guī)則的總結(jié):
(1). 如果請(qǐng)求url中沒有命名空間時(shí),將采用"/"作為命名空間。
(2). 當(dāng)我們將常量 struts.mapper.alwaysSelectFullNamespace設(shè)為true時(shí),那么請(qǐng)求url的命名空間必須和配置文件配置的完全相同才能匹配。
當(dāng)將常量 struts.mapper.alwaysSelectFullNamespace設(shè)為false時(shí),那么請(qǐng)求url的命名空間和配置文件配置的可按模糊匹配。規(guī)則:
a.如果配置文件中配置了/common 而url中的命名空間/common、/common/home、/common/home/index等等都是可匹配的,即子命名空間可匹配父命名空間。
b.如果對(duì)于某個(gè)url請(qǐng)求中的命名空間同時(shí)匹配了倆個(gè)或倆個(gè)以上的配置文件中配置的命名空間,則選字符最長(zhǎng)的,如:當(dāng)前請(qǐng)求的命名空間為/common/home/index/aaaa, 而我們?cè)谂渲脮r(shí)同時(shí)配置 了/common/home、/common/home/index 則將會(huì)匹配命名空間最長(zhǎng)的,即/common/home/index。
(3).最后,如果請(qǐng)求的命名空間在配置中沒有匹配到時(shí),將采用""作為命名空間。如果沒有設(shè)置為""的命名空間將拋出404錯(cuò)誤。
?、冢簆arseActionName(mapping)
好了,到這里parseNameAndNamespace方法已經(jīng)分析完了,我們?cè)俅位氐絞etMapping方法中去,看到16行:handleSpecialParameters(request, mapping);好像是處理特殊參數(shù)的函數(shù)吧,里面有點(diǎn)看不懂,暫時(shí)就不管,以后有時(shí)間再研究。我們看到18行:return parseActionName(mapping);主要是用來處理形如/userAction!getAll.action的請(qǐng)求,分離action名和方法名:
protected ActionMapping parseActionName(ActionMapping mapping) { if (mapping.getName() == null) { return null; } //如果允許動(dòng)態(tài)方法調(diào)用 if (allowDynamicMethodCalls) { // handle "name!method" convention. String name = mapping.getName(); int exclamation = name.lastIndexOf("!"); //如果包含"!"就進(jìn)行分離 if (exclamation != -1) { //分離出action名 mapping.setName(name.substring(0, exclamation)); //分離出方法名 mapping.setMethod(name.substring(exclamation + 1)); } } return mapping; }
到此為止getMapping方法已經(jīng)分析結(jié)束了!
(5)execute.executeAction(request, response, mapping)
上面我們分析完了mapping的獲取,繼續(xù)看doFilter方法:
//如果mapping為空,則認(rèn)為不是調(diào)用action,會(huì)調(diào)用下一個(gè)過濾器鏈,直到獲取到mapping才調(diào)用action if (mapping == null) { boolean handled = execute.executeStaticResourceRequest(request, response); if (!handled) { chain.doFilter(request, response); } } else { //執(zhí)行action execute.executeAction(request, response, mapping); }
如果mapping對(duì)象不為空,則會(huì)執(zhí)行action,我們看到上面代碼第9行:execute是ExecuteOperations類的對(duì)象,ExecuteOperations類在包org.apache.struts2.dispatcher.ng下面,我們找到它里面的executeAction方法:
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException { dispatcher.serviceAction(request, response, servletContext, mapping); }
我們可以看到它里面只是簡(jiǎn)單的調(diào)用了dispatcher的serviceAction方法:我們找到dispatcher的serviceAction方法:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { //封轉(zhuǎn)上下文環(huán)境,主要將requestMap、params、session等Map封裝成為一個(gè)上下文Map Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); boolean nullStack = stack == null; if (nullStack) { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { stack = ctx.getValueStack(); } } if (stack != null) { extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack)); } String timerKey = "Handling request from Dispatcher"; try { UtilTimerStack.push(timerKey); String namespace = mapping.getNamespace();//從mapping對(duì)象獲取命名空間 String name = mapping.getName(); //獲取請(qǐng)求的action名 String method = mapping.getMethod(); //獲取請(qǐng)求方法 //得到配置對(duì)象 Configuration config = configurationManager.getConfiguration(); //根據(jù)執(zhí)行上下文參數(shù),命名空間,名稱等創(chuàng)建用戶自定義Action的代理對(duì)象 ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( namespace, name, method, extraContext, true, false); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! //如果配置文件中執(zhí)行的這個(gè)action配置了result,就直接轉(zhuǎn)到result if (mapping.getResult() != null) { Result result = mapping.getResult(); result.execute(proxy.getInvocation()); } else { proxy.execute(); } // If there was a previous value stack then set it back onto the request if (!nullStack) { request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); } } catch (ConfigurationException e) { // WW-2874 Only log error if in devMode if (devMode) { String reqStr = request.getRequestURI(); if (request.getQueryString() != null) { reqStr = reqStr + "?" + request.getQueryString(); } LOG.error("Could not find action or result\n" + reqStr, e); } else { if (LOG.isWarnEnabled()) { LOG.warn("Could not find action or result", e); } } sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); } catch (Exception e) { if (handleException || devMode) { sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } else { throw new ServletException(e); } } finally { UtilTimerStack.pop(timerKey); } }
最后通過Result完成頁面跳轉(zhuǎn)!
到此這篇關(guān)于Java struts2請(qǐng)求源碼分析案例詳解的文章就介紹到這了,更多相關(guān)Java struts2請(qǐng)求源碼分析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java struts2 package元素配置及實(shí)例解析
- Java框架Struts2實(shí)現(xiàn)圖片上傳功能
- Java中的Struts2框架攔截器之實(shí)例代碼
- Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a
- struts2簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- struts2數(shù)據(jù)處理_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- struts2標(biāo)簽總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- struts2攔截器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
相關(guān)文章
java實(shí)現(xiàn)人員信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人員信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Ubuntu安裝JDK與IntelliJ?IDEA的詳細(xì)過程
APT是Linux系統(tǒng)上的包管理工具,能自動(dòng)解決軟件包依賴關(guān)系并從遠(yuǎn)程存儲(chǔ)庫(kù)中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過程,需要的朋友可以參考下2023-08-08Java中ArrayBlockingQueue和LinkedBlockingQueue
這篇文章主要介紹了Java中ArrayBlockingQueue和LinkedBlockingQueue,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09Java中System.currentTimeMillis()計(jì)算方式與時(shí)間單位轉(zhuǎn)換講解
本文詳細(xì)講解了Java中System.currentTimeMillis()計(jì)算方式與時(shí)間單位轉(zhuǎn)換,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Java swing實(shí)現(xiàn)酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02快速搭建一個(gè)SpringBoot項(xiàng)目(純小白搭建教程)
本文主要介紹了快速搭建一個(gè)SpringBoot項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11java如何接收和發(fā)送ASCII數(shù)據(jù)
這篇文章主要介紹了java如何接收和發(fā)送ASCII數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09將本地JAR文件手動(dòng)添加到Maven本地倉(cāng)庫(kù)的實(shí)現(xiàn)過程
在Java開發(fā)中,使用Maven作為項(xiàng)目管理工具已經(jīng)成為了主流的選擇,Maven提供了強(qiáng)大的依賴管理功能,可以輕松地下載和管理項(xiàng)目所需的庫(kù)和工具,在某些情況下,你可能會(huì)需要將本地下載的JAR文件手動(dòng)添加到Maven的本地倉(cāng)庫(kù)中,這篇博客將詳細(xì)介紹如何實(shí)現(xiàn)這一過程2024-10-10