Struts2的Action编写
Action的编写的方式:
第一种方式:
Struts2的Action编写的最简单的方式就是写一个普通类,不继承自任何类,也不实现接口。如下:
1 package cn.geore.action; 2 3 public class OneAction { 4 /** 5 * 在Servlet中每次执行的是service方法,而在Struts2种每次调用执行的方法是execute() 6 * 因此对于具体的功能只需要卸载execute()中即可 7 * @return 8 */ 9 public String execute() { 10 return "first"; 11 } 12 13 public String add() { 14 return "add"; 15 } 16 }
第二种方式:
创建一个普通类,然后实现Action接口。
1 package cn.geore.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class TwoAction implements Action { 6 7 @Override 8 public String execute() throws Exception { 9 return "success"; 10 } 11 12 }
Action接口的常量值:
1 /** 2 * 成功,返回sucess。可以调用它,也可以在Action类中的execute方法中直接return "success"; 3 */ 4 public static final String SUCCESS = "success"; 5 6 /** 7 * The action execution was successful but do not 8 * show a view. This is useful for actions that are 9 * handling the view in another fashion like redirect. 10 */ 11 public static final String NONE = "none"; 12 13 /** 14 * The action execution was a failure. 15 * Show an error view, possibly asking the 16 * user to retry entering data. 17 */ 18 public static final String ERROR = "error"; 19 20 /** 21 * The action execution require more input 22 * in order to succeed. 23 * This result is typically used if a form 24 * handling action has been executed so as 25 * to provide defaults for a form. The 26 * form associated with the handler should be 27 * shown to the end user. 28 * <p/> 29 * This result is also used if the given input 30 * params are invalid, meaning the user 31 * should try providing input again. 32 */ 33 public static final String INPUT = "input"; 34 35 /** 36 * The action could not execute, since the 37 * user most was not logged in. The login view 38 * should be shown. 39 */ 40 public static final String LOGIN = "login";
View Code
第三种方式:
创建类,继承父类ActionSupport
1 package cn.geore.action; 2 3 import com.opensymphony.xwork2.Action; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class ThreeAction extends ActionSupport { 7 @Override 8 public String execute() throws Exception { 9 // ..... 10 return Action.SUCCESS; 11 } 12 }
Action类方法的访问:
对于Action类的方法,在默认的情况下,在每次执行的时候,默认访问的多是execute()方法,如果要访问其他的方法,Struts2提供了三种方式进行方法的访问。对于Action类的方法,如果又返回值的时候就必须是String类型。如果方法不返回,可以使用void修饰,但是不建议这么写,一般使用return "none";表示返回为空,如下:
1 // 无返回值的时候建议这样子定义 2 public String add() { 3 // ...... 4 return Action.NONE; 5 } 6 7 // 有返回值的时候,返回值必须为String类型 8 public String update() { 9 // ...... 10 return Action.SUCCESS; 11 }
定义一个BookAction类,在使用下面的三种方式实现对这个类方法的访问:
1 package cn.geore.bookaction; 2 3 import com.opensymphony.xwork2.Action; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class BookAction extends ActionSupport { 7 // 添加图书 8 public String addBook() { 9 System.out.println("添加图书..."); 10 return Action.NONE; 11 } 12 13 // 更新图书 14 public String updateBook() { 15 System.out.println("删除图书..."); 16 return Action.NONE; 17 } 18 }
第一种方式:在struts2的核心配置文件中,action标签的method属性决定调用Action类的哪一个方法。
book.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><!-- method配置 --><package name="bookaction" extends="struts-default" namespace="/book"><action name="addBook" class="cn.geore.bookaction.BookAction" method="addBook"><!-- <result name="success">/jsps/one/addBook.jsp</result> --></action><action name="updateBook" class="cn.geore.bookaction.BookAction" method="updateBook"><!-- <result name="success">/jsps/one/updateBook.jsp</result> --></action></package> </struts>
struts.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.i18n.encoding" value="UTF-8"></constant> 7 <!-- 引入外部的Struts模块的配置文件 --> 8 <!-- <include file="cn/geore/action/one.xml"></include> --> 9 <include file="cn/geore/bookaction/book.xml"></include> 10 </struts>
运行截图:
第二种方式:使用通配符的方式实现方法的访问
对于第一种方式,我们在package标签中,要配置action,每一个方法均要配置一个action,如果对于一个很多方法的开发,那么就要写非常多的action配置。那么这样写无疑是比较麻烦的,而Struts2的通配符方式就可以解决这个问题。
使用的方式:在action标签的name属性,给name属性的值一个*号(星号表示匹配任意的内容)。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><!-- 通配符配置 --><package name="bookaction" extends="struts-default" namespace="/book"><!-- name = book_*;这个表示action接收所有以book_开始的任意的字符串method中的{1},表示取得第一个占位符的值,也就是去的book_*的*所表示的值。--><action name="book_*" class="cn.geore.bookaction.BookAction" method="{1}"><!-- <result name="update">/jsps/one/updateBook.jsp</result><result name="add">/jsps/one/addBook.jsp</result> --></action></package> </struts>
第三种方式:动态访问的方式访问方法
转载于:https://www.cnblogs.com/geore/p/7526934.html
Struts2的Action编写相关推荐
- java登录中用户类型分类_基于用户登陆的struts2中action的分类详解
在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...
- Struts2中Action的属性接收参数
Struts2中Action的属性接收参数,有三种传递并接收参数的方式,第一种是在Action添加成员属性接收参数,第二种是域模型,就是利用对象域来进行传递和接收参数,第三种是ModelDriven接 ...
- Struts2中Action接收参数
Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数: a.定义:在Action类中定义属 ...
- Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法...
Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法 www.MyException.Cn 发布于:2012-09-15 ...
- struts2学习 - action -3 动态方法调用 DMI
Action执行的时候并不一定要执行execute方法 可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐) 配置文 ...
- struts2对action的搜索规则
跟java代码编程相似,struts2对Action类的配置也采用包(package)结构进行管理,如此固然清晰了配置文件的层次结构,提高了配置文件的可读性,然而由于各action分布于不同的包(pa ...
- 单元测试Struts2的Action(包含源码)
很久没有从头搭建Struts2的环境了.最近,认真实践了单元测试Struts2.Spring等Java项目. 今天特意写的是单元测试Struts2的Action,遇到了不少问题,果然是实践出真知啊. ...
- struts2从action向jsp传参数
struts2从action向jsp传参数: 1.在action类里面的成员变量域那里写上你要返回给jsp的变量和相应的get set方法(比如list).. 在execute方法里为list填充了 ...
- Struts2中Action访问Servlet API的三种方法
Struts2的Action并未直接与任何Servlet API耦合,这是Struts2的一个改良之处,因为Action类不再与Servlet API耦合,能更轻松的测试该Action.但如何访问? ...
最新文章
- java超级简单到爆的Excel导入导出(easypoi)
- dos命令行设置网络优先级_计算机网络故障及其维修方法
- CTreeCtrl控件的使用小记
- c语言实现二分法_C语言实现二分法求解方程在区间内的根
- 管家婆SQL SERVER数据库“可能发生了架构损坏。请运行DBCC CHECKCATALOG”修复
- springCloud 微服务框架搭建入门
- MailMail正式发布!注册码免费发放活动开启!(已结束~~不要再回复咧~)
- php扩展leonis,LNMP环境部署
- 51单片机c语言数组怎么用,51单片机之C语言-4.2数组
- 计算机硬件广告语,硬件防毒广告宣传语
- 搭建无线监控云存储服务器,搭建无线监控云存储服务器
- 泛泛而谈的菜鸟学习记录(四)—— Vorley噪声生成原理及噪声边缘提取
- Windows11关机键在哪 Win11系统关机键的位置
- FEDformer: Frequency Enhanced Decomposed Transformer for Long-termSeries Forecasting(ICML2022)
- shell trim函数
- mysql数据库实验3查询_mysql数据库(3)-查询
- Json.NET使用入门(二)【反序列化】
- ASP.NET上传文件出现“404-找不到文件或目录”的解决办法
- Zabbix 实现简单的WEN监测
- 计算机网络毕业设计关于flash,《电大计算机网络毕业论文FLASH爱好者网站;的网页设计与制作》.doc...
热门文章
- tokudb 分形树_分形树Fractal tree介绍——具体如何结合TokuDB还没有太懂,先记住其和LSM都是一样的适合写密集...
- python3类的继承详解_python3 多重继承机制
- 需要vmwareinstalldisk上的文件vmnet_手机上一键就能进行PDF与其他文件的相互转换,果然厉害到不行...
- 前端中什么是中台开发环境_Web前端开发中需要学习什么?会使用到哪些开发工具?...
- C++设计模式-桥接模式
- Qt creator5.7 OpenCV249之resize函数(含源码下载)
- linux mint 19界面美化,安装完 LinuxMint 19.3 后必做的10件事
- 会计云课堂实名认证后怎么更改_会计云课堂怎么听课 网上听课步骤详解
- 初中数学抽象教学的案例_初中八年级数学上册教学视频汇总
- linux18.0.4安装mysql