struts.xml的常用配置

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> 3 <struts> 4 <!-- 所有匹配*.action的請求都由struts2處理 --> 5 <constant name="struts.action.extension" value="action" /> 6 <!-- 是否啟用開發模式 --> 7 <constant name="struts.devMode" value="true" /> 8 <!-- struts配置文件改動后,是否重新加載 --> 9 <constant name="struts.configuration.xml.reload" value="true" /> 10 <!-- 設置瀏覽器是否緩存靜態內容 --> 11 <constant name="struts.serve.static.browserCache" value="false" /> 12 <!-- 請求參數的編碼方式 --> 13 <constant name="struts.i18n.encoding" value="utf-8" /> 14 <!-- 每次HTTP請求系統都重新加載資源文件,有助於開發 --> 15 <constant name="struts.i18n.reload" value="true" /> 16 <!-- 文件上傳最大值 --> 17 <constant name="struts.multipart.maxSize" value="104857600" /> 18 <!-- 讓struts2支持動態方法調用 --> 19 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 20 <!-- Action名稱中是否還是用斜線 --> 21 <constant name="struts.enable.SlashesInActionNames" value="false" /> 22 <!-- 允許標簽中使用表達式語法 --> 23 <constant name="struts.tag.altSyntax" value="true" /> 24 <!-- 對於WebLogic,Orion,OC4J此屬性應該設置成true --> 25 <constant name="struts.dispatcher.parametersWorkaround" value="false" /> 26 27 <package name="basePackage" extends="struts-default"> 28 29 30 </package> 31 32 </struts>

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > 3 <struts> 4 5 <!-- include節點是struts2中組件化的方式 可以將每個功能模塊獨立到一個xml配置文件中 然后用include節點引用 --> 6 <include file="struts-default.xml"></include> 7 8 9 <!-- package提供了將多個Action組織為一個模塊的方式 10 package的名字必須是唯一的 package可以擴展 當一個package擴展自 11 另一個package時該package會在本身配置的基礎上加入擴展的package 12 的配置 父package必須在子package前配置 13 name:package名稱 14 extends:繼承的父package名稱 15 abstract:設置package的屬性為抽象的 抽象的package不能定義action 值true:false 16 namespace:定義package命名空間 該命名空間影響到url的地址,例如此命名空間為/test那么訪問是的地址為http://localhost:8080/struts2/test/XX.action 17 --> 18 <package name="com.kay.struts2" extends="struts-default" namespace="/test"> 19 <interceptors> 20 <!-- 定義攔截器 21 name:攔截器名稱 22 class:攔截器類路徑 23 --> 24 <interceptor name="timer" class="com.kay.timer"></interceptor> 25 <interceptor name="logger" class="com.kay.logger"></interceptor> 26 <!-- 定義攔截器棧 --> 27 <interceptor-stack name="mystack"> 28 <interceptor-ref name="timer"></interceptor-ref> 29 <interceptor-ref name="logger"></interceptor-ref> 30 </interceptor-stack> 31 </interceptors> 32 33 <!-- 定義默認的攔截器 每個Action都會自動引用 34 如果Action中引用了其它的攔截器 默認的攔截器將無效 --> 35 <default-interceptor-ref name="mystack"></default-interceptor-ref> 36 37 38 <!-- 全局results配置 --> 39 <global-results> 40 <result name="input">/error.jsp</result> 41 </global-results> 42 43 <!-- Action配置 一個Action可以被多次映射(只要action配置中的name不同) 44 name:action名稱 45 class: 對應的類的路徑 46 method: 調用Action中的方法名 47 --> 48 <action name="hello" class="com.kay.struts2.Action.LoginAction"> 49 <!-- 引用攔截器 50 name:攔截器名稱或攔截器棧名稱 51 --> 52 <interceptor-ref name="timer"></interceptor-ref> 53 54 <!-- 節點配置 55 name : result名稱 和Action中返回的值相同 56 type : result類型 不寫則選用superpackage的type struts-default.xml中的默認為dispatcher 57 --> 58 <result name="success" type="dispatcher">/talk.jsp</result> 59 <!-- 參數設置 60 name:對應Action中的get/set方法 61 --> 62 <param name="url">http://www.sina.com</param> 63 </action> 64 </package> 65 </struts>
一個Action內包含多個請求處理方法的處理
Struts1提供了DispatchAction,從而允許一個Action內包含多個請求處理方法。Struts2也提供了類似的功能。
處理方式主要有以下三種方式:
1. 1 動態方法調用:
1 <!-- 動態方法調用HTML標簽與Struts2標簽 --> 2 <form action="computeAction!add.action" name="from" > 3 <s:form action="computeAction!add.action" name="form" theme="simple" >
則用戶的請求將提交到名為”computeAction”的Action實例,Action實例將調用名為”add”方法來處理請求。
當指定調用某一方法來處理請求時,就不會走默認執行處理請求的execute()方法。
注意:要使用動態方法調用,必須設置Struts2允許動態方法調用,通過設置struts.enable.DynamicMethodInvocation常量來完成,該常量屬性的默認值是true。
1 <struts> 2 <!-- 3 //禁用動態方法調用,默認為true啟用,false禁用 4 constant:name="struts.enable.DynamicMethodInvocation" 5 --> 6 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 7 </struts>
示列:簡單的一個加法和減法例子。
1. index.jsp用戶在頁面輸入兩個數字,選擇相加,或者相減
當用戶點擊加或減需要走同一個Action但處理請求方法不同,這里使用了js動態選擇。
1 <body> 2 <!-- 動態方法調用 使用:Struts2標簽也可以使用HTML標簽 --> 3 <s: name="form" theme="simple" > 4 5 num1:<s:textfield name="num1" /> 6 num2:<s:textfield name="num2" /> 7 <s:submit type="button" value="加" onclick="computeMethod('add')" /> 8 <s:submit type="button" value="減" onclick="computeMethod('subtract')" /> 9 </s:form> 10 11 12 13 <!-- js --> 14 <script type="text/javascript"> 15 function computeMethod(op){ 16 document.form.action="computeAction!"+op;//動態選擇處理請求的方法 17 document.form.submit();//提交 18 } 19 </script> 20 21 </body>
2. struts.xml配置信息,啟用動態方法調用(可選)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <!-- 5 //禁用動態方法調用,默認為true啟用,false禁用 6 constant:name="struts.enable.DynamicMethodInvocation" 7 --> 8 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 9 <package name="struts2" extends="struts-default"> 10 <action name="computeAction" class="com.struts.ComputeAction" > 11 <result name="fruitPage" >/fruit.jsp</result> 12 </action> 13 </package> 14 </struts>
3. ComputeAction控制器的類處理請求
1 package com.struts; 2 /** 3 * Struts2控制器的類 4 * @author asus 5 * 6 */ 7 public class ComputeAction { 8 9 /** 屬性 */ 10 private int num1; 11 private int num2; 12 private int fruit;//結果 13 14 /** 若請求為指定操作方法默認執行execute()方法 */ 15 public String execute(){ 16 17 System.out.println("當調用其它方法就不會走這個方法!"); 18 return ""; 19 } 20 21 /** 執行處理加法 */ 22 public String add(){ 23 this.fruit=num1+num2;//加 24 return "fruitPage"; 25 } 26 27 /** 執行處理減法 */ 28 public String subtract(){ 29 this.fruit=num1-num2;//減 30 return "fruitPage"; 31 } 32 33 34 35 36 /** JavaBean */ 37 public int getNum1() { 38 return num1; 39 } 40 41 public void setNum1(int num1) { 42 this.num1 = num1; 43 } 44 45 public int getNum2() { 46 return num2; 47 } 48 49 public void setNum2(int num2) { 50 this.num2 = num2; 51 } 52 53 public int getFruit() { 54 return fruit; 55 } 56 57 public void setFruit(int fruit) { 58 this.fruit = fruit; 59 } 60 61 }
4. fruit.jsp響應結果的頁面
1 <body> 2 <!-- 結果頁面 --> 3 計算結果:<s:property value="fruit" /> 4 </body>
1.2Action配置method屬性(示列與以上代碼大多一致,只修改有變更的):
將Action類中的每一個處理方法都定義成一個邏輯Action方法。
1. index.jsp頁面
1 <body> 2 3 <!-- Action配置method屬性 使用:Struts2標簽也可以使用HTML標簽 --> 4 <s:form name="form" theme="simple" > 5 6 num1:<s:textfield name="num1" /> 7 num2:<s:textfield name="num2" /> 8 <s:submit type="button" value="加" onclick="computeMethod('addAction')" /> 9 <s:submit type="button" value="減" onclick="computeMethod('subtractAction')" /> 10 </s:form> 11 12 13 14 <!-- js --> 15 <script type="text/javascript"> 16 function computeMethod(op){ 17 document.form.action=op;//動態選擇處理請求的方法 18 document.form.submit();//提交 19 } 20 21 </script> 22 23 </body>
2. struts.xml配置信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <package name="struts2" extends="struts-default"> 5 <action name="addAction" class="com.struts.ComputeAction" method="add" > 6 <result name="fruitPage" >/fruit.jsp</result> 7 </action> 8 <action name="subtractAction" class="com.struts.ComputeAction" method="subtract" > 9 <result name="fruitPage" >/fruit.jsp</result> 10 </action> 11 </package> 12 </struts>
通過action元素的method屬性來指定Action執行時調用的方法。
優點:使得以更加安全的方式來實現動態方法的調用,不讓別人看到你的實現方法。
缺點:繁瑣,一個處理請求的方法要跟一個action。
Struts2根據method屬性查找方法有兩種途徑:
使用動態方法調用和method屬性的區別:
1.通過以上三個struts.xml中的配置信息例子來說,他們的共同點是都在操作同一個Action。
2.<form action="">中請求地址不同。
3.動態方法的返回值相同,則會通過result進入一個頁面。而method屬性就算兩個方法的返回值相同但進去不同的result,可能會進入兩個不同的頁面。
由上可以分析出:
(1)如果使用同一個Action,不同的處理請求的方法,響應使用相同的配置(result等)則使用動態方法調用。
(2)如果使用同一個Action,不同的處理請求的方法,響應分別使用不同的配置,則使用action元素的method屬性,為同一個Action配置多個名稱。
1.3使用通配符映射(wildcard mappings)方式(示列與以上代碼大多一致,只修改有變更的):
1. index.jsp頁面只改動了js部分。
1 <body> 2 3 <!-- 使用通配符映射(wildcard mappings)方式 使用:Struts2標簽也可以使用HTML標簽 --> 4 <s:form name="form" theme="simple" > 5 6 num1:<s:textfield name="num1" /> 7 num2:<s:textfield name="num2" /> 8 <s:submit type="button" value="加" onclick="computeMethod('addAction')" /> 9 <s:submit type="button" value="減" onclick="computeMethod('subtractAction')" /> 10 </s:form> 11 12 13 14 <!-- js --> 15 <script type="text/javascript"> 16 function computeMethod(op){ 17 document.form.action=op;//相比mothod屬性改動只有這里 18 document.form.submit();//提交 19 } 20 21 </script> 22 23 </body>
2.struts.xml的配置信息
在使用method屬性來實現同一個Action的不同方法處理不同的請求時,會發現,隨着方法的增多,從而導致大量的Action配置,這時我們就需要通過使用通配符來解決Action配置過多的方法。
在配置<action.../>元素時,需要指定name、class、method屬性。其中name屬性可支持通配符,然后可以在class、method屬性中使用表達式。通配符用星號 * 表示。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <package name="struts2" extends="struts-default"> 5 6 <action name="*Action" class="com.struts.ComputeAction" method="{1}" > 7 <result name="fruitPage" >/fruit.jsp</result> 8 <!-- <result name="fruitPage" >/{1}.jsp</result>表達式也可以寫在這里 --> 9 </action> 10 </package> 11 </struts>
實際上這一點從原理上來講可以理解,default-action-ref這個配置的意思是當用戶在點擊了沒有定義的action時,如果struts沒有找到用戶定義的action名稱,則會自動跳轉到該默認定義的action中。
個人覺得地址欄中項目后不寫名稱和名稱不存在是兩個概念。
示列:
1. struts.xml 就在通配符例子中配置上默認Action
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <package name="struts2" extends="struts-default"> 5 <!-- 配置默認Action --> 6 <default-action-ref name="defaultAction"></default-action-ref> 7 <action name="defaultAction"> 8 <result>/error.jsp</result> 9 </action> 10 11 <!-- 通配符映射(wildcard mappings) --> 12 <action name="*Action" class="com.struts.ComputeAction" method="{1}" > 13 <result name="fruitPage" >/fruit.jsp</result> 14 <!-- <result name="fruitPage" >/{1}.jsp</result>表達式也可以寫在這里 --> 15 </action> 16 </package> 17 </struts>
2. index.jsp頁面 這里我們把提交的url :Action地址鏈接,寫錯打斷,當提交時找不到對應的Action,則會進入默認Action,進入error.jsp頁面
1 <body> 2 3 <!-- 使用通配符映射(wildcard mappings)方式 使用:Struts2標簽也可以使用HTML標簽 --> 4 <s:form name="form" theme="simple" > 5 6 num1:<s:textfield name="num1" /> 7 num2:<s:textfield name="num2" /> <!-- 測試默認Action,當提交的Action地址錯誤。則會走默認Action --> 8 <s:submit type="button" value="加" onclick="computeMethod('ssss')" /><!-- 把動態url地址亂寫 --> 9 <s:submit type="button" value="減" onclick="computeMethod('subtractActios')" /><!-- 或在url地址中多加字符 --> 10 </s:form> 11 12 13 14 <!-- js --> 15 <script type="text/javascript"> 16 function computeMethod(op){ 17 document.form.action=op;//改動只有這里 18 document.form.submit();//提交 19 } 20 21 </script> 22 23 </body>
3. error.jsp 創建此頁面查看效果
1 <body> 2 錯誤頁面。! 3 未找到,Action實例時會默認走此頁面! 4 </body>
3.處理結果
Struts2的Action處理完用戶請求后,將返回一個普通字符串,整個普通字符串就是一個邏輯視圖名。Struts2通過配置邏輯視圖名和物理視圖資源之間的映射關系,一旦系統收到Action返回的某個邏輯視圖名,系統就會把對應的物理視圖資源呈現給瀏覽者。
1 <!-- 全局結果可滿足一個包中多個Action共享一個結果,也就是說,當多個Action中都有一個重復的result時就可以使用全局結果,也就是說公共的result --> 2 <global-results> 3 <result name="fruitPage" type="dispatcher" >/fruit.jsp</result> 4 </global-results>
名字 | 說明 |
chain | 用來處理Action鏈 |
dispatcher | 用來轉向頁面,通常處理JSP,這是默認的結果類型 |
freeMarker | 處理FreeMarker模板 |
httpHeader | 用來控制特殊的Http行為 |
redirect | 重定向到一個URL |
redirect-action | 重定向到一個Action |
stream | 向瀏覽器發送InputSream對象,通常用來處理文件下載 |
velocity | 處理Velocity模板 |
xslt | 處理XML/XLST模板 |
plaintext | 顯示原始文件內容,例如文件源代碼 |
tiles | 結合Tile使用 |
另外第三方的Result類型還包括JasperReports Plugin,專門用來處理JasperReport類型的報表輸出;Jfreechart Plugin;JSF Plugin。
常用示列:
1.struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > 3 <struts> 4 <package name="struts2" extends="struts-default"> 5 6 <!-- 默認Action --> 7 <default-action-ref name="defaultAction"></default-action-ref> 8 9 <!-- 全局結果可滿足一個包中多個Action共享一個結果,也就是說,當多個Action中都有一個重復的result時就可以使用全局結果,也就是說公共的result 10 <global-results> 11 <result name="fruitPage" type="dispatcher" >/fruit.jsp</result> 12 </global-results> --> 13 14 <action name="defaultAction"> 15 <result>/error.jsp</result> 16 </action> 17 <!-- 通配符映射(wildcard mappings) --> 18 <action name="*Action" class="com.struts.ComputeAction" method="{1}" > 19 <!--1 20 表達式{1}也可以寫在url連接中,class,name中都可以寫,也可以寫多少,索引從1開始 21 <result name="fruitPage" >/{1}.jsp</result> --> 22 23 <!--2 24 默認dispatcher轉發跳轉 25 <result name="fruitPage" type="dispatcher" >/fruit.jsp</result> --> 26 27 <!--3 28 重定向跳轉 29 <result name="fruitPage" type="redirect" >/fruit.jsp</result> --> 30 31 <!--4 32 redirectAction: Action實例 與另一個Action實例互相跳轉 33 <result name="fruitPage" type="redirectAction" >skipAction</result> --> 34 35 <!--4.1 36 使用感嘆號指定跳轉方法,xml會顯示報錯,但可以用。 使用?&不能在Action實例中帶參數 37 <result name="fruitPage" type="redirectAction" >skipAction!add</result> --> 38 39 <!--4.2 40 跳轉Action帶參數的方式: 41 actionName:跳轉Action的名稱 42 method:跳轉Action實例中的哪個方法 43 num:帶的參數,可寫固定,可使用${屬性名}取上一個Action實例中的屬性,實現動態傳值。 44 --> 45 <result name="fruitPage" type="redirectAction" > 46 <param name="actionName">skipAction</param> 47 <param name="method">add</param> 48 <param name="num1">${num1}</param> 49 <param name="num2">${num2}</param> 50 </result> 51 </action> 52 53 <action name="skipAction" class="com.struts.SkipAction" > 54 <result name="success" type="dispatcher" >/fruit.jsp</result> 55 </action> 56 </package> 57 </struts>
2.再創建一個SkipAction 控制器的類
1 package com.struts; 2 /** 3 * 測試與dispatcherAction之間的傳值 4 * @author asus 5 * 6 */ 7 public class SkipAction { 8 9 /** 接收computeAction實例傳過來屬性 */ 10 private int num1; 11 private int num2; 12 private int result;//結果返回給頁面 13 14 public String execute(){ 15 System.out.println("當指定要走的方法時不會走此方法"); 16 return "success"; 17 } 18 19 /**添加的方法 */ 20 public String add(){ 21 result=num1+num2; 22 System.out.println("走add方法!"); 23 return "success"; 24 } 25 26 /** Get,Set方法 */ 27 public int getNum1() { 28 return num1; 29 } 30 31 public void setNum1(int num1) { 32 this.num1 = num1; 33 } 34 35 public int getNum2() { 36 return num2; 37 } 38 39 public void setNum2(int num2) { 40 this.num2 = num2; 41 } 42 43 public int getResult() { 44 return result; 45 } 46 47 public void setResult(int result) { 48 this.result = result; 49 } 50 51 }
來源網站:https://www.cnblogs.com/wkrbky/p/5889328.html