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 执行前面
  这是所有方法前的处理

 

posted @   QiaoZhi  阅读(1005)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2017-07-30 Spring使用注解和struts集成
2017-07-30 404错误处理以及以后缀为action结尾的处理
2017-07-30 Struts局部异常与全局异常处理
点击右上角即可分享
微信分享提示