今天才知道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 接口实现数据准备的更多相关文章

  1. Struts2实现Preparable接口和【struts2】继承ActionSupport类

    Struts2实现Preparable接口 实现preparable接口,实现public void prepare() throws Exception 方法.当你访问某问action指定方法之前, ...

  2. struts2中从后台读取数据到<s:select>

    看到网上好多有struts2中从后台读取数据到<s:select>的,但都 不太详细,可能是我自己理解不了吧!所以我自己做了 一个,其中可能 有很多不好的地方,望广大网友指出 结果如图 p ...

  3. struts2(三)---struts2中的服务端数据验证框架validate

    struts2为我们提供了一个很好的数据验证框架–validate,该框架可以很方便的实现服务端的数据验证. ActionSupport类提供了一个validate()方法,当我们需要在某一个acti ...

  4. mock模拟接口返回数据

    mock,是python中模拟接口返回数据 1.安装 pip install mock import unittest from mock import Mock def add(a,b): pass ...

  5. Struts2中validate数据校验的两种常用方法

    本文主要介绍Struts2中validate数据校验的两种方法及Struts2常用校验器.  1.Action中的validate()方法 Struts2提供了一个Validateable接口,这个接 ...

  6. Struts2中的数据类型转换

    Struts2对数据的类型转换 一.Struts2中自带类型转换拦截器 Struts2内部提供了大量转换器,用来完成数据类型转换的问题,有如下 * boolean 和 Boolean * char和 ...

  7. struts2中数据的传输

    1.传统的写多个request接受参数方法. 2.struts2中的多个setter方法,getter方法 3.利用实体bean,让strut2 实例bean,少写setter方法,getter方法, ...

  8. 简述在ADO中使用接口的抽象数据提供程序以及ADO.NET数据提供程序工厂模型

    如何在ADO中使用接口的抽象数据提供程序 在cofig中 appSettings下,配置数据连接类型 <appSettings> <!--这个键值映射到枚举值中的某个值--> ...

  9. struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的?

    struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的? 解答: 1)可以直接通过与表单元素相同名称的数据成员(需要存在符合命名规范set和get ...

随机推荐

  1. 《Linux 内核分析》第五周

    [李行之原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] <Linux内 ...

  2. 读书笔记(chapter5)

    系统调用 5.1与内核通信 1.系统调用在用户空间进程和硬件设备之间添加一个中间层.作用有三个:它为用户空间提供了一种硬件的抽象接口:系统调用保证了系统的稳定和安全:系统调用是用户空间访问内核的唯一手 ...

  3. Docker(一)-Docker介绍

    什么就Docker? Docker是一个开源项目, 诞生于2013年初,最初是dotCloud公司内部的一个业余项目.它基于Google公司推出的Go语言实现.项目后来加入了Linux基金会,遵从了A ...

  4. SSR & Next.js & Nuxt.js

    SSR & Next.js & Nuxt.js Server Side Rendering https://nextjs.org/ https://nuxtjs.org/ SSR &a ...

  5. 美团 OCTO 分布式服务治理系统

    OCTO 是美团千亿调用量的分布式服务通信框架及服务治理的系统,可实现服务注册.服务自动发现.服务管理.容错处理.数据可视化.服务监控报警.服务分组等.本文总结了 OCTO 架构原理.Java 应用的 ...

  6. Spring之c3p0连接池xml配置和使用举例

    1.导入jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.11.jar 2.源码: beans.xml <beans xmlns="http ...

  7. 初识elasticsearch_1(基本概念和基本操作)

    初识 ElasticSearch是一个基于Lucene的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.本博客部分基于es的官方文档.es的官方文档网址如下:h ...

  8. MT【92】空间余弦定理解题

    评:学校常规课堂教学里很少讲到这个,有点可惜.

  9. 【bzoj1492】 NOI2007—货币兑换Cash

    http://www.lydsy.com/JudgeOnline/problem.php?id=1492 (题目链接) 题意 两种金券,金券按照比例交易:买入时,将投入的资金购买比例为$rate[i] ...

  10. Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流)

    Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流) Description 问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同 ...