dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
/
spring-core.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<context:component-scan base-package=“com.gxwz.code” />
<context:component-scan base-package=“com.gxwz.core” />
spring-mvc.xml (关键配置文件)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.or 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 g/schema/beans”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
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 ">
<context:component-scan base-package=“com.gxwz.web” />
<mvc:annotation-driven />
mvc:interceptors
mvc:interceptor
<mvc:mapping path=“/**”/>
</mvc:interceptor>
</mvc:interceptors>
IndexInterceptor.java
package com.gxwz.interceptor;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
-
在业务处理器处理请求之前被调用
-
如果返回false
-
从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
-
如果返回true
-
执行下一个拦截器,直到所有的拦截器都执行完毕
-
再执行被拦截的Controller
-
然后进入拦截器链,
-
从最后一个拦截器往回执行所有的postHandle()
-
接着再从最后一个拦截器往回执行所有的afterCompletion()
-
与过滤器的区别
-
1.过滤器是依赖于Servlet容器,基于回调函数,Interceptor依赖与框架,基于反射机制
-
2.过滤器的过滤范围更大,还可以过滤一些静态资源,拦截器只拦截请求
*/
public class IndexInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,
Object handler) throws Exception {
//业务逻辑代码编写…(如:解决乱码,权限验证)
System.out.println(“preHandle(), 在访问Controller之前被调用”);
return true;
}
/**