struts2 優點

 

上午

struts2有以下優點:
1 > Struts2沒有像struts1那樣跟Servlet API和struts API有着緊密的耦合,Struts2的應用可以不依賴於Servlet API和struts API。 Struts2的這種設計屬於無侵入式設計,而Struts1卻屬於侵入式設計。
public class OrderListAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
 }
}
2> Struts2提供了攔截器,利用攔截器可以進行AOP編程,實現如權限攔截等功能。
3> Strut2提供了類型轉換器,我們可以把特殊的請求參數轉換成需要的類型。在Struts1中,如果我們要實現同樣的功能,就必須向Struts1的底層實現BeanUtil註冊類型轉換器纔行。
4> Struts2提供支持多種表現層技術,如:JSP、freeMarker、Velocity等
5> Struts2的輸入校驗可以對指定方法進行校驗,解決了Struts1長久之痛。


搭建Struts2環境時,我們一般需要做以下幾個步驟的工作:
1》找到開發Struts2應用需要使用到的jar文件.
2》編寫Struts2的配置文件
3》在web.xml中加入Struts2 MVC框架啓動配置


struts2.1以後就需要fileupload的jar包


通過包來管理功能相似的模塊


在struts1中,通過<action path=“/test/helloworld”>節點的path屬性指定訪問該action的URL路徑。在struts2中,情況就不是這樣了,

訪問struts2中的action的URL路徑由兩部份組成:包的命名空間+action的名稱,

例如訪問本例子HelloWorldAction的URL路徑爲:/test/helloworld (注意:完整路徑爲:http://localhost:端口/內容路徑/test/helloworld)。另外我們也可以加上.action後綴訪問此Action。


常用的常量定義
<!-- 指定默認編碼集,作用於HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的輸出 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 該屬性指定需要Struts 2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
    如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉 -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 當struts的配置文件修改後,系統是否自動重新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 開發模式下使用,這樣可以打印出更詳細的錯誤信息 -->
    <constant name="struts.devMode" value="true" />
    <!– 與spring集成時,指定由spring負責action對象的創建 -->
    <constant name="struts.objectFactory" value="spring" />
 <!–該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性爲false。 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
 <!--上傳文件的大小限制-->
<constant name="struts.multipart.maxSize" value=“10701096"/>

基於網頁上傳文件大小不要超過5M
否則應考慮開發插件來保證上傳的穩定


下午

struts2對於每一個請求,都會創建一個新的action

struts1:create-->cache

動態方法調用(不推薦使用該方法,建議關閉)
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

使用通配符定義action


自定義類型轉換器


ActionContext:
The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc.


The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. See the ThreadLocal class for more information. The benefit of this is you don't need to worry about a user specific action context, you just get it:
ActionContext context = ActionContext.getContext();
Finally, because of the thread local usage you don't need to worry about making your actions thread safe.

 

ServletActionContext:
Web-specific context information for actions. This class subclasses ActionContext which provides access to things like the action name, value stack, etc. This class adds access to web objects like servlet parameters, request attributes and things like the HTTP session.

文件上傳
FileUtils工具類

自定義攔截器

攔截器棧

因爲struts2中如文件上傳,數據驗證,封裝請求參數到action等功能都是由系統默認的defaultStack中的攔截器實現的,所以我們定義的攔截器需要引用系統默認的defaultStack,這樣應用纔可以使用struts2框架提供的衆多功能。

如果希望包下的所有action都使用自定義的攔截器,可以通過<default-interceptor-ref name=“permissionStack”/>把攔截器定義爲默認攔截器。注意:每個包只能指定一個默認攔截器。另外,一旦我們爲該包中的某個 action顯式指定了某個攔截器,則默認攔截器不會起作用。

 

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