天天看點

Struts2的處理流程和對Action的管理方式

首先我們先來看一張我畫的草圖,如下:

<a href="http://s3.51cto.com/wyfs02/M02/3F/8E/wKioL1PLLkeyV34vAADCmZwYEYQ602.jpg" target="_blank"></a>

接下來,是一個web.xml配置檔案的常用代碼

1

2

3

4

5

6

7

8

<code>&lt;</code><code>filter</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>filter-name</code><code>&gt;struts2&lt;/</code><code>filter-name</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>filter-class</code><code>&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/</code><code>filter-class</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>filter</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>filter-mapping</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>url-pattern</code><code>&gt;/*&lt;/</code><code>url-pattern</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>filter-mapping</code><code>&gt;</code>

ok,

<code>StrutsPrepareAndExecuteFilter</code>

    是Struts2核心控制器,它隻負責攔截由&lt;url-pattern&gt;/*&lt;/url-pattern&gt;指定的所有使用者的請求

當使用者請求到達時,系統會根據web.xml中配置Filter過濾使用者的請求,預設情況下,如果使用者的通路路徑不帶有字尾或者是以.action的字尾作為結尾,

這時候請求會轉發給Struts2架構處理,否則Struts2會濾過這個請求處理,當請求轉入Struts2架構時會先經過一系列的攔截器,然後再到Action。

    與Struts1不同的是,Struts2會對每個請求都會産生一個Action,是以Struts2是線程安全的,屬于原型設計模式。

Struts1中的Action建立出來之後會放在緩沖中,屬于單例設計模式,在應用生命周期之内隻會存在一個Action。

    在面試過程中,經常有面試官會問到struts2和struts1之間的Action的管理會有什麼差別?如上!

本文轉自 小夜的傳說 51CTO部落格,原文連結:http://blog.51cto.com/1936625305/1440453,如需轉載請自行聯系原作者

繼續閱讀