圖解一、大綱(下面有其詳解,最下面是文字講解)
1、struts.xml
(1).package配置
package中的namespace對應的地址:localhost:8080/hello/helloAction
<!--
package 它跟項目中的包沒有關係,是用來管理action,可以配置多個action
name 給這個package起個名字,多個package的名字不能重複,沒什麼意義,可以隨便命名;
一般情況下它的命名是按照當前package管理action的分類來命名的。
namespace 訪問路徑地址前綴,給當前的action設置一個訪問前路徑跟name沒有關係
跟其他的package中的namespace能不能重複,沒有要求。
extends 繼承自struts-default必須寫名字不能改
從struts2 核心包中 struts-2.2-core 下面的struts-default.xml
這裏配置了很多默認的屬性。
abstract 抽象的 聲明的一個標誌,當前的這個配置文件不能獨立運行,等待被繼承。-->
<package name="hello"namespace="/hello" extends="struts-default">
(2).action配置
、<!--
action 配置詳解
name 給action起個名字,決定了訪問路徑最後的地址
class 類的完整路徑名
method 訪問類中的方法-->
<action name="helloAction"class="cn.hd.hello.HelloAction" method="hello">
(3).result配置
<!--result
name 對應的是action類中的method的返回值
名字可以隨便寫,只要和action類中的返回值結果是一樣的
但是我們一般情況下使用特定的。
type dispatcher 轉發
redirect 重定向
標籤中間的值 跳轉頁面的地址-->
<result name="success"type="redirect">/hello.html</result>
2、常量配置
默認的常量配置 在struts2的核心包下的
默認常量配置的文件名是default.propertites
如何修改這些常量的方法:
(1).在struts.xml中去修改
<!--i18n 國際化 相當於我們在Servlet中設置的編碼 解決了post請求的中文亂碼問題-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2).在src目錄下創建struts.properties文件 直接在該文件中去修改常量即可
(3).在web.xml文件中去修改,添加一個<context-param>。
在param-name中書寫常量名,在param-value中書寫常量值。
三個地方可以同時修改,但是生效的順序爲web.xml>struts.properties>struts.xml.推薦使用第一個!
常用的常量配置
(1).i18n.ecoding
<!--i18n 國際化 相當於我們在Servlet中設置的編碼 解決了post請求的中文亂碼問題-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2).擴展名 struts.action.extension
<constantname="struts.action.extension"value="action"></constant>
(3).struts.devMode設置開發者模式 默認關閉
<!--developerMode 開發者模式
1.熱部署 當修改配置文件後,等一段時間會自動加載
2.提高錯誤信息的提示(友好的錯誤提示)-->
<constant name="struts.devMode"value="true"></constant>
(4).include 在src struts.xml可以讀取其他位置的xml配置文件。
要求 新創建的xml 必須也有約束
<includefile="cn\hd\dynamic\struts.xml"></include>
3、高級配置
爲什麼要使用動態方法:
1.如果使用的是 POJOaction ,動態方法調用可能會引起安全問題;
2. 動態方法調用和從 Struts 1 中引用過來的通配符方法有重疊;如果你的應用涉及到安全,或者習慣在 action 配置中使用感嘆號作爲通配符的話,需要在struts 應用的 struts.properties 配置文件中將 struts.enable.DynamicMethodInvocation 設置爲 false 。
如何配置動態方法:
(1).推薦使用
(訪問路徑:localhost://8080/user/userAction_add.action)
<action name="userAction_*"class="cn.hd.dynamic.UserAction" method="{1}">
<resultname="success">/hello.html</result>
</action>
action:配置中的name屬性,決定着瀏覽器的訪問地址
struts2可以採用通配符的方式默認去讀取 method{1} 會自動將路徑中userAction… 配置到method中。
(2).開啓動態方法
首先要開啓動態方法的常量(訪問路徑: http://localhost:8080/user/userAction!delete.action)
<!--動態方法調用的常量,默認是關閉的-->
<constant name="struts.enable.DynamicMethodInvocation"value="true"></constant>
<action name="userAction"class="cn.hd.dynamic.UserAction">
<resultname="success">/hello.html</result>
</action>
將action中的method屬性刪除掉
完成了動態方法的開啓
測試的時候,在瀏覽器的路徑中 輸入action的name值 + ! +方法名(Action中的方法名)
注意:
第一種方法 在struts2的高版本中會無效
首先你也要開啓動態方法,然後增加allowed-methods屬性
<action name="userAction_*"class="cn.hd.dynamic.UserAction" method="{1}">
<resultname="success">/hello.html</result>
<!--高版本要加這個屬性,不然版本會不生效-->
<allowed-methods>update,add,delete,select</allowed-methods>
</action>
4、默認配置
(1).刪減配置
<!--
method 屬性可以不寫 默認方法 execute
result 標籤中的name屬性可以不寫success 默認爲success
type 可以不寫默認是dispatcher(轉發)
class 可以不寫 默認的類是核心包Struts2-default中的ActionSupport
打開這個類文件它裏面有execute方法,該方法返回一個success字符串
-->
<actionname="defaultAction"class="cn.hd.default_demo.DefaultAction">
<result>/hello.html</result>
</action>
(2).默認的action地址
<!--default-action-ref 默認的action地址
如果請求地址不存在,那麼會默認訪問default-action-ref裏面的action-->
<default-action-refname="defaultAction"></default-action-ref>