Struts2學習筆記(五):Action屬性注入值和指定Struts 2處理的請求

來源:互聯網
上載者:User

Struts2為Action中的屬性提供了依賴注入功能,在struts2的設定檔中,我們可以很方便地為Action中的屬性注入值。注意:屬性必須提供setter方法。

 

 1 public class HelloWorldAction{
2 private String savePath;
3
4 public String getSavePath() {
5 return savePath;
6 }
7 public void setSavePath(String savePath) {
8 this.savePath = savePath;
9 }
10 ......
11 }
 
<package name="jim" namespace="/test" extends="struts-default">
<action name="helloworld" class="com.jim.action.HelloWorldAction" >
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
上面通過<param>節點為action的savePath屬性注入“/images” 

 

前面我們都是預設使用.action尾碼訪問Action。其實預設尾碼是可以通過常量”struts.action.extension“進行修改的,例如:我們可以配置Struts 2隻處理以.do為尾碼的請求路徑:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
 如果使用者需要指定多個請求尾碼,則多個尾碼之間以英文逗號(,)隔開。如: <constant name="struts.action.extension" value="do,go"/> 常量可以在struts.xml或struts.properties中配置,建議在struts.xml中配置,兩種配置方式如下: struts.xml 檔案中配置常量 <struts>    <constant name="struts.action.extension" value="do"/></struts>  struts.properties 中配置常量 struts.action.extension=do 因為常量可以在下面多個設定檔中進行定義,所以我們需要瞭解struts2載入常量的搜尋順序:struts-default.xmlstruts-plugin.xmlstruts.xmlstruts.propertiesweb.xml如果在多個檔案中配置了同一個常量,則後一個檔案中配置的常量值會覆蓋前面檔案中配置的常量值. 常用的常量介紹<!-- 指定預設編碼集,作用於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" /> <!-- 預設的視圖主題 --><constant name="struts.ui.theme" value="simple" /> <!– 與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"/>    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.