您的位置:首页 > 编程语言 > Java开发

springmvc拦截器处理

2016-07-19 10:20 253 查看
1.拦截器原理同filter类似?

2.拦截器的实现?

  1)首先定义一个类,该类需要继承自HandlerInterceptor接口。

public class UserInterceptor implementsHandlerInterceptor {

 

         @Override

         publicvoid afterCompletion(HttpServletRequest arg0,

                            HttpServletResponsearg1, Object arg2, Exception arg3)

                            throwsException {

                   //TODO Auto-generated method stub

                  

         }

 

         @Override

         publicvoid postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

                            Objectarg2, ModelAndView arg3) throws Exception {

                   //TODO Auto-generated method stub

                  

         }

 

         @Override

         publicboolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

                            Objectarg2) throws Exception {

                   //TODO Auto-generated method stub

                   HttpSessionsession = arg0.getSession();

                   if(session.getAttribute("user")==null){

                            returnfalse;

                   }else{

                       return true;

                   }

         }

2)配置拦截器:在springmvc.xml中配置:特别需要注意在springmvc的声明里需要更改,更改之后的配置信息如下:

<beansxmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:mvc="http://www.springframework.org/schema/mvc"

         xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

       default-autowire="byName"

       >

3)编写相应的拦截器的拦截路径,在springmvc.xml中配置如下:

<mvc:interceptors>

                    <!--使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求  -->

                   <mvc:interceptor>

                       <mvc:mapping path="/admin/*.action"/> 

                       <beanclass="com.gxa.bj.interceptor.UserInterceptor">

                       </bean>

                   </mvc:interceptor>

         </mvc:interceptors>

注意:黄色的这部分的路径配置,表示的是拦截器拦截的是这个路径下的。*表示任意的。

4)示例,比如现在做一个AdminUser的控制器,需要由拦截器来拦截该控制器。

@Controller

@RequestMapping(value="/admin")

public class AdminUserAction {

  @RequestMapping(value="/user.action")

  public void user(){

            System.out.println("请求过来了");

   }

  @RequestMapping(value="/delete.action")

  public void delete(){

            System.out.println("删除的请求");

   }

}

5)如果做的是必须登录之后才能访问的拦截器:

在拦截器里的preHandle方法里:

@Override

         publicboolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

                            Objectarg2) throws Exception {

                   //TODO Auto-generated method stub

                   HttpSessionsession = arg0.getSession();

                  if(session.getAttribute("user")==null){

                            arg0.getRequestDispatcher("../login.jsp").forward(arg0,arg1);

                            returnfalse;

                   }else{

                       return true;

                   }

        

         }

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: