詳解Struts2中的ActionContextCleanUp

    在Struts 2.1.6之前,ActionContextCleanUp的完整路徑是com.opensymphony.webwork.dispatcher.ActionContextCleanUp,現在的路徑變成了org.apache.struts2.dispatcher.ActionContextCleanup。

     那麼這個類究竟有什麼用處呢?是不是一定要用呢?

     下面是這個類內部的註釋。

     Special filter designed to work with the FilterDispatcher and allow
for easier integration with SiteMesh. Normally, ordering your filters to have
SiteMesh go first, and then FilterDispatcher go second is perfectly fine.
However, sometimes you may wish to access Struts features, including the
value stack, from within your SiteMesh decorators. Because FilterDispatcher
cleans up the ActionContext, your decorator won't have access to the
data you want.
     By adding this filter, the FilterDispatcher will know to not clean up and
instead defer cleanup to this filter.
 The ordering of the filters should then be:
1.this filter 

2.SiteMesh filter

3.FilterDispatcher

 

      就是說,一般情況下,如果你要用SiteMesh或者其他過濾器,一般是放在FilterDispatcher或者是現在的StrutsPrepareAndExecuteFilter之前。在調用完所有過濾器的doFilter方法後,核心過濾器FilterDispatcher或者StrutsPrepareAndExecuteFilter會清空ActionContext,如果其他過濾器要一直使用value stack等struts的特性時,如果不用ActionContextCleanUp的話,便得不到想要的值。

 

     ActionContextCleanUp的作用就是上面用粗體標註出來的那一句。它會在doFilter方法裏設置一個計數器counter的初始值爲1,有了這個值,後續的核心過濾器就不會清空ActionContext,而是由之前的過濾器也就是ActionContextCleanUp來清空ActionContext。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章