HandlerInterceptor xml版本使用

实验了一下,两步

1. 自定义interceptor 实现 HandlerInterceptor

2. 将自定义的interceptor注册 (xml)

 

 

1. 对所有的path都生效的interceptors这样写就行了,不用写interceptor区分,不用写path

springMVC.xml:

    <mvc:interceptors>
            <bean class="inteceptor.HandlerInteceptor1"></bean>
    </mvc:interceptors>

 HiController:


package com.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HiController {
@ResponseBody
@RequestMapping("/hi")
public String getHi() {
System.out.println("execute hi controller");
return "say hi";
}

@ResponseBody
@RequestMapping("/hell")
public String getHell() {
System.out.println("execute hell controller");
return "say hell";
}
}
HandlerInteceptor1:
package inteceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HandlerInteceptor1 implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("pre");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("after");
    }
}

访问http://localhost:8080/mvc/hi  和  http://localhost:8080/mvc/hell  两个路径拦截器都生效了:

 

 

 

2. 如果需要对不同的路径,提供不同的拦截器:

springMVC.xml就要这样做了 , 路径可以使用通配符,也可以不用

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hi/**"/>
            <bean class="inteceptor.HandlerInteceptor1"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

访问http://localhost:8080/mvc/hi  和  http://localhost:8080/mvc/hell  只有/hi路径拦截器生效了:

 

 3.所以如果想对不同的路径有不同的拦截器,再创建一个interceptor:

HandlerInterceptor2:
package inteceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HandlerInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("pre2");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post2");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("after2");
    }
}

springMVC.xml:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hi/**"/>
            <bean class="inteceptor.HandlerInteceptor1"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/hell/**"/>
            <bean class="inteceptor.HandlerInterceptor2"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

访问http://localhost:8080/mvc/hi  和  http://localhost:8080/mvc/hell   拦截器1 2 分别生效了:

 

 

到此,xml版本怎么写的全部实验完成。

还有一个问题, 用java是怎么注册interceptor的?

 

posted @ 2020-10-16 13:46  圣金巫灵  阅读(199)  评论(0编辑  收藏  举报