• struts2之PreResultListener(转)


    本文转自:http://javaloverlover.iteye.com/blog/1451900

    struts2可由action,拦截器添加PreResultListener监听器,这个监听器可以在转入实际物理视图前回调该监听器的beforeResult()方法。 
    action代码: 

    Java代码  
    1. package com.randy.com;  
    2.   
    3. import java.util.Date;  
    4.   
    5. import com.opensymphony.xwork2.ActionContext;  
    6. import com.opensymphony.xwork2.ActionInvocation;  
    7. import com.opensymphony.xwork2.ActionSupport;  
    8. import com.opensymphony.xwork2.interceptor.PreResultListener;  
    9.   
    10. public class LoginAction extends ActionSupport {  
    11.   
    12.     private static final long serialVersionUID = -2503680075934354752L;  
    13.   
    14.     private String username;  
    15.     private String password;  
    16.   
    17.     public String execute() {  
    18.         ActionInvocation invitation = ActionContext.getContext().getActionInvocation();  
    19.         invitation.addPreResultListener(new PreResultListener() {  
    20.               
    21.             @Override  
    22.             public void beforeResult(ActionInvocation invitation, String resultCode) {  
    23.                 System.out.println(resultCode);  
    24.                 invitation.getInvocationContext().put("extra"new Date() + resultCode+"转入");  
    25.             }  
    26.         });  
    27.         if ("randy".equals(getUsername()) && "jin".equals(getPassword())) {  
    28.             ActionContext.getContext().getSession().put("user", username);  
    29.             return SUCCESS;  
    30.         }  
    31.         return LOGIN;  
    32.     }  
    33.   
    34.     public String getUsername() {  
    35.         return username;  
    36.     }  
    37.   
    38.     public void setUsername(String username) {  
    39.         this.username = username;  
    40.     }  
    41.   
    42.     public String getPassword() {  
    43.         return password;  
    44.     }  
    45.   
    46.     public void setPassword(String password) {  
    47.         this.password = password;  
    48.     }  
    49.   
    50. }  


    jsp代码: 

    Jsp代码  
    1. <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>  
    2. <%@ taglib uri="/struts-tags" prefix="s" %>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    7. <title><s:text name="succPage"/></title>  
    8. </head>  
    9. <body>  
    10.     <s:text name="succTip">  
    11.         <s:param>${sessionScope.user}</s:param>  
    12.     </s:text>  
    13.     <s:property value="username"/>  
    14.     <s:property value="extra"/>  
    15.     <a href="aa.action">ffff</a>  
    16. </body>  
    17. </html>  



     交流群:132607763 若满了,请加:251207741

  • 相关阅读:
    关于HTTP协议,一篇就够了
    jvm在什么情况下会执行GC
    为什么我们做分布式使用Redis?
    linux入门系列
    linux学习笔记-13.进程控制
    linux学习笔记-12.输入输出重定向及管道
    app获取自己的签名
    安卓给微信公众号发消息
    微信扫码下载apk
    设备通道开启关闭状态
  • 原文地址:https://www.cnblogs.com/kivi/p/3191837.html
Copyright © 2020-2023  润新知