• 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

  • 相关阅读:
    黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战a基础篇
    单点登录业务逻辑解析
    C++ do{ } while(0)
    esp8266+MQTT+DHT11(温湿度计) platformio
    ESP8266+ MQTT+SG90(舵机) platformio
    esp8266+http (PlatformIO)
    ESP32+L298N+MQTT+4G无线远程监控+四驱动小破车
    ESP8266 + MQTT + 土壤湿度传感器
    esp8266 + mqtt + 温度计 (platformio)
    根据配置生成表格布局
  • 原文地址:https://www.cnblogs.com/kivi/p/3191837.html
Copyright © 2020-2023  润新知