• Struts2拦截器


    实现用户登录过滤

    UserLoginInterceptor.java

     1 package com.tcf.inter;
     2 
     3 import java.util.List;
     4 
     5 import com.opensymphony.xwork2.Action;
     6 import com.opensymphony.xwork2.ActionContext;
     7 import com.opensymphony.xwork2.ActionInvocation;
     8 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     9 import com.tcf.entity.User;
    10 
    11 public class UserLoginInterceptor extends AbstractInterceptor {
    12     private String excludeMethods;
    13     
    14     public String getExcludeMethods() {
    15         return excludeMethods;
    16     }
    17 
    18     public void setExcludeMethods(String excludeMethods) {
    19         this.excludeMethods = excludeMethods;
    20     }
    21 
    22     @Override
    23     public String intercept(ActionInvocation arg0) throws Exception {
    24         User user = (User) ActionContext.getContext().getSession().get("userInfo");
    25         String name = ActionContext.getContext().getName();
    26         String[] actions = excludeMethods.split(",");
    27         
    28         if(user == null && !contains(actions,name)){
    29             return Action.INPUT;
    30         }
    31         arg0.invoke();
    32         return Action.SUCCESS;
    33     }
    34     
    35     public boolean contains(String[] list,String str){
    36         for(String s:list){
    37             if(s.equals(str)){
    38                 return true;
    39             }
    40         }
    41         return false;
    42     }
    43 }

    struts.xml

     1 <interceptors>
     2     <interceptor name="userInter" class="com.tcf.inter.UserLoginInterceptor"></interceptor>
     3     <interceptor-stack name="allInter">
     4         <interceptor-ref name="userInter">
     5             <!--不进行拦截的action名称 -->
     6             <param name="excludeMethods">loginUser,registerUser,upload,uploades,download</param>
     7         </interceptor-ref>
     8         <interceptor-ref name="defaultStack" />
     9     </interceptor-stack>
    10 </interceptors>
    11 <default-interceptor-ref name="allInter"></default-interceptor-ref>
  • 相关阅读:
    ZT:POI操作PPT
    ADO.NET 2.0 Dataset和Datatable 新功能新特性
    ADO.NET 2.0 大批量数据操作和多个动态的结果集
    浏览.NET Framework 2.0 类型库中新增的常用功能
    ASP.NET程序中常用代码汇总(四)
    保存div与页面滚动条的位置
    在ASP.NET中实现AJAX
    ASP.NET 2.0中隐藏listbox的某一项
    泛型最佳实践C#
    ASP.NET 2.0 验证控件新的功能
  • 原文地址:https://www.cnblogs.com/yunwei1237/p/5899599.html
Copyright © 2020-2023  润新知