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

SpringMVC基础-10-拦截器

时间:2020-06-22 18:37:35      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:映射   post请求   tpm   def   www   back   bool   str   char   

技术图片

 

代码示例:

InterceptorTestController:

 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 }

MyFirstInterceptor:

 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 }

MySecondInterceptor:

 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 }

springmvc.xml:

 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     <!-- springmvc可以保证动态请求和静态请求都能访问 -->
20     <mvc:annotation-driven/>
21 
22     <!--拦截器配置-->
23     <mvc:interceptors>
24         <!--配置某个拦截器:默认是拦截所有请求的-->
25         <bean class="com.atguigu.controller.MyFirstInterceptor"></bean>
26 
27         <!--配置某个拦截器更详细的信息-->
28         <mvc:interceptor>
29             <!--只拦截test01请求-->
30             <mvc:mapping path="/test01"/>
31             <bean class="com.atguigu.controller.MySecondInterceptor"></bean>
32         </mvc:interceptor>
33     </mvc:interceptors>
34 
35 </beans>

web.xml:

 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_interceptor</display-name>
 9     <welcome-file-list>
10         <welcome-file>index.jsp</welcome-file>
11     </welcome-file-list>
12 
13     <!-- The front controller of this Spring Web application,
14     responsible for handling all application requests -->
15     <servlet>
16         <servlet-name>springDispatcherServlet</servlet-name>
17         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
18         <init-param>
19             <!-- contextConfigLocation:指定SpringMVC配置文件位置 -->
20             <param-name>contextConfigLocation</param-name>
21             <param-value>classpath:springmvc.xml</param-value>
22         </init-param>
23         <load-on-startup>1</load-on-startup>
24     </servlet>
25 
26     <!-- Map all requests to the DispatcherServlet for handling -->
27     <servlet-mapping>
28         <servlet-name>springDispatcherServlet</servlet-name>
29         <url-pattern>/</url-pattern>
30     </servlet-mapping>
31 
32     <!-- 配置一个字符编码的Filter;一定注意:字符编码filter一般都在其他Filter之前; -->
33     <!--配置字符集编码的Filter-->
34     <filter>
35         <filter-name>CharacterEncodingFilter</filter-name>
36         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
37         <!-- encoding:指定解决POST请求乱码 -->
38         <init-param>
39             <param-name>encoding</param-name>
40             <!--不区分大小写-->
41             <param-value>utf-8</param-value>
42         </init-param>
43         <init-param>
44             <!-- forceEncoding:顺手解决响应乱码;response.setCharacterEncoding(this.encoding); -->
45             <param-name>forceEncoding</param-name>
46             <param-value>true</param-value>
47         </init-param>
48     </filter>
49     <filter-mapping>
50         <filter-name>CharacterEncodingFilter</filter-name>
51         <url-pattern>/*</url-pattern>
52     </filter-mapping>
53 
54     <!--支持Rest风格的Filter(开启PUT、DELETE请求)-->
55     <filter>
56         <filter-name>HiddenHttpMethodFilter</filter-name>
57         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
58     </filter>
59     <filter-mapping>
60         <filter-name>HiddenHttpMethodFilter</filter-name>
61         <url-pattern>/*</url-pattern>
62     </filter-mapping>
63     <!-- 使用SpringMVC前端控制器写完就直接写字符编码过滤器;
64         Tomcat一装上,上手就是server.xml的8080处添加URIEncoding="UTF-8"
65      -->
66 
67 </web-app>

index.jsp:

 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 here</title>
 8     <%
 9         pageContext.setAttribute("ctp", request.getContextPath());
10     %>
11 </head>
12 <body>
13     <a href="${ctp}/test01">test01</a>
14 </body>
15 </html>

success.jsp:

 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 here</title>
 8 </head>
 9 <body>
10 <%
11 System.out.println("success.jsp....");
12 %>
13 <h1>成功!</h1>
14 </body>
15 </html>

 

SpringMVC基础-10-拦截器

标签:映射   post请求   tpm   def   www   back   bool   str   char   

原文地址:https://www.cnblogs.com/116970u/p/13178137.html

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