0027SpringMVC拦截器的编写和配置

SpringMvc中想使用拦截器,主要分为两步:

a、编写拦截器,需实现HandlerInterceptor接口

b、springmvc.xml中配置拦截器

逻辑图如下:

0027SpringMVC拦截器的编写和配置_第1张图片

 

 

 测试过程主要分为如下几步:

1、编写interceptorIndex.jsp

2、编写InterceptorController.java

3、编写interceptorSuccess.jsp

4、编写拦截器MyInterceptor1.java

5、springmvc.xml中配置拦截器

具体实现如下:

1、编写interceptorIndex.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/8
Time: 17:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


SpringMVC Interceptor


SpringMVC Interceptor测试


interceptor

2、编写InterceptorController.java

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/interceptor")
public class InterceptorController {
public static final String SUCCESS = "interceptorSuccess";
@RequestMapping("interceptorTest")
public String interceptorTest(){
System.out.println("执行controller中的方法");
return SUCCESS;
}
}

3、编写interceptorSuccess.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/8
Time: 17:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


Title


执行成功


<%System.out.println("执行interceptorSuccess.jsp"); %>

4、编写拦截器MyInterceptor1.java

package com.example.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor1 implements HandlerInterceptor {
@Override
/*1、返回true代表放行,可以处理后续的拦截器或者controller中的方法
* 2、返回false代表后续的内容不会被执行,可用参数中的request或者response跳转到错误页面或者登录页面等(权限验证)*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("执行MyInterceptor1的前置方法");
return true;
}

@Override
/*结果页面执行完之后执行*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("执行MyInterceptor1的后置方法");
}

@Override
  
/*也可使用参数的request或者response跳转到某些页面,则controller中的return不会被执行,因此可以控制不会跳两个页面*/

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("执行MyInterceptor1的afterCompletion方法");
}
}

5、springmvc.xml中配置拦截器


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">


















































MyInterceptor2.java与MyInterceptor1.java代码基本相同,不重复罗列。

测试:启动项目,在interceptorIndex.jsp点击链接访问controller中的方法,执行结果如下:

0027SpringMVC拦截器的编写和配置_第2张图片

 

 结果总结:

1、执行拦截器的前置方法

2、执行controller中的方法

3、执行拦截器的后置方法

4、执行controller中的return,进入相应的页面,执行页面中的代码

5、页面执行完之后,执行拦截的afterCompletion方法。

 

若有理解不到之处,望指正!

 

 

 

 

 

 

你可能感兴趣的:(0027SpringMVC拦截器的编写和配置)