Struts1之url截取_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Struts1之url截取
先我們來對(duì)ActionServlet深層次進(jìn)行分析。我們用斷點(diǎn)的調(diào)試的方式來看底層源碼。因?yàn)檫@個(gè)實(shí)例是post方式提交,所以將斷點(diǎn)設(shè)置到doPost方法上。
我們debug運(yùn)行程序,進(jìn)入doPost里面的方法:
這個(gè)方法非常重要是ActionServlet運(yùn)行的核心方法。
我們進(jìn)入這個(gè)方法:
再繼續(xù)進(jìn)入:
我們赫然發(fā)現(xiàn)了這樣一個(gè)方法就是processPath方法,這個(gè)方法就是截取字符串的方法。這個(gè)方法的源代碼如下:
/** * <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是否存在路徑信息,這里要知道當(dāng)當(dāng)一個(gè)頁面是以RequestDispatcher.include方式顯示的話,這個(gè)屬性值才存在。所以這里沒有值,就會(huì)進(jìn)入path=request.getPathInfo()程序中,這里的getPathInfo獲取的值是相對(duì)servlet的路徑信息。
// 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是否存在值,這個(gè)也是當(dāng)一個(gè)頁面是以equestDispatcher.include方式顯示的話,這個(gè)屬性值才存在,所以這里的值沒有。之后進(jìn)入path = request.getServletPath();這個(gè)方法是獲得返回請求URI上下文后的子串,所以這里的返回值就是“/”和訪問頁面名稱和后綴(這里和我的mvc實(shí)例截取的是不是一樣的道理)。隨后進(jìn)入下面代碼:
path = path.substring(prefix.length()); intslash = path.lastIndexOf("/"); intperiod = path.lastIndexOf("."); if((period >= 0) && (period > slash)) { path = path.substring(0, period); } return (path);
這里的方法主要和我的上面的那里是一樣的,主要就是去掉后綴。
- Java框架Struts2實(shí)現(xiàn)圖片上傳功能
- Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a
- struts2標(biāo)簽總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- struts1之簡單mvc示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- struts1之ActionServlet詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Struts1教程之ActionMapping_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- java struts2框架簡介
- Java struts2 package元素配置及實(shí)例解析
相關(guān)文章
使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法
在使用IDEA開發(fā)maven+springMVC項(xiàng)目時(shí)遇到不加載EL表達(dá)式的問題,遇到此問題的朋友不在少數(shù),今天小編給大家?guī)砹耸褂肐DEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法,一起看看吧2018-08-08使用JSP實(shí)現(xiàn)簡單的用戶登錄注冊頁面示例代碼解析
這篇文章主要介紹了使用JSP實(shí)現(xiàn)簡單的用戶登錄注冊頁面示例代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08解決request.getParameter取值后的if判斷為NULL的問題
這篇文章主要介紹了解決request.getParameter取值后的if判斷為NULL的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
web.xml讀取連接數(shù)據(jù)庫的參數(shù),實(shí)現(xiàn)代碼。2009-05-05JSP response對(duì)象實(shí)現(xiàn)文件下載的兩種方式
這篇文章主要介紹了JSP隱含對(duì)象response實(shí)現(xiàn)文件下載的兩種方法,很簡單,但很實(shí)用,需要的朋友可以參考下2014-09-09Spring 中 @Service 和 @Resource 注解的區(qū)別
這篇文章主要介紹了Spring @Service 和 @Resource 注解的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-03-03JAVA/JSP學(xué)習(xí)系列之一(JDK安裝)
JAVA/JSP學(xué)習(xí)系列之一(JDK安裝)...2006-10-10