SpringMVC基础-10-拦截器
2021-05-08 13:33
标签:映射 post请求 tpm def www back bool str char InterceptorTestController: MyFirstInterceptor: MySecondInterceptor: springmvc.xml: web.xml: index.jsp: success.jsp: SpringMVC基础-10-拦截器 标签:映射 post请求 tpm def www back bool str char 原文地址:https://www.cnblogs.com/116970u/p/13178137.html代码示例:
1 package com.atguigu.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5
6 /**
7 * @Title: InterceptorTestController
8 * @Description:
9 * @Author:
10 * @Version: 1.0
11 * @create 2020/6/22 11:08
12 */
13 @Controller
14 public class InterceptorTestController {
15
16 @RequestMapping("/test01")
17 public String test01(){
18 System.out.println("test01...");
19 //int i = 10/0;
20 return "success";
21 }
22 }
1 package com.atguigu.controller;
2
3 import org.springframework.web.servlet.HandlerInterceptor;
4 import org.springframework.web.servlet.ModelAndView;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 /**
10 * 1.实现 HandlerInterceptor接口
11 * 2.在SpringMVC配置文件中注册注册这个拦截器
12 * 配置这个拦截器拦截哪些请求的目标方法
13 */
14 public class MyFirstInterceptor implements HandlerInterceptor {
15
16 /**
17 * 目标方法之前运行
18 */
19 @Override
20 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
21 System.out.println("MyFirstInterceptor...preHandle...");
22 return true;
23 }
24
25 @Override
26 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
27 System.out.println("MyFirstInterceptor...postHandle...");
28 }
29
30 @Override
31 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
32 System.out.println("MyFirstInterceptor...afterCompletion...");
33 }
34 }
1 package com.atguigu.controller;
2
3 import org.springframework.web.servlet.HandlerInterceptor;
4 import org.springframework.web.servlet.ModelAndView;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 /**
10 * @Title: MySecondInterceptor
11 * @Description:
12 * @Author:
13 * @Version: 1.0
14 * @create 2020/6/22 12:58
15 */
16 public class MySecondInterceptor implements HandlerInterceptor {
17
18 /**
19 * 目标方法之前运行
20 */
21 @Override
22 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
23 System.out.println("MySecondInterceptor...preHandle...");
24 return true;
25 }
26
27 @Override
28 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
29 System.out.println("MySecondInterceptor...postHandle...");
30 }
31
32 @Override
33 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
34 System.out.println("MySecondInterceptor...afterCompletion...");
35 }
36 }
1 xml version="1.0" encoding="UTF-8"?>
2 beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
9
10 context:component-scan base-package="com.atguigu">context:component-scan>
11
12 bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
13 property name="prefix" value="/WEB-INF/pages/">property>
14 property name="suffix" value=".jsp">property>
15 bean>
16
17
18 mvc:default-servlet-handler/>
19
20 mvc:annotation-driven/>
21
22
23 mvc:interceptors>
24
25 bean class="com.atguigu.controller.MyFirstInterceptor">bean>
26
27
28 mvc:interceptor>
29
30 mvc:mapping path="/test01"/>
31 bean class="com.atguigu.controller.MySecondInterceptor">bean>
32 mvc:interceptor>
33 mvc:interceptors>
34
35 beans>
1 DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5 web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
6 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
7 id="WebApp_ID" version="3.0">
8 display-name>10.SpringMVC_interceptordisplay-name>
9 welcome-file-list>
10 welcome-file>index.jspwelcome-file>
11 welcome-file-list>
12
13
15 servlet>
16 servlet-name>springDispatcherServletservlet-name>
17 servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
18 init-param>
19
20 param-name>contextConfigLocationparam-name>
21 param-value>classpath:springmvc.xmlparam-value>
22 init-param>
23 load-on-startup>1load-on-startup>
24 servlet>
25
26
27 servlet-mapping>
28 servlet-name>springDispatcherServletservlet-name>
29 url-pattern>/url-pattern>
30 servlet-mapping>
31
32
33
34 filter>
35 filter-name>CharacterEncodingFilterfilter-name>
36 filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
37
38 init-param>
39 param-name>encodingparam-name>
40
41 param-value>utf-8param-value>
42 init-param>
43 init-param>
44
45 param-name>forceEncodingparam-name>
46 param-value>trueparam-value>
47 init-param>
48 filter>
49 filter-mapping>
50 filter-name>CharacterEncodingFilterfilter-name>
51 url-pattern>/*url-pattern>
52 filter-mapping>
53
54
55 filter>
56 filter-name>HiddenHttpMethodFilterfilter-name>
57 filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
58 filter>
59 filter-mapping>
60 filter-name>HiddenHttpMethodFilterfilter-name>
61 url-pattern>/*url-pattern>
62 filter-mapping>
63
66
67 web-app>
1 @ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 html>
5 head>
6 meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 title>Insert title heretitle>
8
9 pageContext.setAttribute("ctp", request.getContextPath());
10 %>
11 head>
12 body>
13 a href="${ctp}/test01">test01a>
14 body>
15 html>
1 @ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 html>
5 head>
6 meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 title>Insert title heretitle>
8 head>
9 body>
10
11 System.out.println("success.jsp....");
12 %>
13 h1>成功!h1>
14 body>
15 html>