码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc自定义拦截器

时间:2015-07-25 20:07:06      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:拦截器

  1. 首先在SpringMvc.xml中加入自己定义的拦截器

<mvc:interceptors>

        <mvc:interceptor>

        <mvc:mapping path="/manager/**"/>//拦截改路径的url请求

            <bean id="mLoginInterceptor" class="com.usermanage.util.MLoginInterceptor"/>

        </mvc:interceptor>

      </mvc:interceptors>

2.任何访问我定义拦截的url都会返回登录页面

package com.usermanage.util;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import com.usermanage.entity.Tmanager;


public class MLoginInterceptor extends HandlerInterceptorAdapter {

 

    @Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String url = request.getRequestURL().toString();

        String path = request.getContextPath();

        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

        if (request.getSession().getAttribute("mloginUser") == null){

        System.out.println(request.getSession().getAttribute("loginUser"));

        response.sendRedirect(basePath+"index.jsp");

        return false;

        }else{

        System.out.println("sdsjkd");

        return true;

        }

           

            //request.getRequestDispatcher("redirect:toIndexHome").forward(request, response);

    }

 

    @Override

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

   

        super.postHandle(request, response, handler, modelAndView);

       

    }

}


SpringMvc自定义拦截器

标签:拦截器

原文地址:http://hatch.blog.51cto.com/9349645/1678324

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