Struts2 XML配置詳解

2021-08-14 01:23:46 字數 2237 閱讀 6516

這裡主要介紹一下關於struts2的xml配置資訊

這裡主要配置了一些struts2的核心元件,把這些元件注入到struts2中

屬性解釋

|-class:必填屬性,為bean屬性的實現類

|-type:可選屬性,它指定bean例項實現的struts2的規範,該規範通常是通過某個介面來體現,因此該屬性的值通常是乙個struts2介面。如果需要將bean例項作為struts2元件來使用,則應該指定該屬性的值。

|-name:可選屬性,給bean屬性起個名稱,對於有相同type型別的多個bean,他們的name屬性不能相同。

type=

"com.opensymphony.xwork2.factory.resultfactory" name="struts"

class="org.apache.struts2.factory.strutsresultfactory" />

type=

"com.opensymphony.xwork2.factory.actionfactory" name="struts"

class="com.opensymphony.xwork2.factory.defaultactionfactory" />

type=

"com.opensymphony.xwork2.factory.converte***ctory" name="struts"

class="com.opensymphony.xwork2.factory.defaultconverte***ctory" />

type=

"com.opensymphony.xwork2.factory.intercepto***ctory" name="struts"

class="com.opensymphony.xwork2.factory.defaultintercepto***ctory" />

以上**為struts2自己裡面配置的一些元件,**在struts2-core-2.3.24.jar下的struts-default.xml中可以找到。這個屬性一般情況下不用自己配置。

包中包含了命名空間和繼承屬性,action類就是配置在包下的

name="user"

namespace="/"

extends="struts-default" >

action是struts2用來代替servlet的,action是乙個多例項,執行緒安全的物件

name="useraction_*"

class="cn.itheima.a_hello.useraction"

method="" >

name屬性最後的_*是實現乙個動態方法呼叫,比如訪問useraction_login就會執行useraction中的login方法

result負責action類的方法執行後頁面的跳轉

name="success"

type="dispatcher" >/hello.jspresult>

type屬性的具體值可以到struts-default中檢視

用於匯入其他struts的配置檔案,file裡面填寫src下的完整路徑

struts2的常量配置檔案位置在struts2-core-2.3.24.jar/org.apache.struts2/default.properties中,可以開啟此檔案進行檢視。

此屬性用於修改struts的常量,name為要修改常量的名字,value為想要修改的值。

想要修改常量也可以在src目錄下建立struts.properties在裡面進行鍵值對的配置

struts.devmode=true

修改常量還可以在web.xml中進行配置

param>

struts.devmodeparam-name>

true

param-value>

param>

struts框架讀取配置檔案的載入順序

|-default.properties

|-struts-default.xml

|-struts-plugin.xml

|-struts.xml

|-struts.properties

|-web.xml

Struts2 xml表單驗證

只對action中的某個方法進行校驗 1 需要校驗的action名 方法名 validation.xml 對應xml中的屬性的name值 如 registeraction add validation.xml 對應 add.action 對registeraction中的add 方法進行校驗 對整個...

struts 2 XML驗證器規則說明

struts 2 xml驗證器規則說明 default.xml 檔案目錄 xwork core 2.3.8.jar包中 com opensymphony xwork2 validator validators default.xml 位置 驗證規則 驗證器型別 驗證是否為空,可選引數trim 驗證字...

struts2配置詳解

在配置struts 時,如果除了核心 外還有其他 其他 需要放在核心 之前。struts2中action開發的幾種方式 1 繼承actionsupport packagecn.test.user importcom.opensymphony.xwork2.actionsupport publiccl...