【struts2】struts2中的Action详解

在传统的MVC框架(如struts1、Spring等)中,Action都需要实现特定的接口,这些接口都是MVC框架定义的,实现MVC的接口会与MVC框架耦合。struts2的Action要灵活得多,可以实现struts2接口,也可以不实现。

一、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中实现了其他的方法,例如数据校验等,继承ActionSuppot的好处是可以直接使用数据校验等struts2集成的方法。

二、Action接口

自定义Action还可以直接实现Action接口。事实上,struts2的ActionSupport类也实现自该接口。Action接口只定义了一个execute()主方法,以及几个常用的结果名称(success、nono、error、input、login等)。编程中尽量使用这些预置的结果名称。

Action接口的代码如下:

package com.opensymphony.xwork2;
public interface Action{//Action接口
    public abstract String execute() throws Exception;//主方法
    public static final String SUCCESS = "success"; //预置result
    public static final String NONE = "none";   
    public static final String ERROR = "error";     
    public static final String INPUT = "input";     
    public static final String LOGIN = "login"; ````````

}

直接实现该接口以及execute()方法即可。

三、不继承任何类的Action

struts2的Action并不一定要实现Action接口,任何的POJO都可以用做Action,只要这个Action具有public String execute()方法。如果struts2发现Action类没有实现Action接口,会通过反射来调用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"则跳转到登录成功页面
        }
        return "login";//否则跳转到登陆页面
    }

    public String login(){  //登录方法
        return execute();   //返回主方法
    }   
    public String logout(){  //注销方法
        return "logout";     //返回注销页面
    }
}

不实现Actin接口的好处是不与struts2发生耦合,代码不依赖于struts2的类库。

四、Action的可执行方法

execute()是Action的默认方法。struts2还可以执行Action的其他方法,只要这些方法没有参数,并返回String类型。这些方法也可以有throws声明,也可以没有。struts2会在运行时根据方法的特征判断是否是可执行方法(参数、返回值),并通过反射执行。例如:

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"则跳转到登录成功页面
        }
        return LOGIN;//否则跳转到登陆页面
    }

    public String login() throws Exception{ //登录方法,有throws声明
        return execute();//返回主方法
    }

    public String logout() {//注销方法,没有throws声明
        return "logout"; //返回注销页面
    }

}

login()和logout()并不是默认的可执行方法,可将其配置到struts.xml中,或者通过特定的URL(login!login.action、login!logout.action)直接执行这些非默认方法,见下文。

1、通过URL执行Action的方法

执行Action的非默认方法,例如logout(),可以使用
action!method.action的URL形式访问,其中,
action为struts.xml中配置的Action的名字,method为Action的方法名,中间用“!”隔开,
例如:
http://localhost:8080/struts2/loginPerson!logout.action将执行loginPerson的logout()方法。

2、通过将执行方法配置到struts.xml的Action中来执行action的方法

方法一:

也可以把方法用method配置到struts.xml的Action中,省去“!”符号。这时Action的名称可以随便指定。可以为每个方法均定义一种Action名称,然后使用该Action名称对应的访问方法访问Action。这样的缺点是同一个Action需要重复配置多次。例如:

struts.xml

<!-- 配置执行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>

<!-- 配置执行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时会执行login()方法;
请求logoutPerson.action时会执行logout()方法。
或者直接使用通配符配置:

方法二:

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名称,可以使用多个星号。星号代表的内容也可以在本action配置内部使用{1}、{2}等引用,其中{1}表示第一个星号的内容,{2}表示第二个星号的内容,以此类推。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值