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 ...
随机推荐
- 第二阶段Sprint7
昨天:将“录制”及“保存”整合到一起,修复出现的Bug,使之能够正常运行. 今天:把视频录制整合到时间提醒里,实现视频提醒 遇到的问题:额,整进去直接就停止运行了..也没有报错..
- 云平台项目--学习经验--打包压缩工具requirejs
requirejs是一个JavaScript模块加载器.适合在浏览器中国使用,也可以在其他脚本环境使用,它鼓励了代码的模块化.使用RequireJS加载模块化脚本将提高代码的加载速度和质量.如何加载R ...
- TCP/IP 之 大明王朝邮差 (转)
原创: 刘欣 码农翻身 2016-05-12 前言: 本文主要想说一下TCP的知识, 比喻有不恰当之处,敬请包涵. 大明王朝天启四年, 清晨. 天色刚蒙蒙亮,我就赶着装满货物的马车来到了南城门, 这里 ...
- json-server(copy)
https://blog.csdn.net/wangle_style/article/details/79455508(原文章地址) 新版vue-cli如何使用json-server来mork 原创 ...
- 解决sublime text3下中文无法输入的问题(Ubuntu)
sublime-text-imfix,非常无脑.就喜欢这样的.
- 【大数据】Spark性能优化和故障处理
第一章 Spark 性能调优 1.1 常规性能调优 1.1.1 常规性能调优一:最优资源配置 Spark性能调优的第一步,就是为任务分配更多的资源,在一定范围内,增加资源的分配与性能的提升是成正比的, ...
- BZOJ4520 CQOI2016K远点对(KD-Tree+堆)
堆维护第k大,每个点KD-Tree上A*式查询较远点,跑得飞快,复杂度玄学. #include<iostream> #include<cstdio> #include<c ...
- 【Revit API】调用Revit内部命令PostableCommand
Revit内置了一些命令,直接调用Revit操作方式. 可以去API文档查询PostableCommand枚举,还是很多的. 话不多说,直接上代码 var commandId = RevitComma ...
- BZOJ 4380 [POI2015]Myjnie | DP
链接 BZOJ 4380 题面 有n家洗车店从左往右排成一排,每家店都有一个正整数价格p[i]. 有m个人要来消费,第i个人会驶过第a[i]个开始一直到第b[i]个洗车店,且会选择这些店中最便宜的一个 ...
- WEB下载显示下载名称乱码--java
将文件名编码转换为ISO8859-1即可,如下 downloadFileName = new String(fileName.getBytes("gbk"), "ISO8 ...