Struts2 中 Action的總結

Action

實現一個Action的最常用方式:從ActionSupport繼承

 

調用Action

l 通配符調用action

<action name="index_*" class="daiwei.struts.action.StrutsDemo" method="userStar">

<result>/action/index.jsp</result>

</action>

上述通配符可以調用所有以”index_”爲開頭的URL

若想使用通配符控制調用方法,使用如下:

<action name="index-*" class="daiwei.struts.action.StrutsDemo" method="{1}">

<result>/action/index.jsp</result>

</action>

該模式下,若調用格式爲struts/index-add,則調用的方法爲StrutsDemo.add()

l 動態調用

<action name="index" class="daiwei.struts.action.StrutsDemo">

<result name="success">/action/index.jsp</result>

</action>

在改配置下,其中Action StrutsDemo中有方法:execute()add()

若不想使用默認的execute()方法,可以使用動態調用模式,調用方式如下:

Struts/index!add

參數傳遞

l 單個簡單參數

1. 在Action中加入該參數,並實現set,get方法。

代碼見daiwei.struts.action.UserAction

2. 在頁面中調用

http://localhost:8080/struts2/action/myUser?mes=es

l 單個對象參數

一般模式

1. action中加入該對象,並實現set,get方法法

代碼見daiwei.struts.action.UserAction

2. 在頁面中調用

http://localhost:8080/struts2/action/myUser?user.name=admin

ModelDriven模式

1. 在action中加入對象,並實現ModelDriven接口,重寫getModel()方法

代碼見daiwei.struts.action.UserActionDriven

2. 在頁面中調用

http://localhost:8080/struts2/action/userDriven?name=admin

l Request, Session, Application

Map類型的上述三種

1. 依賴於容器Struts2)

見代碼daiwei.struts.action.LoginAction1

2. IOC最常用

見代碼daiwei.struts.action.LoginAction2

原始類型的上述三種

1. 依賴於容器(Struts2)

見代碼daiwei.struts.action.LoginAction3

2. IOC

見代碼daiwei.struts.action.LoginAction4

l 參數驗證addFieldError

代碼見daiwei.struts.action.UserAction

代碼下載鏈接:http://download.csdn.net/detail/u011672579/9545505


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章