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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值