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. 【转】unity3d优化总结篇

    https://blog.csdn.net/weixin_33733810/article/details/94610167 某些技术或建议有些过时,但也值得参考 另外,关于如何设置不同layer的裁 ...

  2. [Java]使用正则表达式实现分词

    手工分词稍嫌麻烦,不好维护,而利用正则表达式就利索多了.Java提供了java.util.regex.Matcher,java.util.regex.Pattern类来帮助我们实现此功能. 例一:以下 ...

  3. [MVC HtmlHelper简单了解]

    HtmlHelper用来在视图中显示Html控件,简化代码,使用方便!,降低了View视图中的代码复杂度!可以更快速的完成工作! 以下是一些常用 的html标签 辅助方法 使用HTML辅助方法输出   ...

  4. React之父子组件之间传值

    1.新增知识点 /** React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. 父子组件:组件的相互调用中,我们把调 ...

  5. nodejs之mongodb操作

    声明: 当查询到数据库数据后,对数据库数据进行遍历,可以采用toArray()函数,具体实现可以看第六点 1.本地安装mongodb 安装包:https://www.mongodb.com/downl ...

  6. Flume采集日志

    角色 Source 数据来源 (exec, kafka, http…)Channel 数据通道 (memory,file,jdbc)Sink 数据目的地 (kafka,hdfs,es…) Agent ...

  7. 启动elasticsearch-head显示集群健康值:未连接

    ES启动后,进行es header访问的话,使用localhost:9100会显示集群健康值未连接 2种情况(均为windows10环境下): 1:未在elasticsearch-6.8.0\conf ...

  8. yum升级python

    一,查看系统默认版本 [root@redhat7 ~]# python -VPython 2.7.5 二,安装依赖包 yum install epel-release -y 三,查看新版本 yum s ...

  9. 【HANA系列】SAP 一位SAP培训顾问的建议:SAP HANA应该如何学习?

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP 一位SAP培训顾问的建议 ...

  10. [开发技巧]·pandas如何保存numpy元素

    [开发技巧]·pandas如何保存numpy元素 ​ 1.问题描述 在开发的过程中遇到一个问题,就是需要把numpy作为pandas的一个元素进行保存,注意不是作为一列元素.但是实践的过程中却不顺利, ...