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. 分布式-信息方式-ActiveMQ支持的传输协议和配置

                             ActiveMQ支持的传输协议和配置■ Connector: ActiveMQ提供的,用来实现连接通讯的功能.包括: client-to-broker ...

  2. LeetCode117----填充同一层兄弟节点

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  3. 【python学习】字符串相关

    # -*- coding: utf-8 -*- # ========================================================================== ...

  4. gherkin

    语法 The primary keywords are: Feature Rule (as of Gherkin 6) Scenario (or Example) Given, When, Then, ...

  5. 设置terminal 命令别名

    vi -/.bash_profile code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" ...

  6. java:LeakFilling(Servlet,JSP)

    1.web-inf中的  .jsp 文件不会运行,其他都根据web.xml文档中的自然顺序进行运行 2.keep-alive 长连接(持续)  UDP,TCP短连接 3.状态码:1(消息异常) 2(成 ...

  7. 一张图包含SEO一切要点

    看到一张好图 from http://www.rongyipiao.com/?p=8

  8. HTML5 plus 报错 Uncaught SyntaxError: Unexpected identifier at XXXX.html:1

    最近使用 VUE2.X + muse-ui  + HTML5 plus 开发webApp 调用HTML5 plus报错,错误提示如下 Uncaught SyntaxError: Unexpected ...

  9. Angular5 tslint错误:The selector of the component “XXXComponent” should be used as element

    错误描述 在项目中自己封装了一个 select 组件 @Component({ selector: '[app-choosen-select]', templateUrl: './selectcomm ...

  10. 史上最详细 Linux 用户与用户组知识

    1.用户和用户组文件 在 linux 中,用户帐号,用户密码,用户组信息和用户组密码均是存放在不同的配置文件中的. 在 linux 系统中,所创建的用户帐号和其相关信息 (密码除外) 均是存放在 / ...