(学习笔记仅帮助自己理解记忆,部分内容可能存在理解错误,如有发现,还望指出)
MVC拦截器Interceptor
类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等
通常拦截器类可以通过两种方式来定义:
(这里使用第一种来实现)
- 通过实现HandlerInterceptor接口,或继承HandlerInterceptor接口的实现类(如HandlerInterceptorAdapter)来定义
- 通过实现WebRequestInterceptor接口,或继承WebRequestInterceptor接口的实现类来定义
示例:
web.xml代码:
<!--隐藏http请求过滤器-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置dispatcherServlet前置控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--对.js资源文件不进行拦截-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
SpringMVC.xml代码:
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/*"/>
<bean id="loginInterceptor" class="interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!--注解扫描-->
<context:component-scan base-package="controller,interceptor,pojo" />
<!--开启MVC的注解驱动-->
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
LoginInterceptor类定义(实现HandlerInterceptor接口,preHandle,postHandle,afterCompletion三个方法)
preHandle:该方法会在控制器方法前执行,其返回值表示是否中断后续操作
- true------>继续执行方法
- false----->不进入处理方法
postHandle:该方法会在控制器方法调用之后,且解析视图之前执行。可以通过此方法对请求域中的模型和视图做出进一步的修改
afterCompletion:该方法会在整个请求完成,即视图渲染结束之后执行。可以通过此方法实现一些资源清理、记录日志信息等工作
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
HttpSession session = httpServletRequest.getSession();
User user = (User) session.getAttribute("user");
if(user != null){
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
DataBindController类代码编写:
//登录界面
@RequestMapping(value="/login")
public String login(){
System.out.println("111111111111111");
return "login";
}
//登录验证
@RequestMapping(value="/loginCheck")
public String loginCheck(User u, HttpSession session){
System.out.println(11111111);
if(u.getName().equals("zs") && u.getPwd().equals("123")){
User user = new User();
user.setName(u.getName());
user.setPwd(u.getPwd());
session.setAttribute("user",user);
System.out.println(2222222);
return "redirect:loginSuccess";
}
return "redirect:login";
}
//登录成功
@RequestMapping(value="/loginSuccess")
public String loginSuccess(){
return "loginSucces";
}
//url路径中user/*的拦截器验证
@RequestMapping(value="/user/loginSuccess")
public String intercept(HttpSession session){
return "loginSucces";
}
login.jsp代码编写:
<form method="post" action="/loginCheck">
<div class="wrap">
<table>
<tr>
<td colspan="2"><h1>登录图书管理系统</h1></td>
</tr>
<tr>
<td>用户名:</td><td><input name="name" type="text" value="admin"/></td>
</tr>
<tr>
<td>密码:</td><td><input name="pwd" type="text" value="123"/></td></tr>
<tr>
<td colspan="2"><input id="loginBtn" type="submit" value="登录"/></td>
</tr>
</table>
</div>
</form>
loginSucces.jsp代码编写:
<h1>欢迎回来: ${user.name} !!!</h1>
测试:
- http://localhost:8080/user/login
- http://localhost:8080/login
- http://localhost:8080/user/loginSuccess