zoukankan      html  css  js  c++  java
  • 拦截器(Interceptor)中的invocation.invoke()

      1 关于在Struts2的自定义的验证拦截器(Interceptor)中的invocation.invoke()是什么意思?
      2 
      3  
      4 
      5  
      6 package com.xjtu.interceptor;  
      7   
      8 import com.opensymphony.xwork2.ActionContext;  
      9 import com.opensymphony.xwork2.ActionInvocation;  
     10 import com.opensymphony.xwork2.interceptor.Interceptor;  
     11   
     12 public class PermissionInterceptor implements Interceptor {  
     13   
     14     /** 
     15      *  
     16      */  
     17     private static final long serialVersionUID = 1L;  
     18   
     19     @Override  
     20     public void destroy() {  
     21         // TODO Auto-generated method stub  
     22   
     23     }  
     24   
     25     @Override  
     26     public void init() {  
     27         // TODO Auto-generated method stub  
     28   
     29     }  
     30   
     31     @Override  
     32     public String intercept(ActionInvocation invocation) throws Exception {  
     33         System.out.println("进入拦截器");  
     34         Object user = ActionContext.getContext().getSession().get("user");  
     35         System.out.println(user);  
     36         if (user != null) {  
     37             return invocation.invoke();  
     38         }  
     39         ActionContext.getContext().put("message", "您无权执行该操作!");  
     40         return "success";  
     41     }  
     42   
     43 }  
     44 
     45 
     46 
     47  
     48 
     49  
     50 package com.xjtu.soft;  
     51   
     52 public class HelloWorldAction {  
     53     private String message;  
     54   
     55     public String getMessage() {  
     56         return message;  
     57     }  
     58       
     59     public void setMessage(String message) {  
     60         this.message = message;  
     61     }  
     62       
     63     public String execute(){  
     64         System.out.println("执行execute");  
     65         message = "execute";  
     66         return "success";  
     67     }  
     68   
     69     public String addUI() {  
     70         System.out.println("执行addUI");  
     71         message = "addUI";  
     72         return "success";  
     73     }  
     74   
     75   
     76 }  
     77 
     78 
     79 
     80 struts.xml配置自定义的拦截器如下:
     81 
     82  
     83 
     84  
     85 <?xml version="1.0" encoding="UTF-8" ?>  
     86 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
     87 <struts>  
     88     <constant name="struts.action.extension" value="do,action" />  
     89     <constant name="struts.multipart.maxSize" value="10485760" />  
     90   
     91   
     92     <package name="itcast" namespace="/test" extends="struts-default">  
     93         <interceptors>  
     94             <interceptor name="permission"  
     95                 class="com.xjtu.interceptor.PermissionInterceptor" />  
     96             <interceptor-stack name="permissionstack">  
     97                 <interceptor-ref name="defaultStack"></interceptor-ref>  
     98                 <interceptor-ref name="permission"></interceptor-ref>  
     99             </interceptor-stack>  
    100         </interceptors>  
    101           
    102         <global-results>  
    103             <result name="success">/WEB-INF/page/message.jsp</result>  
    104         </global-results>  
    105   
    106         <action name="list_*" class="com.xjtu.soft.HelloWorldAction"  
    107             method="{1}">  
    108             <interceptor-ref name="permissionstack"></interceptor-ref>  
    109         </action>  
    110     </package>  
    111   
    112 </struts>      
    113 
    114 
    115 
    116 
    117 invocation.invoke() 就是通知struts2接着干下面的事情
    118 比如 调用下一个拦截器 或 执行下一个Action
    119 就等于退出了你自己编写的这个interceptor了
    120  
    121 
    122 如何使用struts2拦截器,或者自定义拦截器。特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox拦截器):
    123 
    124 <interceptor-ref name="checkbox">
    125   <param name="uncheckedValue">0</param>
    126 </interceptor-ref>
    127 <interceptor-ref name="defaultStack"/>
    128 ******(必须加,否则出错)
  • 相关阅读:
    51nod1381 硬币游戏
    51nod1381 硬币游戏
    51nod1384 全排列
    LOJ P10130 点的距离 题解
    POJ P1985 Cow Marathon 题解
    求树的直径(两种方法)
    洛谷 P3518 [POI2011] SEJ-Strongbox 题解
    洛谷 UVA12101 Prime Path 题解
    POJ P2251 Dungeon Master 题解
    POJ P3009 Curling 2.0 题解
  • 原文地址:https://www.cnblogs.com/wyb628/p/6395853.html
Copyright © 2011-2022 走看看