1. 拦截器方法
方法名 | 说明 |
---|
preHandle() | 该方法在请求处理之前执行, 返回值是boolean类型, 当返回false时, 表示请求结束, 后续的方法不在执行. 当返回值为true时就会调用下一个Interceptor的preHandle方法 |
postHandle() | 该方法在请求处理之后被调用, 前提是preHandle方法的返回值为true时才执行. 且它会在DispatcherServlet进行视图返回渲染之前被调用, 所以我们可以在这个方法中对Controller处理之后的ModelAndView对象进行操作 |
afterCompletion() | 该方法在整个请求结束之后执行, 也就是在DispatcherServlet渲染了对应的视图之后执行, 前提是preHandle方法返回true |
2. 编写代码测试
2.1 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
2.2 编写SpringMVC核心配置文件
<?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"
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">
<context:component-scan base-package="site.zhouyun.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp" ></property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="site.zhouyun.interceptor.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
2.3 编写拦截器类
public class UserInterceptor implements HandlerInterceptor
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
System.out.println("preHandle....");
String username = request.getParameter("username");
if("Cloud".equals(username))
{
return true;
}
request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
{
System.out.println("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
{
System.out.println("afterCompletion...");
}
}
2.4 编写Controller
@Controller
@RequestMapping("user")
public class UserController
{
@RequestMapping("login")
public ModelAndView login(ModelAndView modelAndView)
{
System.out.println("login...");
modelAndView.addObject("name","Cloud");
modelAndView.setViewName("success");
return modelAndView;
}
}
2.5 编写jsp界面
- 在webapp文件夹下创建jsp文件夹, 然后在jsp文件夹里创建error.jsp和success.jsp
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户名或密码错误</h1>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Welcome ${name}</h1>
</body>
</html>
3 测试
- 启动项目, 打开浏览器在地址栏输入
http://localhost:8080/user/login
查看效果 - 直接访问
http://localhost:8080/user/login
会跳转到error.jsp界面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bcadb8570e63bec92c792a8bc1d0dc3f.png#pic_center)
- 想要成功访问必须加上username参数, 并且值要跟代码里写的一样, 我这里写的是"Cloud", 所以想要成功访问的url为:
http://localhost:8080/user/login?username=Cloud
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9e958515d3bea4f05f9377a05326b703.png#pic_center)
- 注意: 我这里没有对jsp做限制, 所以可以直接访问到jsp界面, 如果想要实现不能直接访问JSP界面可以将jsp文件夹移动到
WEB-INF
文件夹下, 然后修改代码中对应的路径就行.