春季MVC拦截器HandleInterceptorAdapter示例
有两种方法可以将拦截器与Spring MVC结合使用。
春季MVC教程:
- 春季MVC你好世界示例
- 春季MVC休眠MySQL示例
- 春季MVC拦截器示例
- 春季MVC angularjs示例
- 春季MVC @RequestMapping示例
- 弹簧组件,服务,存储库和控制器示例
- 春季MVC @ModelAttribute注释示例
- 春季MVC @RestController注释示例
- 弹簧 MultiActionController示例
- 春季MVC模型图春季MVC文件上传示例
- 春季休息ful 网页服务示例
- 春季休息ful 网页 Service JSON示例
- 春季休息ful 网页服务CRUD示例
- 春季安全 您好 World示例
- 春季安全自定义登录表单示例
HandlerInterceptor接口:
我们可以实现HandlerInterceptor接口来使用拦截器。它有三种我们需要实现的方法。
HandlerInterceptorAdapter类:
HandlerInterceptor的问题是您需要实现所有方法,但是如果扩展到称为HandlerInterceptorAdapter的抽象类,则只能实现所需的那些方法,而其他方法将具有默认实现。
让我们使用HandlerInterceptorAdapter类实现自定义拦截器示例。
春季MVC拦截器示例:
添加Spring MVC依赖项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<项目 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:si="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.Arpit.爪哇2blog</groupId> <artifactId>弹簧MVCHelloWorldExample</artifactId> <打包>战争</打包> <版>0.0.1-快照</版> <名称>弹簧MVCHelloWorldExample 马文 网络应用</名称> <网址>http://maven.apache.org</url> <依存关系> <依赖> <groupId>unit</groupId> <artifactId>unit</artifactId> <版>3.8.1</版> <范围>测试</范围> </依赖> <依赖> <groupId>爪哇x.小服务程序</groupId> <artifactId>爪哇x.小服务程序-api</artifactId> <版>3.1.0</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-核心</artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-网路mvc</artifactId> <版>${弹簧.版}</版> </依赖> </依存关系> <建立> <finalName>弹簧MVCHelloWorldExample</finalName> <外挂程式> <插入> <groupId>组织.阿帕奇.专家.外挂程式</groupId> <artifactId>专家-编译器-插入</artifactId> <版>3.1</版> <组态> <资源>${杰克.版}</资源> <目标>${杰克.版}</目标> </组态> </插入> </外挂程式> </建立> <属性> <弹簧.版>4.2.1。发布</弹簧.版> <杰克.版>1.7</杰克.版> </属性> </项目> |
创建控制器
创建一个名为“org.arpit.java2blog.springmvc.controller”
创建一个名为的控制器类 “HelloWorldController.java”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
包 组织.Arpit.爪哇2blog.弹簧.控制者; 进口 组织.弹簧框架.刻板印象.控制者; 进口 组织.弹簧框架.网路.捆绑.注解.请求映射; 进口 组织.弹簧框架.网路.小服务程序.模型和视图; @控制者 上市 类 你好世界Controller { @请求映射(“/你好世界”) 上市 模型和视图 您好() { 返回 新 模型和视图(“您好”); } } |
创建拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
包 组织.Arpit.爪哇2blog.弹簧.拦截器; 进口 爪哇x.小服务程序.http.HttpServletRequest; 进口 爪哇x.小服务程序.http.HttpServletResponse; 进口 组织.弹簧框架.网路.小服务程序.模型和视图; 进口 组织.弹簧框架.网路.小服务程序.处理程序.HandlerInterceptorAdapter; 上市 类 样品拦截器 延伸 HandlerInterceptorAdapter { @覆写 上市 布尔值 preHandle(HttpServletRequest 请求, HttpServletResponse 响应, 目的 处理程序) 抛出 例外 { 请求.setAttribute(“ blogName”, “ 爪哇2blog”); 返回 真正; } @覆写 上市 虚空 postHandle(HttpServletRequest 请求, HttpServletResponse 响应, 目的 处理程序, 模型和视图 模型和视图) 抛出 例外 { 串 blogName = (串) 请求.getAttribute(“ blogName”); //我们在这里添加了一些modelAndView对象,并将在view jsp中使用它。 模型和视图.addObject(“ blogName”,blogName); 模型和视图.addObject(“ authorName”, “阿尔伯特”); } @覆写 上市 虚空 完成后(HttpServletRequest 请求, HttpServletResponse 响应, 目的 处理程序, 例外 前) 抛出 例外 { 串 blogName = (串) 请求.getAttribute(“ blogName”); 串 authorName = (串) 请求.getAttribute(“ authorName”); 系统.出.打印(“请求网址::” + 请求.getRequestURL().至String()); 系统.出.打印(“博客名称:” + blogName); 系统.出.打印(“作者姓名:” + authorName); } } |
在这里,我在preHandle方法中设置了request属性(blogName),而在postHandle方法中得到了它。我们还在modelAndView中添加了两个对象,这些对象将在jsp中使用。
建立检视
修改 index.jsp as以下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ 页 语言=“ 爪哇” 内容类型=“文本/ html; charset = UTF-8” 页Encoding=“ UTF-8”%> <!DOCTYPE html 上市 “-// W3C // DTD HTML 4.01 Transitional // EN” "http://www.w3.org/TR/html4/loose.dtd"> <html> <头> <元 http-当量=“内容类型” 内容=“文本/ html; charset = UTF-8”> <标题>你好世界</标题> </头> <身体> <a href=“ 您好world.html”>请点击 这里 至 读 您好 信息 </a> </身体> </html> |
创造 您好.jsp in /WEB-INF / 夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ 页 语言=“ 爪哇” 内容类型=“文本/ html; charset = UTF-8” 页Encoding=“ UTF-8”%> <!DOCTYPE html 上市 “-// W3C // DTD HTML 4.01 Transitional // EN” "http://www.w3.org/TR/html4/loose.dtd"> <html> <头> <元 http-当量=“内容类型” 内容=“文本/ html; charset = UTF-8”> <标题>您好</标题> </头> <身体> 这个 是 书面 通过 ${authorName} 上 ${blogName} </身体> </html> |
配置Spring MVC应用程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<网路-应用程式 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:si="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 版="3.0"> <显示-名称>原型 已建立 网页 应用</显示-名称> <欢迎-文件-清单> <欢迎-文件>指数.jsp</欢迎-文件> </欢迎-文件-清单> <小服务程序> <小服务程序-名称>弹簧-调度员</小服务程序-名称> <小服务程序-类> 组织.弹簧框架.网路.小服务程序.分派器 </小服务程序-类> <加载-上-启动>1</加载-上-启动> </小服务程序> <小服务程序-映射> <小服务程序-名称>弹簧-调度员</小服务程序-名称> <网址-模式>/</网址-模式> </小服务程序-映射> </网路-应用程式> |
创建名为“springmvc-dispatcher-servlet.xml” in / WEB-INF folder as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<豆子:豆子 xmlns="http://www.springframework.org/schema/mvc" xmlns:si="http://www.w3.org/2001/XMLSchema-instance" xmlns:豆子="http://www.springframework.org/schema/beans" xmlns:语境="http://www.springframework.org/schema/context" si:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> <语境:零件-扫描 基础-包=“ 组织.arpit.java2blog.springmvc.controller” /> <豆子:豆 类=“ 组织.springframework.web.servlet.view.InternalResourceViewResolver”> <豆子:属性 名称=“字首” 值=“ / WEB-INF /”/> <豆子:属性 名称=“后缀” 值=“ .jsp” /> </豆子:豆> <拦截器> <拦截器> <映射 路径=“ /helloworld.html” /> <豆子:豆 类=“ 组织.arpit.java2blog.springmvc.interceptor.SampleInterceptor”></豆子:豆> </拦截器> </拦截器> </豆子:豆子> |
创建名为“springmvc-dispatcher-servlet.xml” in / WEB-INF folder as below.
1 2 3 4 5 6 7 |
xmlns:si="http://www.w3.org/2001/XMLSchema-instance" xmlns:豆子="http://www.springframework.org/schema/beans" xmlns:语境="http://www.springframework.org/schema/context" si:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> |
It ‘是时候进行Maven构建了。
运行应用程序
当您点击上方链接时,您将获得以下屏幕
我们用Spring MVC拦截器示例完成了工作。如果您仍然遇到任何问题,请发表评论。
点击开始20KB .zip