Struts2_ActionSupport和通配符映射和動態方法調用

Struts2_ActionSupport

ActionSupport:action中classs沒有定義時默認的一個類,定義在struts-default.xml裏

1).ActionSupport是默認的Action類,若某個action節點沒有配置class屬性,則ActionSupport即爲將執行的Action類,而execute方法即爲默認執行的action方法

   <action name="TestActionSupport">
        	<result>/test-actionsupport.jsp</result>
        </action>

等同於

 <action name="TestActionSupport"  

class="com.opensymphony.xwork2.ActionSupport" method="execute">
        	<result>/test-actionsupport.jsp</result>
        </action>

2).在手工完成字段驗證,顯示錯誤消息,國際化等情況下,推薦繼承ActionSupport.

ActionSupport範例:

struts.xml
        <action name="TestActionSupport">
        	<result>/test-actionsupport.jsp</result>
        </action>

不用寫Action類,默認使用ActionSupport
index,jsp
	<a href="TestActionSupport">TestActionSupport</a>


通配符映射

一個Web應用可能有成百上千個action聲明,可以利用struts提供的通配符映射機制把多個彼此相似的映射關係簡化爲一個映射關係

先寫個Action類來說明

package com.wul.struts2_4.action;

public class UserAction {
	
	public String save(){
		System.out.println("save");
		return "save-success";
	}
	
	public String update(){
		System.out.println("update");
		return "update-success";
	}
	
	public String delete(){
		System.out.println("delete");
		return "delete-success";
	}
	
	public String query(){
		System.out.println("query");
		return "query-success";
	}
	
	public String test(){
		System.out.println("test");
		return "test-success";
	}
	
	
}

在struts.xml中,

---若找到多個匹配,沒有通配符的那個將勝出,即精確匹配會優先執行
(1)
        <action name="UserAction-*" class="com.wul.struts2_4.action.UserAction" method="{1}">
        	<result name="{1}-success">/chenggong.jsp</result>
        </action>
(2)
         <action name="UserAction-save" class="com.wul.struts2_4.action.UserAction"  method="test">
        	<result name="test-success">/chenggong.jsp</result>
        </action>
傳過來的action是"UserAction-save"的話,因爲有save方法和test方法與其對應,但test方法是精確匹配,所以執行(2)


---若指定的動作不存在,struts2將會嘗試把這個URI與任何一個包含着通配符*的動作名進行匹配

---被通配符匹配到的URI字符的子串可以用(1),(2)來引用,(1)匹配第一個子串,(2)匹配第二個字串。。。

   <action name="UserAction-save" class="com.wul.struts2_4.action.UserAction" 
        		method="save">
        	<result name="save-success">/chenggong.jsp</result>
        </action>
        
         <action name="UserAction-update" class="com.wul.struts2_4.action.UserAction" 
        		method="update">
        	<result name="update-success">/chenggong.jsp</result>
        </action>
        
         <action name="UserAction-delete" class="com.wul.struts2_4.action.UserAction" 
        		method="delete">
        	<result name="delete-success">/chenggong.jsp</result>
        </action>
        
         <action name="UserAction-query" class="com.wul.struts2_4.action.UserAction" 
        		method="query">
        	<result name="query-success">/chenggong.jsp</result>
        </action>

等同於

        <action name="UserAction-*" class="com.wul.struts2_4.action.UserAction" method="{1}">
        	<result name="{1}-success">/chenggong.jsp</result>
        </action>

---(0)匹配整個URI


----若struts找到的帶有通配符的匹配不止一個,則按先後順序進行匹配(沒有通配符,但傳過來的action都相同會默認執行後面一個
(1)
        <action name="*-update" class="com.wul.struts2_4.action.UserAction" 
        		method="test">
        	<result name="test-success">/chenggong.jsp</result>
        </action>
(2)
        <action name="UserAction-*" class="com.wul.struts2_4.action.UserAction" 
        		method="{1}">
        	<result name="{1}-success">/chenggong.jsp</result>
        </action>


傳過來的action是"UserAction-update"的話,因爲(1),(2)都與其匹配,按先後順序執行(1)



----*可以匹配零個或多個字符,但不包括/字符,如果想把/字符包括在內,需要使用**,如果需要對某個字符進行轉義,需要使用\.



動態方法調用
  動態方法調用:通過url動態調用Action中的方法
  action聲明
  
   URL :
   -- /struts-app2/Product.action    Struts調用Product類的execute
   --/struts-app2/Product!save.action   Struts調用Product類的save()方法

默認情況下,Struts的動態方法調用處於禁用狀態








發佈了1 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章