spring之拦截器

时间:2024-12-10 17:33:44

拦截器

实现HandlerInterceptor接口:注册拦截器<mvc:inteceptors>

spring和springMVC父子容器的关系

在spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在项目中引入spring和springmvc两个框架,那么它其实就是两个容器,spring是父容器,springmvc是子容器,并且在spring父容器中注册的bean对于springmvc容器中是可见的,而在springmvc容器中注册的bean对于spring父容器中就是不可见的,也就是子容器可以看见父容器中注册的Bean,反之就不行。

全部拦截:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/springmvc/hello" method="post">
姓名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
 package com.bjsxt.handlers;

 import java.util.Map;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; //后台控制器
@Controller
@Scope("prototype")
@RequestMapping("/springmvc")
public class MyController{
//接收json字符串封装成对象
@RequestMapping("/hello")
public String hello1(String username,int age,Model model,Map<String,Object>map,ModelMap modelMap){
System.out.println(username+"______"+age);
model.addAttribute("username", username);
System.out.println("MyController.hello1()");
map.put("age", age);
modelMap.addAttribute("gender", "female");
System.out.println("MyController.hello1()");
return "welcome"; } }
 package com.bjsxt.interceptors;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
//自定义拦截器
public class FistInterceptor implements HandlerInterceptor {
//该方法执行时机:处理器方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("拦截器执行");
return false;
} //该方法执行时机:所有方法处理完成执行之后,响应给浏览器客户端执行
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub } //该方法执行时机:处理器方法执行之后
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub } }
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
<!-- 注册注解驱动 -->
<mvc:annotation-driven/>
<!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="" class="com.bjsxt.interceptors.FistInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 静态资源无法访问第二种解决方案 -->
<!-- <mvc:default-servlet-handler/> -->
<!-- 静态资源无法访问第三种解决方案 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎页面!${username}--${age}--${gender}
</body>
</html>

指定拦截

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/springmvc/hello2" method="POST">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
</body>
</html>
 package com.bjsxt.handlers;

 import java.util.Map;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; //后端控制器
@Controller
@Scope("prototype")
@RequestMapping("/springmvc")
public class MyController{
@RequestMapping("/hello")
public String hello1(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){
System.out.println(username + " ----------"+age);
model.addAttribute("username", username);
map.put("age", age);
modelMap.addAttribute("gender", "female");
return "welcome";
}
@RequestMapping("/hello2")
public String hello2(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){
System.out.println(username + " 2222----------2222"+age);
model.addAttribute("username", username);
map.put("age", age);
modelMap.addAttribute("gender", "female");
return "welcome";
} }
 package com.bjsxt.interceptors;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; //自定义拦截器
public class FirstInterceptor implements HandlerInterceptor {
//该方法执行时机:处理器方法执行之前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("拦截器preHandle()执行!");
return true;
} //该方法执行时机:处理器方法执行之后执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("拦截器postHandle()执行!"); } //该方法执行时机:所有工作处理完成之后,响应给浏览器客户端之前执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("拦截器afterCompletion()执行!"); } }
 <?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
<!-- 注册注解驱动 -->
<mvc:annotation-driven/>
<!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/springmvc/hello"/>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/springmvc/hello2"/>
<bean id="" class="com.bjsxt.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> <!-- 静态资源无法访问第二种解决方案 -->
<!-- <mvc:default-servlet-handler/> -->
<!-- 静态资源无法访问第三种解决方案 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>