SpringMVC框架六:拦截器

拦截器的概念就不介绍了:

SpringMVC中拦截器的执行规则:

一个小示例展示:

我搞两个拦截器:

第一个:

package org.dreamtech.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 { @Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("页面渲染后1"); } @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("方法后1"); } @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("方法前1");
return true;
} }

第二个:

package org.dreamtech.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 Interceptor2 implements HandlerInterceptor { @Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("页面渲染后2"); } @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("方法后2"); } @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("方法前2");
return true;
} }

然后把它配置到SpringMVC.xml中:

    <!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 配置多个拦截器 -->
<mvc:interceptor>
<!-- 拦截所有 -->
<mvc:mapping path="/**" />
<!-- 自定义拦截器类 -->
<bean class="org.dreamtech.springmvc.interceptor.Interceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<!-- 拦截所有 -->
<mvc:mapping path="/**" />
<!-- 自定义拦截器类 -->
<bean class="org.dreamtech.springmvc.interceptor.Interceptor2s" />
</mvc:interceptor>
</mvc:interceptors>

执行之后:

SpringMVC框架六:拦截器

然而我们如果把拦截器一的方法前不放行:

    @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("方法前1");
return false;
}

这时候,发现控制台只是打印了:方法前1

但是,我们如果把拦截器二的方法前不放行:

打印的却是:

SpringMVC框架六:拦截器

到这里就可以看出SpringMVC的执行规则了:

为了方便,我借用某博主的图片:

SpringMVC框架六:拦截器

文字解释:

preHandler按拦截器定义顺序调用,

postHandler按拦截器定义逆序调用,

afterCompletion按拦截器定义逆序调用,

postHandler在所有的拦截器返回成功时候才会调用,

afterCompletion在preHandler返回true才会调用

应用:

登录功能:只有登录才能查看其他信息

限制:在非登陆状态下无法访问该网站任何一处

随意写一个登录页面:

<body>
<form action="${pageContext.request.contextPath }/login.action" method="post">
用户名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>

只用一个拦截器:

    <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.dreamtech.springmvc.interceptor.Interceptor2" />
</mvc:interceptor>
</mvc:interceptors>

拦截器:

package org.dreamtech.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 Interceptor2 implements HandlerInterceptor { @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception { } @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception { } @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
String requestURI = request.getRequestURI();
/*
* URI:/login.action
* URL:http://xxx/xxx/login.action
*/
if(!requestURI.contains("/login")){
String username = (String) request.getSession().getAttribute("USER_SESSION");
if(username==null){
response.sendRedirect(request.getContextPath()+"/login.action");
return false;
}
}
return true;
} }

实现:

    @RequestMapping(value = "/login.action", method = RequestMethod.GET)
public String login() {
return "login"; } @RequestMapping(value = "/login.action", method = RequestMethod.POST)
public String login(String username, HttpSession httpSession) {
httpSession.setAttribute("USER_SESSION", username);
return "redirect:/item/itemlist.action"; }

这里注意:我直接返回字符串:login是因为提前做了配置:

    <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
上一篇:jQuery的attr与prop,attribute和property区别


下一篇:Session初识