springmvc拦截器
来源:互联网 发布:tags是什么软件小米 编辑:程序博客网 时间:2024/06/14 03:02
在使用框架的时候大多时候我们是不需要自己定义拦截器的,但是有时候我们需要进行权限的控制,那么就需要自己去写拦截器
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.控制台的输出结果为
处理前——————————————
**************
处理后——————————————
- [SpringMVC] SpringMVC拦截器
- SpringMVC-----9、SpringMVC拦截器
- springMVC—springMVC拦截器
- springMVC 拦截器
- SpringMVC—拦截器
- springmvc 拦截器配置
- SpringMvc拦截器
- SpringMvc 拦截器
- springMVC 拦截器
- springmvc登录拦截器
- springMVC之拦截器
- SpringMVC使用拦截器
- springMVC 自定义拦截器
- springmvc拦截器实现
- springMVC 拦截器
- springmvc的拦截器
- SpringMVC拦截器
- springMVC拦截器
- linux rpm 命令
- C++ virtual函数 实现机制
- java21个知识点重点
- index_ffs, leading,merge,no_merge,no_unnest,use_hash
- java-关于java中this关键字的只用
- springmvc拦截器
- RecyclerView实例-实现可下拉刷新上拉加载更多并可切换线性流和瀑布流模式(1)
- Java泛型的实现:原理与问题
- 在centos中键入不同终端的几种方法
- Android使用AttributeSet自定义控件的方法
- Factor Combinations
- 演示MP3的复制。通过IO流缓冲区。
- 【转载】逃离adapter的地狱-针对多个View type的组合实现方案
- Kth Smallest Element in a BST