Springmvc的jar包下载:https://pan.baidu.com/s/1Uu5R96z4LwwtydGq4B60Xg 密码:8c3n
Interceptor1.java(自定义的拦截器,实现HandlerInterceptor接口):
package com.xxx.springmvc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
//自定义的拦截器
public class Interceptor1 implements HandlerInterceptor { //实现HandlerInterceptor接口
// Controller中的方法执行前调用此方法
// 返回true:继续执行; false:终止执行
// 这里可以加入登录校验、权限拦截等
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
System.out.println("方法执行前");
//判断用户是否登陆。 如果没有登陆,重定向到登陆页面;如果登陆了,就放行了
// URL http://localhost:8080/项目名/login.action
// URI /项目名/login.action
String requestURI = request.getRequestURI();
if(!requestURI.contains("/login")){
String username = (String) request.getSession().getAttribute("USER_SESSION");
if(null == username){
response.sendRedirect(request.getContextPath() + "/login.action");
return false; //终止执行。 不放行
}
}
return true; //继续执行。 放行
}
// controller中的方法执行后,但未返回视图前调用此方法
// 这里可在返回用户前对模型数据进行加工处理,比如这里加入公用信息以便页面显示
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
System.out.println("方法执行后 ,页面渲染前");
}
// controller中的方法执行后且视图返回后调用此方法
// 这里可得到执行controller时的异常信息
// 这里可记录操作日志
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
System.out.println("页面渲染后 ");
}
}
src/springmvc.xml(Springmvc的核心配置文件):
<?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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解扫描。 扫描 @Controler @Service等注解 -->
<context:component-scan base-package="com.xxx"/>
<!-- 注解驱动; 指定处理器映射器、处理器适配器 -->
<mvc:annotation-driven />
<!-- ====================================================== -->
<!-- Springmvc的拦截器 -->
<mvc:interceptors>
<!-- 可以配置多个拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/> <!-- 两个*表示可以递归多层 -->
<!-- 自定义的拦截器类 -->
<bean class="com.xxx.springmvc.interceptor.Interceptor1"/>
</mvc:interceptor>
<!-- <mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.xxx.springmvc.interceptor.Interceptor2"/>
</mvc:interceptor> -->
<!-- 多个拦截器的执行先后的顺序。
HandlerInterceptor1..preHandle..
HandlerInterceptor2..preHandle..
HandlerInterceptor2..postHandle..
HandlerInterceptor1..postHandle..
HandlerInterceptor2..afterCompletion..
HandlerInterceptor1..afterCompletion..
拦截器一 放行,拦截器二 不放行的执行结果和顺序:
HandlerInterceptor1..preHandle..
HandlerInterceptor2..preHandle..
HandlerInterceptor1..afterCompletion..
-->
</mvc:interceptors>
<!-- ====================================================== -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图(jsp页面)请求路径的前缀 -->
<property name="suffix" value=".jsp"/> <!-- 后缀 -->
</bean>
</beans>