struts2.1.3以後使用自定義Filter

struts2中html

ActionContextCleanUp,java

StrutsPrepareAndExecuteFilter,web

StrutsPrepareFilter,StrutsExecuteFilter,數據庫

FilterDispatcherdapache

 

剛看了文檔中的描述(說的很清楚了,仍是文檔好啊):session

org.apache.struts2.dispatcherapp

Class FilterDispatcherjsp

Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using theActionContextCleanUp filter in addition to this one網站

 

org.apache.struts2.dispatcher 
Class ActionContextCleanUpui

Deprecated. Since Struts 2.1.3, use StrutsPrepareFilter and StrutsExecuteFilter to use other Servlet filters that need access to the ActionContext

 

org.apache.struts2.dispatcher.ng.filter 
Class StrutsPrepareAndExecuteFilter

Handles both the preparation and execution phases of the Struts dispatching process. This filter is better to use when you don't have another filter that needs access to action context information, such as Sitemesh.

 

org.apache.struts2.dispatcher.ng.filter 
Class StrutsPrepareFilter

Prepares the request for execution by a later StrutsExecuteFilter filter instance.

 

org.apache.struts2.dispatcher.ng.filter 
Class StrutsExecuteFilter

Executes the discovered request information. This filter requires the StrutsPrepareFilter to have already been executed in the current chain.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

FilterDispatcher是struts2.0.x到2.1.2版本的核心過濾器.
StrutsPrepareAndExecuteFilter是自2.1.3開始就替代了FilterDispatcher的.

StrutsPrepareAndExecuteFilter是StrutsPrepareFilter和StrutsExecuteFilter的組合,那何時用StrutsPrepareFilter和StrutsExecuteFilter的組合來替代StrutsPrepareAndExecuteFilter呢?下面會講到。

這樣的改革固然是有好處的.
爲何這麼說.? 應該知道若是咱們本身定義過濾器的話, 是要放在strtus2的過濾器以前的, 若是放在struts2過濾器以後,你本身的過濾器對action的過濾做用就廢了,不會有效!除非你是訪問jsp/html! 
那我如今有需求, 我必須使用Action的環境,而又想在執行action以前拿filter作一些事, 用FilterDispatcher是作不到的.! 
那麼StrutsPrepareAndExecuteFilter能夠把他拆分紅StrutsPrepareFilter和StrutsExecuteFilter,能夠在這兩個過濾器之間加上咱們本身的過濾器.! 
給你打個比喻, 如今有病人要作手術, 如今struts2要作兩件事, 搭病牀(環境),執行手術.! 那麼打麻藥的工做呢.? 不可能要病人站着打吧, 因此必須有病牀的環境,打完麻藥以後再動手術.! 這個比喻很是形象了.!

 

 

ActionContextCleanUp也是自2.1.3開始由StrutsPrepareFilter和StrutsExecuteFilter的組合替代。

看個例子:

2.1.3以前用sitemesh的web.xml的配置:

<filter>  
   <filter-name>ActionContextCleanUp</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
</filter>  
<filter>  
   <filter-name>sitemesh</filter-name>  
   <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>  
</filter>  
<filter>  
   <filter-name>struts2</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
</filter>  
<filter-mapping>  
   <filter-name>ActionContextCleanUp</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
   <filter-name>sitemesh</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
   <filter-name>struts2</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>


其中ActionContextCleanUp filter
 SiteMesh filter
 FilterDispatcher
的順序不能改變。

 ActionContextCleanUp的做用是延長action中屬性的生命週期,包括自定義屬性,以便在jsp頁面中進行訪問,讓actionContextcleanup過濾器來清除屬性,不讓action本身清除。

 爲了使用WebWork,咱們只須要在web.xml配置FilterDispatcher一個過濾器便可,閱讀一下FilterDispatcher的JavaDoc和源碼,咱們能夠看到它調用了:

finally{
           ActionContextCleanUp.cleanUp(req);
}

在ActionContextCleanUp中,有這樣的代碼:

         req.setAttribute(CLEANUP_PRESENT, Boolean.TRUE);

若是FilterDispatcher檢測到這個屬性,就不會清除ActionContext中的內容了,而由ActionContextCleanUp後續的代碼來清除,保證了一系列的Filter訪問正確的ActionContext。

 

自Struts2是2.1.3版本開始,死活不得行,老是報錯。原來新版本中已經不能再採用這種方式了。
【從struts2.1.3開始ActionContextCleanUp 和 FilterDispatcher過濾器,已經不建議使用了。
   將使用StrutsPrepareFilter和StrutsExecuteFilter攔截器替代】
而從Struts2.1.3開始應該改成如下配置才能集成起來,要否則你的裝飾器頁面或者被裝飾頁面

裏面將不能使用Struts2的標籤。一使用就會報錯說什麼the Struts Dispatcher not found

<filter>  
   <filter-name>StrutsPrepareFilter</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>  
</filter>  
<filter>  
   <filter-name>sitemesh</filter-name>  
   <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>  
</filter>  
<filter>  
   <filter-name>StrutsExecuteFilter</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>  
</filter>  
<filter-mapping>  
   <filter-name>StrutsPrepareFilter</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
   <filter-name>sitemesh</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
   <filter-name>StrutsExecuteFilter</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>


StrutsPrepareFilter,sitemesh,StrutsExecuteFilter的順序配置過濾器,sitemesh夾在中間。

經過以上配置能正確使用。 


java 自定義 Filter 例

使用Filter分爲兩部分:編寫Filter和配置Filter。

1.編寫Filter:

   1)實現 javax.servlet.Filter 接口

   2)實現接口中方法。

public void init(FilterConfig config) throws ServletException ,

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,

FilterChain filterchain) throws IOException, ServletException ,

public void destroy()  三個方法。

 

其中,在doFilter中,filterchain.doFilter(servletRequest, servletResponse);用於繼續執行原有請求的活動。

好比你訪問了網站index.jsp頁面,但願在訪問這個頁面前對數據庫進行查詢來初始化這個頁面,那麼能夠在doFilter方法中,編寫數據庫查詢的代碼。可是以後必定要寫filterchain.doFilter(servletRequest, servletResponse);這樣頁面纔會在執行完數據庫查詢後跳轉至index.jsp。即在filter後繼續執行原有的請求。

 

注意:在doFilter的參數中的request和response參數爲ServletRequest 和ServletResponse,如需獲取session,可將它們轉換爲HttpServletRequest和HttpServletResponse。再經過request.getSession()獲取HttpSession。

 

代碼例子:

import java.io.IOException;  
import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
  
public class UserroleFilter implements Filter{  
      
    public void destroy() {  
          
    }  
      
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,  
            FilterChain filterchain) throws IOException, ServletException {  
      
        HttpServletResponse response = (HttpServletResponse) servletResponse;  
        HttpServletRequest request = (HttpServletRequest)servletRequest;  
        HttpSession session = request.getSession();  
        //須要過濾的代碼         
        filterchain.doFilter(servletRequest, servletResponse);  
    }  
      
    public void init(FilterConfig config) throws ServletException {  
          
    }  
}

2.配置Filter:

在web.xml中配置哪些請求須要使用過濾器進行過濾。

<filter>  
    <filter-name>selfTestFilter</filter-name>  
    <filter-class>com.maggie.filter.SelfTestFilter</filter-class>  
    </filter>  
<filter-mapping>  
    <filter-name>selfTestFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>
相關文章
相關標籤/搜索