這篇文章主要介紹了Struts1之url截取_動力節點Java學院整理的相關資料,需要的朋友可以參考下
##Struts1之url截取
#先我們來對ActionServlet深層進行分析。我們用斷點的調試的方式來看底層源碼。因為這個實例是post方式提交,所以將斷點設定到doPost方法。
我們debug運作程序,進入doPost裡面的方法:
## 重要是ActionServlet運作的核心方法。
我們進入這個方法:
##卷
# 我們赫然發現了這樣一個方法就是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的路徑資訊。
// 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);這裡的方法主要和我的上面的那裡是一樣的,主要就是去掉後綴。
以上是關於Struts1之url截取的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器