struts2 中 Preparable 接口实现数据准备
今天才知道struts还有Preparable接口,实现此接口需要实现其prepare()方法,调用action中其他方法之前会先调用prepare()方法。此接口和方法可以用于初始化一些数据。
测试代码:
package cn.qlq.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable; @Namespace("/")
@ParentPackage("default")
public class FirstAction extends ActionSupport implements Preparable { private static final long serialVersionUID = 1L;
private String test;
@Override
public void prepare() throws Exception {
System.out.println("这是所有方法前的处理");
} @Action(value = "test",
results = { @Result(name = "success1", location = "/index2.jsp", type = "redirect") ,
@Result(name = "error", location = "/index2.jsp") ,
@Result(name = "success" ,type = "json" , params = {"root","test"})
})
@Override
public String execute() throws Exception {
test = "test";
return super.execute();
} public String getTest() {
return test;
} public void setTest(String test) {
this.test = test;
} }
当我们访问execute方法的时候会先执行prepare()方法。
另外,当action种有一个方法叫做haha(),我们可以定义一个prepareHaha()方法,则在访问haha()之前会先访问prepareHaha(),再访问prepare(),最后访问haha(),如下代码:
package cn.qlq.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable; @Namespace("/")
@ParentPackage("default")
public class FirstAction extends ActionSupport implements Preparable { private static final long serialVersionUID = 1L;
private String test;
@Override
public void prepare() throws Exception {
System.out.println("这是所有方法前的处理");
} @Action(value = "test",
results = { @Result(name = "success1", location = "/index2.jsp", type = "redirect") ,
@Result(name = "error", location = "/index2.jsp") ,
@Result(name = "success" ,type = "json" , params = {"root","test"})
})
@Override
public String execute() throws Exception {
test = "test";
return super.execute();
} public void prepareHaha() {
System.out.println("haha 执行前面");
}
@Action(value = "haha" ,results ={@Result(name = "success", location = "/index2.jsp", type = "redirect")} )
public String haha() throws Exception {
return super.execute();
} public String getTest() {
return test;
} public void setTest(String test) {
this.test = test;
} }
结果:
haha 执行前面
这是所有方法前的处理
struts2 中 Preparable 接口实现数据准备的更多相关文章
- Struts2实现Preparable接口和【struts2】继承ActionSupport类
Struts2实现Preparable接口 实现preparable接口,实现public void prepare() throws Exception 方法.当你访问某问action指定方法之前, ...
- struts2中从后台读取数据到<s:select>
看到网上好多有struts2中从后台读取数据到<s:select>的,但都 不太详细,可能是我自己理解不了吧!所以我自己做了 一个,其中可能 有很多不好的地方,望广大网友指出 结果如图 p ...
- struts2(三)---struts2中的服务端数据验证框架validate
struts2为我们提供了一个很好的数据验证框架–validate,该框架可以很方便的实现服务端的数据验证. ActionSupport类提供了一个validate()方法,当我们需要在某一个acti ...
- mock模拟接口返回数据
mock,是python中模拟接口返回数据 1.安装 pip install mock import unittest from mock import Mock def add(a,b): pass ...
- Struts2中validate数据校验的两种常用方法
本文主要介绍Struts2中validate数据校验的两种方法及Struts2常用校验器. 1.Action中的validate()方法 Struts2提供了一个Validateable接口,这个接 ...
- Struts2中的数据类型转换
Struts2对数据的类型转换 一.Struts2中自带类型转换拦截器 Struts2内部提供了大量转换器,用来完成数据类型转换的问题,有如下 * boolean 和 Boolean * char和 ...
- struts2中数据的传输
1.传统的写多个request接受参数方法. 2.struts2中的多个setter方法,getter方法 3.利用实体bean,让strut2 实例bean,少写setter方法,getter方法, ...
- 简述在ADO中使用接口的抽象数据提供程序以及ADO.NET数据提供程序工厂模型
如何在ADO中使用接口的抽象数据提供程序 在cofig中 appSettings下,配置数据连接类型 <appSettings> <!--这个键值映射到枚举值中的某个值--> ...
- struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的?
struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的? 解答: 1)可以直接通过与表单元素相同名称的数据成员(需要存在符合命名规范set和get ...
随机推荐
- 2-Sixteenth Scrum Meeting-20151216
任务安排 成员 今日完成 明日任务 闫昊 写完学习进度记录的数据库操作 写完学习进度记录的数据库操作 唐彬 编写与服务器交互的代码 编写与服务器交互的代码 史烨轩 获取视频url 余帆 本地 ...
- 使用Samba服务程序,让linux系统之间共享文件
yum install -y cifs-utils mkdir /database 创建挂载目录 在root家目录创建认证文件(依次为SMB用户名.SMB用户密码.SMB共享域) v ...
- Docker(七)-Dcoker常用命令
容器生命周期管理 run start/stop/restart kill rm pause/unpause create exec 容器操作 ps inspect top attach events ...
- Oracle 12c 之前的版本路线图
- 4 vuex的安装
安装可以看,引入又问题https://blog.csdn.net/u014196765/article/details/78022065?locationNum=9&fps=1(引入) htt ...
- idea中 mybatis的debug文件需要放在src的目录下 不能加多余的路径
- 【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp
题目描述 给出 $A$ 串和 $B$ 串,从 $A$ 串中选出至多 $x$ 个互不重合的段,使得它们按照原顺序拼接后能够得到 $B$ 串.求是否可行.多组数据. $T\le 10$ ,$|A|,|B| ...
- 【题解】 bzoj1911: [Apio2010]特别行动队 (动态规划+斜率优化)
bzoj1911,懒得复制,戳我戳我 Solution: 线性DP(打牌) \(dp\)方程还是很好想的:\(dp[i]=dp[j-1]+a*(s[i]-s[j-1])^2+b*(s[i]-s[j-1 ...
- 【题解】 [HNOI2015]落忆枫音 (拓扑排序+dp+容斥原理)
原题戳我 Solution: (部分复制Navi_Aswon博客) 解释博客中的两个小地方: \[\sum_{\left(S是G中y→x的一条路径的点集\right))}\prod_{2≤j≤n,(j ...
- springboot配置hibernate jpa多数据源
这里我用的springboot项目,配置文件yml文件配置,gradle配置jar包依赖. 找了一天资料,终于整好了多数据源,步骤如下: application.yml: spring: dataso ...