2、Struts2開始深入
- 2019 年 10 月 27 日
- 筆記
一、Struts2的配置文件載入順序
1 、進入過濾器【StrutsPrepareAndExecuteFilter】跟程式碼,可以看到對應的文件載入順序
進入StrtsPrepareAndExecuteFilter,跟裡面的init方法:
進入:
在進入:
init_DefaultProperties() —-載入default.properties
init_TraditionalXmlConfigurations(); —-載入struts-default.xml、struts-plugin.xml、struts.xml
init_LegacyStrutsProperties(); —-載入struts.properties
init_CustomConfigurationProviders(); —-載入配置提供類
init_FilterInitParameters() ; —-載入web.xml中過濾器初始化參數
init_AliasStandardObjects() ; —-載入Bean對象
2 、根據上面的程式碼,我們就可以得出配置文件的載入順序了
前三個配置文件不需要關心,是Struts2的內部配置文件,我們無法修改,能修改的就是後面三個配置文件,這幾個配置文件的載入是有一定的順序的。
重點關係後面的幾個配置文件,那是我們開發中需要進行更改的。後載入的配置文件中的常量會覆蓋先載入配置文件中的常量。
default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
注意:後配置的常量的值會覆蓋先配置的常量的值。
二、Action的配置文件Struts.xml。
1、<package>配置
Strtus2框架的核心組件是Action和攔截器,他使用包來管理Action和攔截器。每個包就是多個Action、多個攔截器、多個攔截器引用的集合。
在Struts.xml中,package元素用來定義包配置。在配置包時,必須指定name屬性,且不可重複。還需設置extends的屬性,extends的屬性值
必須是另一個包的name屬性,但該屬性值通常設置為struts-default。這樣該包中的Action就具有了Struts2框架默認的攔截器等功能。
name :包的名稱,只有在一個項目中不重名即可。
extends :繼承哪個包,通常值為struts-default。
namespace :名稱空間,與<action>標籤中的name屬性共同決定訪問路徑。
名稱空間有三種寫法:
帶名稱的名稱空間 :namespace=”/aaa”
跟名稱空間 :namespance=”/”
默認名稱空間 :namespace=””
abstract :抽象的,用於其他包的繼承。
2、<action>配置
action映射是框架中的基本單元。action映射就是將一個請求的url映射到一個action類,然後在通過參數配置對應的方法。
name :與namespace共同決定訪問路徑
class :Action類的全路徑
method :執行Action中的哪個方法的方法名,默認值execute
converter :用於設置類型轉換器
3、Struts2常量的配置:
在Struts2的框架中,提供了非常多的常量:(在default.properties)
struts.i18n.encoding=UTF-8 —-Struts2中所有的post請求的中文亂碼不用處理。
struts.action.extension=action,, —-Struts2請求的默認的擴展名。默認擴展名是.action或者什麼都不寫。
三、修改常量的值,可以有三個位置進行修正
1、struts.xml中進行修改
在struts.xml中通過<constant>元素配置常量是最常用的方式。需要指定兩個必須的屬性:name 、value.
name:指定了常量的常量名
value:指定了常量的常量值
<?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> <!--設置字元編碼為utf-8--> <constant name="struts.i18n.encoding" value="UTF-8"/> </struts>
2、struts.properties中進行修改
struts.properties文件其格式是Key-value對,每個Key對應一個value,key表示的struts2中的常量、value則是其常量值。
### 設定默認編碼為UTF-8 struts.i18n.encoding=UTF-8
3、web.xml中進行修改
在web.xml文件中配置核心過濾器的時候,可以通過初始化參數來配置常量。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <!--設置默認編碼為utf-8--> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
注意:眾多的常量記錯難度係數太大,我們可以參考default.properties文件來選擇我們需要的常量配置。而且,後載入的
配置文件的常量是會覆蓋先載入的配置文件中常量的值。
四、include的配置
分模組開發,也就是每個人負責不同的模組。那麼Struts.xml 肯定是無法滿足共用的,太容易出衝突了。一旦出現問題,會導致整個項目都
出現問題的。這個時候可以使用Sturts2的分模組開發。也就是一個主配置文件,然後再關聯其他不同模組的配置文件來協助 開發即可。
協同開發,互不影響。分而治之的思想,將任務分配下去。
包含文件
<?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> <!--設置字元編碼為utf-8--> <constant name="struts.i18n.encoding" value="UTF-8"/> </struts>
被包含文件
<?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> <!--設置字元編碼為utf-8--> <!--<constant name="struts.i18n.encoding" value="UTF-8"/>--> <!--引入com.turtle.demo1包下面的配置--> <include file="com/turtle/demo1/struts-demo1.xml"/> </struts>
五、Action的寫法
1、Action類是POJO的類,不繼承特殊的類、不實現任何特殊的介面,僅僅是一個pojo。
package com.turtle.demo1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Demo1Action { Logger logger = LoggerFactory.getLogger(Demo1Action.class); public String execute() { logger.debug("進入了伺服器。"); return "SUCCESS"; } }
Struts2中默認執行Action類中的execute方法,該方法有具體的規則:
方法的許可權修飾符為public
返回一個字元串,會用來指示跳轉到下一個Result
方法沒有參數
2、Action類實現一個Action的介面
package com.turtle.demo1; import com.opensymphony.xwork2.Action; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Demo2Action implements Action { Logger logger = LoggerFactory.getLogger(Demo2Action.class); // 該提供了五個常量 // String SUCCESS = "success"; // String NONE = "none"; // String ERROR = "error"; // String INPUT = "input"; // String LOGIN = "login"; @Override public String execute() { logger.debug("進入了伺服器。"); return SUCCESS; } }
3、 Action類繼承ActionSupport類【用得最多的寫法】
package com.turtle.demo1; import com.opensymphony.xwork2.ActionSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Demo3Action extends ActionSupport { Logger logger = LoggerFactory.getLogger(Demo3Action.class); @Override public String execute() { logger.debug("進入了伺服器。"); return SUCCESS; } }
六、Action的訪問:
定義一個action類:
package com.turtle.demo2; import com.opensymphony.xwork2.ActionSupport; import com.turtle.demo1.Demo1Action; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Demo2Action extends ActionSupport { Logger logger = LoggerFactory.getLogger(Demo1Action.class); /** * 保存 * @return */ public String save() { logger.debug("進入了save方法"); return null; } public String update() { logger.debug("進入update方法"); return null; } public String delete() { logger.debug("進入delte方法"); return null; } }
1、通過配置action標籤中method屬性來完成:
頁面
<%-- Created by IntelliJ IDEA. User: TurtleZhang Date: 2019/10/27 Time: 17:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h1>通過配置action標籤中method屬性來完成</h1> <div> <table> <tr> <td> <a href="${pageContext.request.contextPath}/save.action" >Save</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/update.action" >Update</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/delete.action" >Delete</a> </td> </tr> </table> </div> </body> </html>
配置文件
<?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.i18n.encoding" value="UTF-8"/> <package name="demo2" extends="struts-default" namespace="/"> <action name="save" class="com.turtle.demo2.Demo2Action" method="save"/> <action name="update" class="com.turtle.demo2.Demo2Action" method="update"/> <action name="delete" class="com.turtle.demo2.Demo2Action" method="delete"/> </package> </struts>
配置很簡單,也能正確訪問,但是同一個Action類配置了很多次,而且改動的只是後面的method的值。這樣有點重複了,大忌。體驗感覺不太好。
2、通過通配符的配置完成
頁面
<%-- Created by IntelliJ IDEA. User: TurtleZhang Date: 2019/10/27 Time: 17:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h1>通過配置action標籤中method屬性來完成</h1> <div> <table> <tr> <td> <a href="${pageContext.request.contextPath}/save.action" >Save</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/update.action" >Update</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/delete.action" >Delete</a> </td> </tr> </table> </div> </body> </html>
配置文件
<?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.i18n.encoding" value="UTF-8"/> <package name="demo2" extends="struts-default" namespace="/"> <action name="*" class="com.turtle.demo2.Demo2Action" method="{1}"/></package> </struts>
這種方法是最常使用的,在<action>中的name屬性使用*來代表任意字元,metho中的{1}代表的是屬性中出現的第一個*所代替的字元。這個時候就只需要
配置一個即可。
3、動態方法訪問
動態方法訪問在Struts2中默認是不開啟的,如果想要使用需要去開啟一個常量,我們載入struts.xml中
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
頁面
<%-- Created by IntelliJ IDEA. User: TurtleZhang Date: 2019/10/27 Time: 17:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h1>通過配置action標籤中method屬性來完成</h1> <div> <table> <tr> <td> <a href="${pageContext.request.contextPath}/demo2Action!save.action" >Save</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/demo2Action!update.action" >Update</a> </td> </tr> <tr> <td> <a href="${pageContext.request.contextPath}/demo2Action!delete.action" >Delete</a> </td> </tr> </table> </div> </body> </html>
配置文件
<?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.i18n.encoding" value="UTF-8"/> <!--開啟動態方法訪問--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="demo2" extends="struts-default" namespace="/"> <action name="demo2Action" class="com.turtle.demo2.Demo2Action"/> </package> </struts>