首頁  >  文章  >  Java  >  關於Struts1之url截取的詳細介紹

關於Struts1之url截取的詳細介紹

黄舟
黄舟原創
2018-05-24 15:19:261667瀏覽

這篇文章主要介紹了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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn