在使用框架的时候大多时候我们是不需要自己定义拦截器的,但是有时候我们需要进行权限的控制,那么就需要自己去写拦截器
1.拦截器类
package com.qbd.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Intercepter implements HandlerInterceptor {
/*
* 负责清理工作
* */
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
/*、
* 处理后
* */
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("处理后——————————————");
// TODO Auto-generated method stub
}
/*
* 处理前
* */
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
// TODO Auto-generated method stub
System.out.println("处理前——————————————");
return true;/*返回true继续向下执行,返回false则不向下执行*/
}
}
2.mvc.xml配置
<?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.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">
<!-- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="104857600000"/>
<property name="maxInMemorySize" value="40960"/>
</bean> -->
<context:component-scan base-package="com.qbd.controller"></context:component-scan>
<!-- 拦截器的配置 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- /**代表此跟目录以及子目录下的所有 -->
<mvc:mapping path="/**"/>
<bean class="com.qbd.controller.Intercepter"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
3.测试程序
package com.qbd.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
@Controller
public class HController {
@RequestMapping("/hello")
public String name() {
System.out.println("****");
return "index.jsp";
}
}
4.控制台的输出结果为
处理前——————————————
**************
处理后——————————————