码迷,mamicode.com
首页 > 其他好文 > 详细

interceptor 拦截器

时间:2015-12-17 22:41:44      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:interceptor 拦截器

技术分享技术分享技术分享技术分享技术分享

<?xml version="1.0" encoding= "UTF-8" ?>

<!DOCTYPE struts PUBLIC

     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

     "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name ="struts.devMode" value= "true"></constant >

    <constant name= "struts.enable.DynamicMethodInvocation" value= "true"></constant >

     <package name ="test" namespace="/" extends= "struts-default">

     <!--      定义一个权限控制拦截器   -->

     <interceptors >

           <interceptor name ="authority" class= "com.cola.interceptor.AuthorizationInterceptor" ></interceptor >

     </interceptors >    

     <!--    定义一个全球结果集处理方式,所有经过拦截器的action都会按下面处理跳转 -->  

     <global-results >

           <result name ="login">/login.jsp</ result>

     </global-results >

           <action name ="test" class= "com.cola.action.TestAction" method ="execute">

               <result name ="success" type= "dispatcher">/test.jsp</result >

               <!-- 使用拦截器 -->

               <interceptor-ref name= "defaultStack"></interceptor-ref >

               <interceptor-ref name= "authority"></interceptor-ref >

           </action >

     </package >

     <include file ="struts2.xml"></ include>

</struts>

--------------------------------------------------

package com.cola.interceptor;


import java.util.Map;


import com.cola.bean.User;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;


@SuppressWarnings("all" )

public class AuthorizationInterceptor implements Interceptor{


     @Override

     public void destroy() {

           // TODO Auto-generated method stub

          

     }


     @Override

     public void init() {

           // TODO Auto-generated method stub

          

     }


     @Override

     public String intercept(ActionInvocation ai) throws Exception {

          Map<String,Object> session = ai.getInvocationContext().getSession();

          User user = (User) session.get( "user");

           if( user!= null){

               return ai.invoke();

          }

           return Action. LOGIN;

     }



}


interceptor 拦截器

标签:interceptor 拦截器

原文地址:http://icola.blog.51cto.com/5412204/1725782

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!