1:Struts2的默認訪問后綴是.action(特別需要注意的是改了配置文件web.xml或者struts.xml需要重啟服務器)
2:Struts2中常用的常量介紹:
<!-- 一:全局配置 -->
<!--1.請求數據編碼 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!--2.修改struts2默認的自定義后綴 -->
<constant name="struts.action.extension" value="action,do,"/>
<!--3.設置瀏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!--4.當struts的配置文件修改后,系統是否自動重新加載該文件,默認值為false(生產環境下使用),開發階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!--5.開發模式下使用,這樣可以打印出更詳細的錯誤信息 -->
<constant name="struts.devMode" value="true" />
<!--6.默認的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!--7.與spring集成時,指定由spring負責action對象的創建 -->
<constant name="struts.objectFactory" value="spring" />
<!--8.該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性為 false -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!--9.上傳文件的大小限制 -->
<constant name="struts.multipart.maxSize" value="10701096"/>
3:Struts2的動態Action的簡單應用和多個.xml的使用:
第一步:引包,略去
第二步:配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>struts2_20170219</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 引入struts2的核心過濾器 --> 14 <filter> 15 <!-- 過濾器的名稱 --> 16 <filter-name>struts2</filter-name> 17 <!-- 過濾器類 --> 18 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 19 </filter> 20 <filter-mapping> 21 <!-- 過濾器名稱 --> 22 <filter-name>struts2</filter-name> 23 <!-- 過濾器映射 --> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 </web-app>
第三步:開發第一個Action,配置第一個struts01.xml文件
1 package com.bie.struts01; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 * @author BieHongLi 7 * @version 創建時間:2017年2月19日 下午3:08:53 8 * 開發action,處理請求 9 */ 10 public class HelloAction extends ActionSupport{ 11 12 private static final long serialVersionUID = 1L; 13 14 /** 15 * 重寫execute,處理請求的方法 16 */ 17 @Override 18 public String execute() throws Exception { 19 System.out.println("訪問到了action,正在 處理請求"); 20 System.out.println("hello world!!! struts2"); 21 return SUCCESS; 22 } 23 24 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 聲明包 --> 8 <package name="helloPackage" abstract="false" extends="struts-default"> 9 <action name="helloAction" class="com.bie.struts01.HelloAction"> 10 <result name="success">success.jsp</result> 11 </action> 12 </package> 13 14 15 </struts>
第四步:開發第二個Action,配置第二個struts02.xml文件
1 package com.bie.struts02; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 * @author BieHongLi 7 * @version 創建時間:2017年2月20日 下午4:05:38 8 * 9 */ 10 public class TestAction extends ActionSupport{ 11 12 private static final long serialVersionUID = 1L; 13 14 public String test(){ 15 System.out.println("測試的方法!!!"); 16 return SUCCESS; 17 } 18 19 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 聲明包 --> 8 <package name="testPackage" abstract="false" extends="struts-default"> 9 <!-- 動態方法調用的格式如:http://localhost:8080/struts2_20170219/testAction!test --> 10 <action name="testAction" class="com.bie.struts02.TestAction"> 11 <result name="success">success.jsp</result> 12 </action> 13 </package> 14 15 16 </struts>
第五步:配置struts2的全局變量以及總struts.xml文件;
需要注意的是動態Action默認是不使用的,將false改為true即可使用動態Action。
<?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> <!-- 一:全局配置 --> <!--1.請求數據編碼 --> <constant name="struts.i18n.encoding" value="UTF-8"/> <!--2.修改struts2默認的自定義后綴 --> <constant name="struts.action.extension" value="action,do,"/> <!--3.設置瀏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 --> <constant name="struts.serve.static.browserCache" value="false"/> <!--4.當struts的配置文件修改后,系統是否自動重新加載該文件,默認值為false(生產環境下使用),開發階段最好打開 --> <constant name="struts.configuration.xml.reload" value="true"/> <!--5.開發模式下使用,這樣可以打印出更詳細的錯誤信息 --> <constant name="struts.devMode" value="true" /> <!--6.默認的視圖主題 --> <constant name="struts.ui.theme" value="simple" /> <!--7.與spring集成時,指定由spring負責action對象的創建 --> <constant name="struts.objectFactory" value="spring" /> <!--8.該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性 為 false --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!--9.上傳文件的大小限制 --> <constant name="struts.multipart.maxSize" value="10701096"/> </struts>
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <!-- Struts2的全局配置,必須寫在最上面,格式如下所示 --> 9 <include file="constant.xml"></include> 10 11 <!-- 總配置文件,引入其他所有的配置文件 ,引入其他的配置文件需要注意的是/不是. --> 12 <include file="com/bie/struts01/struts01.xml"></include> 13 <include file="com/bie/struts02/struts02.xml"></include> 14 15 16 </struts>
運行效果如下所示:(注意:動態Action的訪問是action的name屬性加!后面是方法名即可訪問。)詳細如下圖所示:
4:配置各項默認值:詳解如下所示 配置全局跳轉視圖,先去action的result找,如果沒有就去全局視圖找:
<global-results> <result name="success">success.jsp</result> </global-results>
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 聲明包 --> 8 <package name="helloPackage" abstract="false" extends="struts-default"> 9 <!-- 配置全局跳轉視圖 --> 10 <global-results> 11 <result name="success">success.jsp</result> 12 </global-results> 13 14 <action name="helloAction" class="com.bie.HelloAction"> 15 <!-- <result name="success">success.jsp</result> --> 16 </action> 17 18 <action name="worldAction" class="com.bie.WorldAction"> 19 <!-- <result name="success">success.jsp</result> --> 20 <!-- 返回結果標記success對應的頁面在當前action中沒有配置,那么會 21 會去找全局配置是否有success標記對應的頁面 ,如果全局配置也沒有 22 success標記對應的頁面,那么就報404錯誤。--> 23 </action> 24 25 26 <!-- 配置各項默認值 --> 27 <!-- 28 1:name 只配置了訪問路徑名稱 29 2:class 默認執行得action在struts-default有配置 30 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /> 31 3:method默認是execute 32 默認得方法execute返回值為success,對應頁面去全局視圖找, 33 如果全局試圖沒,那么報404錯誤。 34 --> 35 <action name="test01"></action> 36 </package> 37 38 39 </struts>
5:Struts2的Action的開發的幾種方式
(1):方式一,繼承ActionSupport,最經常使用的。
如果用struts的數據校驗功能,必須繼承此類;
(2):方式二,實現Action接口
(3):方式三,手動寫;
6:使用通配符進行配置Action(在struts中配置信息中,可以用*和{1}優化配置;),如下所示:
1 package com.bie.lesson03; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 * @author Author:別先生 7 * @date Date:2017年9月10日 下午10:22:34 8 * 9 * 10 */ 11 public class StrutsConfig extends ActionSupport{ 12 13 /** 14 * 15 */ 16 private static final long serialVersionUID = 1L; 17 18 public String login() { 19 20 System.out.println("模擬的登陸的方法"); 21 22 return SUCCESS; 23 } 24 25 public String register(){ 26 27 System.out.println("模擬的注冊的方法"); 28 29 return SUCCESS; 30 } 31 32 }
然后配置strutsConfig.xml配置文件,注意這里的Action使用了占位符進行配置:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 聲明包 --> 8 <package name="strutsPackage03" extends="struts-default"> 9 <!-- 10 定義action 11 name:訪問路徑 12 class:類的路徑 13 method:方法名稱 14 --> 15 <action name="user_*" class="com.bie.lesson03.StrutsConfig" method="{1}"> 16 <result name="success">success.jsp</result> 17 </action> 18 19 </package> 20 21 </struts>
然后在總的配置文件struts.xml中引入配置文件strutsConfig.xml即可,如下所示:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- struts在允許的時候會加載這個總配置文件:src/struts.xml --> 8 9 <!-- 在總配置文件中引入其他所有的配置文件 --> 10 11 <include file="com/bie/lesson03/strutsConfig.xml"></include> 12 </struts>
訪問的時候如下所示:
對比一下可以很清晰的發現他們之間的區別,這就是使用了占位發簡化開發;
7:Struts的常量
Struts1中默認訪問后綴是*.do
Struts2中默認訪問后綴是*action
如何修改默認訪問后綴:
(1):Struts的訪問后綴在哪里定義:
在default.properties文件中
默認訪問后綴:struts.action.extension=action,,
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- struts在允許的時候會加載這個總配置文件:src/struts.xml --> 8 9 <!-- 全局配置 --> 10 <!-- 修改Struts的默認訪問后綴 --> 11 <constant name="struts.action.extension" value="action,do,struts"></constant> 12 13 14 </struts>
8:配置的順序書寫,?代表0或者1,*代表0或者多:
The content of element type "package" must match "(
result-types?,
interceptors?,
default-interceptor-ref?,
default-action-ref?,
default-class-ref?,
global-results?,
global-exception-mappings?,
action*)".