Struts2  快速入門(三)

動態方法調用(DMI):
動態方法調用(DMI): dynamic_method_invocation。 功能:在一個Action中處理多個功能,從而減少系統Action的數量。 DMI 開發過程: a Action extends ActionSupport。 b Action提供多個方法完成不同的功能方法的簽名(修飾符,返回值,參數表) 和execute方法一致,方法名字可以隨意書寫。 c struts.xml 配置文件。
DMI 開發第一種方式(不推薦):在struts.xml中的action標籤中添加method屬性。 <action name="add" class="day5.DMIAction" method="addUser"> <result name="success">/day3/ok.jsp</result> </action>
DMI 開發第二種方式:在struts.xml中的action標籤中使用通配符。 <action name="user_*" method="{1}" class="day5.DMIAction" > <result name="success">/day3/ok.jsp</result> </action> DMI 開發第三種方式:在jsp頁面中使用!的形式發出請求。

 

<a href=" user!add">添加用戶</a> <a href=" user!add.action">添加用戶</a> user爲請求名,add爲動態調用action中的方法名,注意:請求後綴要寫在最後。

、<result>標籤、自定義結果類型、json結果類型
1.Struts2結果類型內置(可以參考struts-default.xml): <action> <result name="" type="dispatcher" ----- forward jsp <result name="" type="redirect" ----- redirect jsp <result name="" type="chain" ----- Action forward Action <result name="" type="redirectAction" ----- Action redirect Action </action> 2.自定義結果類型 a類 implmenets Result實現execute()方法; b配置struts.xml: <package> <result-types> <result-type name="pdf" class=""/> </result-types> <action> <result type="pdf"/> </action> </package> 3.Json的Result響應類型的使用 a. 引入struts2-json-plugin.jar b. struts.xml配置<package extends="jso

c. result響應使用 <result name="success" type="json"></result> d. 如果action中的屬性不需要返回: 可以在get方法前使用@JSON(serialize=false)註解。

 

7、struts.xml中其他的配置:
1.全局的異常配置: <global-exception-mappings> <exception-mapping exception="xxxException" result="error"/> <global-exception-mappings> 2.局部(action中)的異常配置: <action name="lg" class="day3.LoginAction"> <exception-mapping result="xxx" exception="xxxException"/> <result name="success">/loginOk.jsp</result> <result name="error">/loginError.jsp</result>

</action> 3. 全局的result配置: <package ....> <global-results> <result name="message">/message.jsp</result> </global-results> </package> 4.常量配置(參考struts2.core.jar中的struts-default.xml文件編寫): 同時這些文件也可以寫在struts.properties文件中,放到src下: A.<constant name="struts.action.extension" value="do"/> <!--該屬性指定需要Struts 2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。--> B.<constant name="struts.custom.i18n.resources" value="appRes"/> <!--配置國際化文件。--> C.<constant name="struts.i18n.encoding" value="uft-8"/> <!--指定默認編碼集,作用於HttpServletRequest的setCharacterEncoding方法和freemarker 、velocity的輸出--> D. <constant name="struts.serve.static.browserCache" value="false"/> <!--設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉--> E.<constant name="struts.configuration.xml.reload" value="true"/> <!--當struts的配置文件修改後,系統是否自動重新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開--> F.<constant name="struts.devMode" value="true" /> <!--開發模式下使用,這樣可以打印出更詳細的錯誤信息--> G.<constant name="struts.enable.DynamicMethodInvocation" value="false"/> <!–該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性爲false。--> H.<constant name="struts.multipart.maxSize" value=“10701096"/> <!--上傳文件的大小限制-->
8、Struts2的中文亂碼解決:
中文亂碼問題一般是指當請求參數有中文時,無法在Action中得到正確的中文。Struts2中有2種辦法可以解決這個問題: 1、設置JSP頁面的pageEncoding=”utf-8”,就不會出現中文亂碼; 2、如果JSP頁面的pageEncoding=”GBK”,那麼需要修改struts.i18n.encoding=GBK,在struts.xml中加入如下語句進行修改。
<struts> <constant name="struts.i18n.encoding" value="GBK"/> …… </struts>
上面2種方法可以解決POST請求中的中文參數,但是GET請求中的中文參數不能解決,
 
GET請求中的中文參數的亂碼需要通過修改Tomcat的server.xml文件來解決,修改如下內容,加入URIEncoding=”GBK”:
<Connector port="8080" …… URIEncoding="GBK"/>
如果還沒有把亂碼問題解決,還可以加一個過濾器(Filter)在doFilter()方法中寫如下代碼: HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); arg2.doFilter(arg0, arg1); 然後,注意要在web.xml最上端配置這個過濾器。

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