今天我们来讨论下struts的监听器,我们知道,在struts的action里面处理完事件之后,我们会通过返回一个字符串的,通过struts的配置文件,跳转到相应的页面,我们在跳转之前可以做一些事情,比如写入日志之类的工作,struts提供了一个监听器在方便我们完成这个工作,具体的代码实现如下:
01 | public class LoginAction extends ActionSupport { |
05 | public String getName() { |
09 | public void setName(String name) { |
14 | public String execute() throws Exception { |
15 | ActionInvocation actionInvocation = ActionContext.getContext() |
16 | .getActionInvocation(); |
17 | actionInvocation.addPreResultListener( new PreResultListener() { |
20 | public void beforeResult(ActionInvocation arg0, String arg1) { |
21 | System.out.println( "返回之前拦截一下,也可以在这里加入日志" ); |
24 | System.out.println( "准备跳转了" ); |
后台打印如下: