struts2 监听器

来源:互联网 发布:单片机与usb连接电路图 编辑:程序博客网 时间:2024/06/05 15:05

今天我们来讨论下struts的监听器,我们知道,在struts的action里面处理完事件之后,我们会通过返回一个字符串的,通过struts的配置文件,跳转到相应的页面,我们在跳转之前可以做一些事情,比如写入日志之类的工作,struts提供了一个监听器在方便我们完成这个工作,具体的代码实现如下:

01public class LoginAction extends ActionSupport { 
02   
03    private String name; 
04   
05    public String getName() { 
06        return name; 
07    
08   
09    public void setName(String name) { 
10        this.name = name; 
11    
12   
13    @Override 
14    public String execute() throws Exception { 
15        ActionInvocation actionInvocation = ActionContext.getContext() 
16                .getActionInvocation(); 
17        actionInvocation.addPreResultListener(new PreResultListener() { 
18               
19            @Override 
20            public void beforeResult(ActionInvocation arg0, String arg1) { 
21                System.out.println("返回之前拦截一下,也可以在这里加入日志"); 
22            
23        }); 
24        System.out.println("准备跳转了"); 
25        return SUCCESS; 
26    
27}

后台打印如下:

1准备跳转了 
2返回之前拦截一下,也可以在这里加入日志