在实际开发中,总存在着这样的场景,比如拦截请求的ip地址,或者在所有的请求都返回相同的数据,如果每一个方法都写出相同数据固然可以实现,但是随着项目的变大,重复的代码会越来越多,所以在这种情况我们可以用拦截器来实现。
最近一直在研究thymeleaf,越发的感觉这个很好用,所以这篇文章也选择结合这个来使用。
新建项目,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot_interceptor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_interceptor</name>
<description>springboot_interceptor</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
新建一个拦截器CommonInterceptor,继承HandlerInterceptorAdapter。给大家说一下,在继承HandlerInterceptorAdapter有三个拦截器是经常使用的:
1.preHandle在业务处理器处理请求之前被调用
2.postHandle在业务处理器处理请求执行完成后,生成视图之前执行
3.afterCompletion在DispatcherServlet完全处理完请求后被调用
本文使用的是postHandle,代码如下:
package com.dalaoyang.interceptor;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.interceptor
* @email aaa@qq.com
* @date 2018/4/27
*/
@Component
public class CommonInterceptor extends HandlerInterceptorAdapter {
Logger logger = Logger.getLogger(CommonInterceptor.class);
//preHandle在业务处理器处理请求之前被调用,
//postHandle在业务处理器处理请求执行完成后,生成视图之前执行
//afterCompletion在DispatcherServlet完全处理完请求后被调用
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
logger.info("请求ip:"+request.getRemoteAddr());
logger.info("请求的方法:"+request.getMethod());
ModelMap modelMap = modelAndView.getModelMap();
modelMap.addAttribute("title","dalaoyang");
}
}
在启动类继承WebMvcConfigurerAdapter来为项目添加拦截器,代码如下:
package com.dalaoyang;
import com.dalaoyang.interceptor.CommonInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class SpringbootInterceptorApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(SpringbootInterceptorApplication.class, args);
}
@Autowired
CommonInterceptor commonInterceptor;
// 增加拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(commonInterceptor);
}
}
IndexController负责跳转,代码如下:
package com.dalaoyang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaaoyang.controller
* @email aaa@qq.com
* @date 2018/4/27
*/
@Controller
public class IndexController {
@RequestMapping("/")
public String index(Model model){
model.addAttribute("content","hi , dalaoyang !");
return "index";
}
}
在templates下新建index.html,其中content是controller返回的内容,title是在拦截器中返回的内容,代码如下:
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title th:text="${title}"></title>
</head>
<body>
<p th:text="${content}"></p>
</body>
</html>
启动项目,访问http://localhost:8888/,控制台如下:
在看一下页面:
源码下载 :大老杨码云
个人网站:https://dalaoyang.cn