深入浅出学习Struts框架(九):分析Struts框架实例4
? ? ? ?前兩篇博客介紹了ActionServlet的初始化,講述了它在初始化的時候做的事情。有了ActionServlet初始化,才能說當我們訪問頁面如何利用digester讀取struts-config.xml配置文件信息,如何講這些信息存到ActionMapping中,如何完成相應的業務處理和頁面跳轉。
?
? ? ? ?那么今天就開始進入這一環節的分析。分析之前,希望大家還是好好看看原先的系列文章,那些文章都是引子,能夠很清晰的認識struts框架大概的運行流程,所以不至于出現分析到底層源碼的時候不好理解的現象。廢話也不多說,現在開始分析。
?
? ? ? 這節課我們開始從截取路徑開始,在《struts框架一》博客中的mvc小實例中就已經寫到了截取路徑了,主要代碼就是:
String requestURI=request.getRequestURI(); System.out.println("request="+requestURI); String path=requestURI.substring(requestURI.indexOf("/",1),requestURI.indexOf(".")); System.out.println("path="+path);
? ? ? 這樣我們就能從一個url中獲得最后的名稱和前面的“/”,也就是/login之類的字符串,之后用if-else判斷就初始化相應的Action,雖然說這僅僅是一個mvc的小實例,但是就是這一簡單的過程其實就是Struts框架截取字符串實現的原理所在(我個人觀點,如果有不同見解,希望溝通交流)。
?
? ? ? 記得前幾篇博客的朋友都知道,我對mvc小實例進行了幾次重構,我把那些if-else中的字符串放到了配置文件中,最終變成了dom4j讀取配置文件,之后動態相匹配通過多肽形式實例化Action,完成相應的業務邏輯和頁面跳轉。有了這個基礎我們來看看struts框架是如何進行截取字符串的,相信大家會有所共鳴。
? ? ? 下面我們來對ActionServlet深層次進行分析。我們用斷點的調試的方式來看底層源碼。因為這個實例是post方式提交,所以將斷點設置到doPost方法上。
?
?
? ? ? 我們debug運行程序,進入doPost里面的方法:
? ??
?
? ? ? ? 這個方法非常重要是ActionServlet運行的核心方法,這個Process處理了非常多的事情,后面的博客會一一介紹。
? ? ? ? 我們進入這個方法:
? ??
? ? ? ?再繼續進入:
? ? ?
? ? ? 我們赫然發現了這樣一個方法就是processPath方法,這個方法就是截取字符串的方法。這個方法的源代碼如下:
/** * <p>Identify and return the path component(from the request URI) that * we will use to select an <code>ActionMapping</code> with which todispatch. * If no such path can be identified,create an error response and return * <code>null</code>.</p> * * @param request The servlet request weare processing * @param response The servlet response weare creating * * @exception IOException if an input/outputerror occurs */ protectedString processPath(HttpServletRequest request, HttpServletResponse response) throws IOException { String path = null; // For prefix matching, match on the path info (if any) path = (String) request.getAttribute(INCLUDE_PATH_INFO); if (path == null) { path = request.getPathInfo(); } if ((path != null) && (path.length() > 0)) { return (path); } // For extension matching, strip the module prefix and extension path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); if (path == null) { path = request.getServletPath(); } String prefix = moduleConfig.getPrefix(); if (!path.startsWith(prefix)) { String msg =getInternal().getMessage("processPath"); log.error(msg + " " + request.getRequestURI()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return null; } path = path.substring(prefix.length()); int slash = path.lastIndexOf("/"); int period = path.lastIndexOf("."); if ((period >= 0) && (period >slash)) { path = path.substring(0, period); } return (path); }
分析一下這段代碼:
?
path = (String)request.getAttribute(INCLUDE_PATH_INFO); if (path == null) { path = request.getPathInfo(); } if ((path != null) && (path.length() > 0)) { return (path); }
? ? ? ? ?這段代碼首先判斷一下javax.servlet.include.path_info是否存在路徑信息,這里要知道當當一個頁面是以RequestDispatcher.include方式顯示的話,這個屬性值才存在。所以這里沒有值,就會進入path =request.getPathInfo()程序中,這里的getPathInfo獲取的值是相對servlet的路徑信息。具體見博客(request.getPathInfo()方法的作用),通過那篇博客的分析,所以這里getPathInfo是獲取不到值的。所以會進入下面的代碼:
// For extension matching, stripthe module prefix and extension path = (String) request.getAttribute(INCLUDE_SERVLET_PATH); if (path == null) { path = request.getServletPath(); } String prefix = moduleConfig.getPrefix(); if (!path.startsWith(prefix)) { String msg =getInternal().getMessage("processPath"); log.error(msg + " " + request.getRequestURI()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return null; }? ? ? ? 這一段代碼是判斷javax.servlet.include.servlet_path是否存在值,這個也是當一個頁面是以equestDispatcher.include方式顯示的話,這個屬性值才存在,所以這里的值沒有。之后進入path = request.getServletPath();這個方法是獲得返回請求URI上下文后的子串,所以這里的返回值就是“/”和訪問頁面名稱和后綴(這里和我的mvc實例截取的是不是一樣的道理)。隨后進入下面代碼:
path = path.substring(prefix.length()); intslash = path.lastIndexOf("/"); intperiod = path.lastIndexOf("."); if((period >= 0) && (period > slash)) { path = path.substring(0, period); } return (path);
? ? ? ?這里的方法主要和我的上面的那里是一樣的,主要就是去掉后綴。
?
? ? ? ?到此為止,截取字符串的工作就算完成了。接下來就是要和action標簽匹配,把相應信息放到ActionMapping中。
隨著分析的深入,這專欄的連載會越來越精彩。敬請關注!
轉載于:https://www.cnblogs.com/springmvc-hibernate/archive/2012/04/10/2483895.html
總結
以上是生活随笔為你收集整理的深入浅出学习Struts框架(九):分析Struts框架实例4的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fedora 16 mysql远程连接
- 下一篇: 软件开发思想之我见