服务器之家

服务器之家 > 正文

SpringMVC拦截器详解

时间:2021-10-14 11:55     来源/作者:池鱼i_

web.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
 
<!--    配置springMvc中央处理器-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        初始化的时候配置加载springmvc的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
<!--    3、修改web.xml中的请求路径地址方式-->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
<!--        3、修改web.xml中的请求路径地址方式-->
        <url-pattern>/</url-pattern>
<!--        <url-pattern>*.do</url-pattern>-->
    </servlet-mapping>
 
<!--
 1、将css文件使用默认处理方式进行处理,不再交由中央控制器处理,
         设置路径的更高匹配优先级
 -->
<!--    <servlet-mapping>-->
<!--        <servlet-name>default</servlet-name>-->
<!--        <url-pattern>*.css</url-pattern>-->
<!--        <url-pattern>*.js</url-pattern>-->
<!--        <url-pattern>*.jpg</url-pattern>-->
<!--        <url-pattern>*.png</url-pattern>-->
<!--        <url-pattern>*.html</url-pattern>-->
<!--    </servlet-mapping>-->
 
 
</web-app>

springMVC配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">
    <!--自动扫描包-->
    <!-- 开启ioc   注解事务支持-->
    <context:component-scan base-package="cn"></context:component-scan>
    <!--开启spiring mvc注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--配置spring 中的视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 
    <mvc:interceptors>
 
                <bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/test1.do"/>
            <mvc:exclude-mapping path="/test2.do"/>
            <bean id="interceptor2" class="cn.hp.interceptor.TestInterceptor2"></bean>
        </mvc:interceptor>
 
   
 
</beans>

controller类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.hp.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
public class UserAction {
    @RequestMapping("/test1.do")
    public  String test01(){
        System.out.println("正在执行test1这个业务逻辑");
        return "index";
    }
    @RequestMapping("/test2.do")
    public  String test02(){
        System.out.println("正在执行test2这个业务逻辑");
        return "index";
    }
}

第一个拦截器配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.hp.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("第一个拦截器,执行业务逻辑之前执行此方法");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("第一个拦截器,执行业务逻辑之后,视图渲染之前执行此方法");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("第一个拦截器,执行视图渲染之后执行此方法");
    }
}

第二个拦截器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.hp.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("第二个拦截器,执行业务逻辑之前执行此方法");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("第二个拦截器,执行业务逻辑之后,视图渲染之前执行此方法");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("第二个拦截器,执行视图渲染之后执行此方法");
    }
}

SpringMVC拦截器详解

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/best_p1/article/details/118637785

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部