SpringMvc中获取Request

简介:

目录

 ●   Controller中加参数
 ●   自动注入
 ●   基类中自动注入
 ●   手动调用

 ●  @ModelAttribute方法

Controller中加参数

 
  1. @Controller

  2. public class TestController {

  3. @RequestMapping("/test")

  4. public void test(HttpServletRequest request) {

  5. ......

  6. }

  7. }

Controller中获取request对象后,如果要在其他方法中(如service方法、工具类方法等)使用request对象,需要在调用这些方法时将request对象作为参数传入

此时request对象是方法参数,相当于局部变量,毫无疑问是线程安全的。

自动注入

 
  1. @Controller

  2. public class TestController{

  3. @Autowired

  4. private HttpServletRequest request; //自动注入request

  5. @RequestMapping("/test")

  6. public void test() throws InterruptedException{

  7. ......

  8. }

  9. }

使用这种方式,当Bean(本例的TestController)初始化时,Spring并没有注入一个request对象,而是注入了一个代理(proxy);当Bean中需要使用request对象时,通过该代理获取request对象。request实际上是一个代理:代理的实现参见AutowireUtils的内部类ObjectFactoryDelegatingInvocationHandler。

调用request的方法method时,实际上是调用了由objectFactory.getObject()生成的对象的method方法;objectFactory.getObject()生成的对象才是真正的request对象。

objectFactory的类型为WebApplicationContextUtils的内部类RequestObjectFactory;而RequestObjectFactory要获得request对象需要先调用currentRequestAttributes()方法获得RequestAttributes对象,生成RequestAttributes对象的核心代码在类RequestContextHolder中,生成的RequestAttributes对象是线程局部变量(ThreadLocal),因此request对象也是线程局部变量;这就保证了request对象的线程安全性。

基类中自动注入

 
  1. public class BaseController {

  2. @Autowired

  3. protected HttpServletRequest request;

  4. }

  5. @Controller

  6. public class TestController extends BaseController {

  7. }

与方法2相比,避免了在不同的Controller中重复注入request;但是考虑到java只允许继承一个基类,所以如果Controller需要继承其他类时,该方法便不再好用。

手动调用

 
  1. @Controller

  2. public class TestController {

  3. @RequestMapping("/test")

  4. public void test() throws InterruptedException {

  5. HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

  6. .......

  7. }

  8. }

通过自动注入实现与通过手动方法调用实现原理差不多。因此本方法也是线程安全的。

优点:可以在非Bean中直接获取。缺点:如果使用的地方较多,代码非常繁琐;因此可以与其他方法配合使用。

@ModelAttribute方法

 
  1. @Controller

  2. public class TestController {

  3. private HttpServletRequest request; 此处线程不安全

  4. @ModelAttribute

  5. public void bindRequest(HttpServletRequest request) {

  6. this.request = request; 此处request线程安全

  7. }

  8. @RequestMapping("/test")

  9. public void test() throws InterruptedException {

  10. ......

  11. }

  12. }

@ModelAttribute注解用在Controller中修饰方法时,其作用是Controller中的每个@RequestMapping方法执行前,该方法都会执行。bindRequest()的作用是在test()执行前为request对象赋值。虽然bindRequest()中的参数request本身是线程安全的,但由于TestController是单例的,request作为TestController的一个域,无法保证线程安全。


原文发布时间为:2018-11-16
本文来自云栖社区合作伙伴“ Java杂记”,了解相关信息可以关注“ Java杂记”。
相关文章
|
2月前
|
JSON 前端开发 数据格式
SpringMVC Post 404
SpringMVC Post 404
12 0
|
3月前
|
存储 前端开发 Java
JavaWeb:Request & Response
在JavaWeb开发中,Request(请求)和Response(响应)是非常重要的概念。它们分别代表着客户端向服务器发送请求和服务器向客户端返回响应的过程。Request对象是由服务器创建的,用于封装来自客户端的请求信息。它包含了请求的HTTP方法(如GET或POST),URL,请求头部、参数等信息。你可以通过Request对象获取客户端发送的表单数据、URL参数、HTTP头部和Cookies等。Response对象则是服务器用来向客户端发送响应的工具。它包含了HTTP状态码、响应头部和响应体等信息。你可以使用Response对象设置响应的状态码、设置响应头部
37 3
 JavaWeb:Request & Response
|
4月前
|
Java
[SpringBoot] 8. aop 获取 request response
[SpringBoot] 8. aop 获取 request response
|
4月前
|
XML 应用服务中间件 数据格式
Servlet的urlPattern配置及XML配置Servlet
Servlet的urlPattern配置及XML配置Servlet
47 0
|
5月前
|
Java Spring
springMVC中获取request 对象
springMVC中获取request 对象
|
XML 编解码 前端开发
JavaWeb《二》Servlet、Request请求
本文是javaweb的第二篇,介绍了Servlet,Servlet是JavaWeb最为核心的内容,它是Java提供的一门动态web资源开发技术。 概述了Request请求与响应,并且主要分析了Request请求。
102 0
JavaWeb《二》Servlet、Request请求
|
SQL JSON 前端开发
SpringMVC:400 Bad Request
SpringMVC:400 Bad Request
107 0
|
XML 数据格式
可以使用注解将Servlet和URL对应起来
可以使用注解将Servlet和URL对应起来
71 0
|
XML IDE Java
JavaWeb - JSP、Servlet、Request、Response、Get、Post 中文乱码问题
JavaWeb - JSP、Servlet、Request、Response、Get、Post 中文乱码问题
120 0
JavaWeb - JSP、Servlet、Request、Response、Get、Post 中文乱码问题