Spring的HandlerMapping处理器支持拦截器应用。当需要为某些请求提供特殊功能时,例如实现对用户进行身份认证、登录检查等功能。
拦截器必须实现HandlerInterceptor接口
流程图片实例:
使用步骤:
1. 编写一个类 实现HandlerInterceptor接口
2. 配置applicationContext.xml
配置文件案例:
<mvc:interceptors> <mvc:interceptor> <!-- 要拦截的地址 --> <mvc:mapping path="/**"/> <!-- 匹配拦截地址 中的不拦截的地址 <mvc:exclude-mapping path=""/>--> <!-- 使用的拦截器类 --> <bean class="cn.xdl.filter.MyFilter"></bean> </mvc:interceptor> </mvc:interceptors>
案例:
控制器代码:
package cn.xdl.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.View; @Controller public class UserController { @RequestMapping("/login.do") public ModelAndView userLogin(String uname,String upass){ System.out.println(uname); System.out.println(upass); ModelAndView modelview=new ModelAndView("loginResult"); return modelview; } }
UserController.java
过滤器代码:
package cn.xdl.user.filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class UserFilter 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 { /* * return true 表示拦截通过 * return false 表示拦截不通过 * * 如果是拦截不通过用户的页面会出现一个大白屏,这个时候应该进行请求重定向,比如:response.sendRedirect("loginError.jsp") */ String uname = request.getParameter("uname"); String upass = request.getParameter("upass"); //对用户的账号和密码进行简单验证 if(uname==null||upass==null||uname.length()<6||upass.length()<6){ //拦截 , response.sendRedirect("loginError.jsp"); return false; } return true; } }
UserFilter.java
DispatcherServlet配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!--指定扫描包--> <context:component-scan base-package="cn"></context:component-scan> <!--开启扫描--> <mvc:annotation-driven/> <!--配置ModelAndView--> <bean id="viewResolve" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"></property> <property name="prefix" value=""></property> <property name="suffix" value=".jsp"></property> </bean> <!--配置拦截器--> <mvc:interceptors> <mvc:interceptor> <!-- 要拦截的地址,这里是拦截所有地址 --> <mvc:mapping path="/**"/> <!-- 匹配拦截地址中不用拦截的地址 <mvc:exclude-mapping path=""/> --> <!-- 指定的拦截器类 --> <bean class="cn.xdl.user.filter.UserFilter"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
dispatcher_serverlet-servlet.xml
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SpringFilterTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher_serverlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher_serverlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>EncodingName</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingName</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
web.xml
jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录失败</title> </head> <body> 登录失败 </body> </html>
loginError.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>结果页面</title> </head> <body> 登录成功 </body> </html>
loginResult.java
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> </head> <body> <form action="login.do" method="post"> 用户名:<input type="text" name="uname" /><br/><br/> 密码:<input type="password" name="upass" /><br/><br/> <input type="submit" value="登录" /> </form> </body> </html>
login.jsp
时间: 2024-11-05 23:50:10