在传统的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"; //预置resultpublic 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}表示第二个星号的内容,以此类推。

【struts2】struts2中的Action详解相关推荐

  1. Struts2.perperties中的配置详解

    struts.configuration 该属性指定加载Struts 2配置文件的配置文件管理器.该属性的默认值是org.apache.Struts2.config.DefaultConfigurat ...

  2. Struts2拦截器的使用 (详解)

    Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...

  3. Struts2框架中的Action接口和ActionSupport类

    Struts2框架中的Action接口和ActionSupport类 1.Action接口 2.ActionSupport类 3.登录案例 3.1.页面 3.2.控制器 3.3.struts.xml配 ...

  4. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    Spring中,applicationContext.xml 配置文件在web.xml中的配置详解 2016年10月04日 15:22:26 阅读数:7936 转自http://www.cnblogs ...

  5. [置顶] iOS中 支付宝钱包详解/第三方支付

    [置顶] iOS中 支付宝钱包详解/第三方支付 韩俊强的博客 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 一.在app中成功完成支付宝支付的过程 1.申请支付 ...

  6. Unity中AB包详解(超详细,特性,打包,加载,管理器)

    Unity中的AssetBundle详解 AssetBundle的概念 AssetBundle又称AB包,是Unity提供的一种用于存储资源的资源压缩包. Unity中的AssetBundle系统是对 ...

  7. Android设置中“强行停止”详解

    Android设置中"强行停止"详解 最近工作上遇到了广播接受不到的问题,查看了<Android 开发艺术探索>一书中关于广播的发送和接受的章节(P356-P362). ...

  8. python3 urlopen_扣丁学堂解析Python3中urlopen()使用详解

    扣丁学堂解析Python3中urlopen()使用详解 2018-07-23 13:31:14 1318浏览 在现如今,2018年Python一直属于IT行业中比较热门技术,那么今天扣丁学堂Pytho ...

  9. WebService中的WSDL详解

    WebService中的WSDL详解 有人在WebService开发的时候,特别是和第三方有接口的时候,走的是SOAP协议,然后用户(或后台)给你一个WSDL文件(或网址),说按照上面的进行适配, 这 ...

最新文章

  1. WaveSwipeRefreshLayout
  2. Java 的转义字符
  3. How ASP.NET MVC Works?
  4. Tickets HDU - 1260
  5. centos7 kafka2.3.1单点部署
  6. Java主函数要放在哪个类里_JAVA:主函数一定要放在静态内部类里吗
  7. django-orm补课-使用shell-新增一行-再增一行-查找行-修改行
  8. 【图像处理基础知识(python+openCV)】——目标检测
  9. 【Express】—get根据不同的参数返回不同的数据
  10. 电脑远程服务_电脑远程维修专家在线服务
  11. cmake常用语法参考
  12. ASP.NET profile之 找不到存储过程'dbo.aspnet_CheckSchemaVersion'
  13. Nexus3 功能介绍
  14. idea 2018汉化包(附使用教程)
  15. 怎么才能转到计算机专业申请书,大学转专业的申请书范文
  16. 微信将可开小号!微信内测一个手机可注册俩号
  17. deeplinux 热点_在deepin linux系统中连接无线的同时开启热点(即网络共享)的步骤
  18. 三年半 Java 后端鹅厂面试经历
  19. oracle的闪存_ORACLEFS1-2闪存存储系统.PDF
  20. 对 ArabicRSS APK 应用木马样本的分析

热门文章

  1. 随机验证码。 * 随机生成十组六位字符组成的验证码。 * 验证码由大小写字母、数字字符组成。
  2. android studio设置内存
  3. Java并发编程—线程同步类
  4. OCR算法识别率怎么评估?
  5. JVM学习笔记之-拉圾回收概述,垃圾回收相关算法
  6. Android事件分发溯源详解
  7. 【python】装饰器
  8. 练习-多表图书管理系统
  9. 安装完MAVEN后输入mvn -v, 提示不是内部命令的问题
  10. hdu 1233 还是畅通工程(最小生成树的Prim和Kruskal两种算法的c++实现)(prim算法详解)...