SpringMVC—拦截器
来源:互联网 发布:淘宝用户名是会员名吗 编辑:程序博客网 时间:2024/06/05 07:49
拦截器
要点:
1.拦截器要实现HandlerInterceptor接口。
2.可设置多个拦截器。
拦截器1:
package com.cjh.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 UserInterceptor implements HandlerInterceptor {
/**
* 用于释放资源的方法
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception)
throws Exception {
}
/**
* 控制器执行完,生成视图之前可以做的动作,向模型中添加公共成员
*/
public void postHandle(HttpServletRequest request, HttpServletResponse repsponse,
Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("UserInterceptor----->postHandle");
}
/**
* 拦截之前执行的方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("UserInterceptor----->preHandle");
return true;
}
}
拦截器2:
package com.cjh.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 UserInterceptor2 implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("UserInterceptor2---->postHandle2");
}
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("UserInterceptor2---->preHandle2");
return true;
}
}
servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 主键扫描,配置要扫描的Java类 -->
<context:component-scan base-package="com.cjh.action"/>
<!-- 定义一个视图解析类,基于ResourceView的解析器 ,此解析器是在url解析器上,加入了对jstl的支持-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 设置拦截器链 ,当配置了多个拦截器bean时,则是顺序的往下执行-->
<mvc:interceptors>
<!-- 设置拦截器 -->
<mvc:interceptor>
<!-- 拦截的url -->
<mvc:mapping path="/*"/>
<!-- 拦截器类 -->
<bean class="com.cjh.Interceptor.UserInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.cjh.Interceptor.UserInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
要点:
1.拦截器要实现HandlerInterceptor接口。
2.可设置多个拦截器。
拦截器1:
package com.cjh.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 UserInterceptor implements HandlerInterceptor {
/**
* 用于释放资源的方法
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception)
throws Exception {
}
/**
* 控制器执行完,生成视图之前可以做的动作,向模型中添加公共成员
*/
public void postHandle(HttpServletRequest request, HttpServletResponse repsponse,
Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("UserInterceptor----->postHandle");
}
/**
* 拦截之前执行的方法
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("UserInterceptor----->preHandle");
return true;
}
}
拦截器2:
package com.cjh.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 UserInterceptor2 implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("UserInterceptor2---->postHandle2");
}
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("UserInterceptor2---->preHandle2");
return true;
}
}
servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 主键扫描,配置要扫描的Java类 -->
<context:component-scan base-package="com.cjh.action"/>
<!-- 定义一个视图解析类,基于ResourceView的解析器 ,此解析器是在url解析器上,加入了对jstl的支持-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 设置拦截器链 ,当配置了多个拦截器bean时,则是顺序的往下执行-->
<mvc:interceptors>
<!-- 设置拦截器 -->
<mvc:interceptor>
<!-- 拦截的url -->
<mvc:mapping path="/*"/>
<!-- 拦截器类 -->
<bean class="com.cjh.Interceptor.UserInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.cjh.Interceptor.UserInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
- springMVC—springMVC拦截器
- SpringMVC—拦截器
- springMVC—拦截器
- [SpringMVC] SpringMVC拦截器
- 【springmvc】——拦截器
- SpringMVC-----9、SpringMVC拦截器
- springmvc——拦截器Interceptor
- SpringMVC札集(09)——拦截器
- SpringMVC——自定义拦截器
- SpringMVC随笔——认识拦截器
- 处理器拦截器详解—SpringMVC
- SpringMVC(四)—拦截器
- springMVC 拦截器
- springmvc 拦截器配置
- SpringMvc拦截器
- SpringMvc 拦截器
- springMVC 拦截器
- springmvc登录拦截器
- Git 常用命令
- 编译OPENJDK手记
- SpringMVC—注解方式
- jQuery得到当前浏览器信息
- hdoj 3436 queue-jumpers
- SpringMVC—拦截器
- java金钱转换问题
- 苹果iOS 6正式版更新详细教程
- 寻找第K大的数的方法总结
- win7下初用Mysql5.5体会及中文显示乱码完美解决
- 表达式求值
- 腾讯、人人、新浪社交网络优劣势分析
- SpringMVC—国际化
- SpringMVC—表单验证