SpringMVC框架六:拦截器
2018-09-01 05:42:36来源:博客园 阅读 ()
拦截器的概念就不介绍了:
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>
执行之后:
然而我们如果把拦截器一的方法前不放行:
@Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println("方法前1"); return false; }
这时候,发现控制台只是打印了:方法前1
但是,我们如果把拦截器二的方法前不放行:
打印的却是:
到这里就可以看出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>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java--反射(框架设计的灵魂)案例 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- Java--反射(框架设计的灵魂) 2020-06-11
- 与JAVA集合相遇 2020-06-11
- Java框架之Hibernate实战篇 2020-06-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash