【struts2】struts2中的Action详解
在傳統(tǒng)的MVC框架(如struts1、Spring等)中,Action都需要實現(xiàn)特定的接口,這些接口都是MVC框架定義的,實現(xiàn)MVC的接口會與MVC框架耦合。struts2的Action要靈活得多,可以實現(xiàn)struts2接口,也可以不實現(xiàn)。
一、ActionSupport類
自定義Action一般直接繼承自ActionSupport類,并定義變量,覆蓋execute()方法。變量的值會被struts2通過setter()方法自動賦值,execute()方法中直接使用即可。execute(0方法的返回值為配置在struts.xml中的<result />配置:,例如:
action方法:
public class LoginAction extends ActionSupport{private String account;private String password;public String execute(){if("lmb".equalsIgnoreCase(account) && "1234".equals(password)){return SUCCESS;}return LOGIN;} }ActionSupport中實現(xiàn)了其他的方法,例如數(shù)據(jù)校驗等,繼承ActionSuppot的好處是可以直接使用數(shù)據(jù)校驗等struts2集成的方法。
二、Action接口
自定義Action還可以直接實現(xiàn)Action接口。事實上,struts2的ActionSupport類也實現(xiàn)自該接口。Action接口只定義了一個execute()主方法,以及幾個常用的結(jié)果名稱(success、nono、error、input、login等)。編程中盡量使用這些預置的結(jié)果名稱。
Action接口的代碼如下:
package com.opensymphony.xwork2; public interface Action{//Action接口public abstract String execute() throws Exception;//主方法public static final String SUCCESS = "success"; //預置resultpublic static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; ````````}直接實現(xiàn)該接口以及execute()方法即可。
三、不繼承任何類的Action
struts2的Action并不一定要實現(xiàn)Action接口,任何的POJO都可以用做Action,只要這個Action具有public String execute()方法。如果struts2發(fā)現(xiàn)Action類沒有實現(xiàn)Action接口,會通過反射來調(diào)用execute()方法,例如:
action方法:
public class LoginAction{private String account;//賬號private String password;//密碼//……getter()和setter()方法……public String execute(){//主方法if("lmb".equalsIgnoreCase(account) && "1234".equals(password)){return "success"; //如果用戶輸入的用戶名為"lmb",密碼為"1234"則跳轉(zhuǎn)到登錄成功頁面}return "login";//否則跳轉(zhuǎn)到登陸頁面}public String login(){ //登錄方法return execute(); //返回主方法} public String logout(){ //注銷方法return "logout"; //返回注銷頁面} }不實現(xiàn)Actin接口的好處是不與struts2發(fā)生耦合,代碼不依賴于struts2的類庫。
四、Action的可執(zhí)行方法
execute()是Action的默認方法。struts2還可以執(zhí)行Action的其他方法,只要這些方法沒有參數(shù),并返回String類型。這些方法也可以有throws聲明,也可以沒有。struts2會在運行時根據(jù)方法的特征判斷是否是可執(zhí)行方法(參數(shù)、返回值),并通過反射執(zhí)行。例如:
action方法:
import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{private String account;//賬號private String password;//密碼//……getter()和setter()方法……public String execute(){//主方法if("lmb".equalsIgnoreCase(account) && "1234".equals(password)){return SUCCESS; //如果用戶輸入的用戶名為"lmb",密碼為"1234"則跳轉(zhuǎn)到登錄成功頁面}return LOGIN;//否則跳轉(zhuǎn)到登陸頁面}public String login() throws Exception{ //登錄方法,有throws聲明return execute();//返回主方法}public String logout() {//注銷方法,沒有throws聲明return "logout"; //返回注銷頁面}}login()和logout()并不是默認的可執(zhí)行方法,可將其配置到struts.xml中,或者通過特定的URL(login!login.action、login!logout.action)直接執(zhí)行這些非默認方法,見下文。
1、通過URL執(zhí)行Action的方法
執(zhí)行Action的非默認方法,例如logout(),可以使用
action!method.action的URL形式訪問,其中,
action為struts.xml中配置的Action的名字,method為Action的方法名,中間用“!”隔開,
例如:
http://localhost:8080/struts2/loginPerson!logout.action將執(zhí)行l(wèi)oginPerson的logout()方法。
2、通過將執(zhí)行方法配置到struts.xml的Action中來執(zhí)行action的方法
方法一:
也可以把方法用method配置到struts.xml的Action中,省去“!”符號。這時Action的名稱可以隨便指定。可以為每個方法均定義一種Action名稱,然后使用該Action名稱對應的訪問方法訪問Action。這樣的缺點是同一個Action需要重復配置多次。例如:
struts.xml
<!-- 配置執(zhí)行LoginAction的login()方法 --> <action name="loginPerson" class="com.lmb.struts2.action.LoginAction" method="login"><result name="success">/welcome.jsp</result><result name="login">/login.jsp</result> </action><!-- 配置執(zhí)行LoginAction的logout()方法 --> <action name="logoutPerson" class="com.lmb.struts2.action.LogoutAction"method="logout"><result name="success">/welcome.jsp</result><result name="logout">/logout.jsp</result> </action注意:
配置的都是LoginAction,只是name屬性、method屬性不一樣。
請求loginPerson.action時會執(zhí)行l(wèi)ogin()方法;
請求logoutPerson.action時會執(zhí)行l(wèi)ogout()方法。
或者直接使用通配符配置:
方法二:
struts.xml
<!-- 使用通配符配置LoginAction的任意方法 --> <action name="*Person" class="com.lmb.struts2.action.LoginAction" method="{1}"><result name="success">/welcome.jsp</result><result name="{1}">/{1}.jsp</result> </action>struts2也支持通配符配置Action。例如上面在action名稱中使用“*”配置action名稱,可以使用多個星號。星號代表的內(nèi)容也可以在本action配置內(nèi)部使用{1}、{2}等引用,其中{1}表示第一個星號的內(nèi)容,{2}表示第二個星號的內(nèi)容,以此類推。
總結(jié)
以上是生活随笔為你收集整理的【struts2】struts2中的Action详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【struts2】struts2中对象的
- 下一篇: 【struts2】struts2的零配置