RequestContextListener实现了 ServletRequestListener ,在其覆盖的requestInitialized(ServletRequestEvent requestEvent)方法中,将request最终设置到了RequestContextHolder中.

public class RequestContextListener implements ServletRequestListener {

    private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =
RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES"; @Override
public void requestInitialized(ServletRequestEvent requestEvent) {
if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
throw new IllegalArgumentException(
"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
}
HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();//从事件对象中获取request对象
ServletRequestAttributes attributes = new ServletRequestAttributes(request);//将request设置到servletRequestAttributes中
request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);//反过来将servletRequestAttributes设置到request中
LocaleContextHolder.setLocale(request.getLocale());
RequestContextHolder.setRequestAttributes(attributes);//再将servletRequestAttributes设置到requestContextHolder中
}

代码中获取该request的步骤:

HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes()))
.getRequest();// 返回了RequestAttributes接口,将其强转为ServletRequestAttributes实现类

使用上述方法前,需要先配置RequestContextListener监听器:

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

在springmvc中使用requestContextListener获取全部的request对象的更多相关文章

  1. jsp页面中使用javascript获取后台放在request或session中的值

    在JSP页面中.常常使用javascript,可是要出javascript获取存储在request,session, application中的值.例如以下是获取request中的值: 如果后台中有: ...

  2. 项目随笔之springmvc中freemark如何获取项目路径

    转载:http://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的 ...

  3. 161018、springMVC中普通类获取注解service方法

    1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.sp ...

  4. springMVC 中几种获取request和response的方式

    1.最简单方式:参数 例如: @RequestMapping("/test") @ResponseBody public void saveTest(HttpServletRequ ...

  5. Spring 中任意位置获取 session 和 request

    在web.xml中添加监听: <listener> <listener-class>org.springframework.web.context.ContextLoaderL ...

  6. vue中怎么实现获取当前点击对象this

    应用场景 在评论列表中,有很多条评论(通过循环出来的评论列表),评论的文字有多跟少,默认展示2行超出显示点击查看更多,,要点击查看更多对当前的这条评论进行全部评论展示! 问题描述 要是在传统的点击事件 ...

  7. SpringMVC中的400错误,The request sent by the client was syntactically incorrect.

    在其他对象属性类型一样情况下,只需要创建一个类,再在springmvc.xml中添加配置: package com.ujiuye.common; import org.springframework. ...

  8. SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回

    @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区. ...

  9. .Net MVC 获取Response和Request对象

    通过  System.Web.HttpContext.Current  获取 public static string ConstractExportExcel(List<ERP_Contrac ...

随机推荐

  1. HTML和CSS 入门系列(二):文字、表单、表格、浮动、定位、框架布局、SEO

    上一篇:HTML和CSS 入门系列(一):超链接.选择器.颜色.盒模式.DIV布局.图片 一.文字 1.1 属性 1.2 字体样式:font-family 1.3 字体大小:font-size 1.4 ...

  2. ajax报告申请添加

    function reportApplyAddFun(){ $("#dlg").dialog("open").dialog("center" ...

  3. C++入门经典-例3.24-找图书的位置

    1:运行代码: // 3.24.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...

  4. 20191017-2 alpha week 2/2 Scrum立会报告+燃尽图 01

    本次作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/9798 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名: ...

  5. LeetCode 19. 删除链表的倒数第N个节点(Remove Nth Node From End Of List)

    题目描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后, ...

  6. Edge Detection

      Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22604   Accepted: 5311 ...

  7. mysql数据库简单补充

    1.只有拥有特定权限的用户才能执行特定的操作.就好像我们在现实生活中,一般没有权利进入军事禁区,除非我们被某个很有权利并且可以指定其他人进入军事基地的人赋予了进入军事禁区的权利. 命令: GRANT ...

  8. Random Initialization for K-Means

    K-Means的中心初始化惯用方式是随机初始化.也就是说:从training set中随机挑选出K个 作为中心,再进行下一步的K-Means算法. 这个方法很容易导致收敛到局部最优解,当簇个个数(K) ...

  9. GO开发:链表

    链表 type Student struct { Name string Next* Student } 每个节点包含下一个节点的地址,这样把所有的节点串起来了,通常把链表中的第一个节点叫做链表头 p ...

  10. Python文件重命名代码

    import os def re_name(path): for file in os.listdir(path): file_path = os.path.join(path, file) # 判断 ...