一般我们在Controller层,会编写类似这样的方法


  @Controller
  @RequestMapping(value="/detail")

  public class GetURIDetailController {

    @SystemControllerLog(description = "id")
@RequestMapping(value="/{id}",method={RequestMethod.GET})
public ModelAndView getDetailID(
@PathVariable("id")
Integer id
   ,HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView();
    //someting request对象方法的调用
modelAndView.addObject("id",id);
modelAndView.setViewName("detail");
return modelAndView;
}
}

本质上Controller就是一个spring 组件,所以在我的请求方法 getDetailID中,可以添加一个HttpServletRequest获得当前请求的request对象,

request对象中包含了 用户ID session 以及等等信息

-------------------------------------------------------------

假设我们要编写一个AOP拦截Controller实现我们的切点事务时候,这个时候

RequestContextHolder这个静态类就派上用处了,
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  


private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
  new NamedThreadLocal<RequestAttributes>("Request attributes");//NamedThreadLocal 线程本地


public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = requestAttributesHolder.get();//获得线程本地request对象
if (attributes == null) {
attributes = inheritableRequestAttributesHolder.get();//
}
return attributes;
}
我阅读了一下Spring的源代码,它获得的是当前请求的request对象

这样设计LOG系统的时候,可以直接从Request对象当中读取用户的Cookies跟Session,完成LOG的记录。

这里有一个 小问题要补充的是,
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener> web.xml中要加入这个listen,这样在发起请求的时候 才会在线程本地中绑定 request对象

Spring Mvc 在非controller层 实现获取request对象的更多相关文章

  1. Spring MVC 注解之controller层

    第一层注解:@Controller 和 @RestController. 这两个注解的作用是:处理页面的HTTP请求,不同点 @RestController相当于@Controller +@Respo ...

  2. spring mvc aop拦截controller层获取RequestBody反序列化后参数

    最近,为了解耦,把一逻辑从interceptor抽出来,放在aop中处理,需要得到RequestBody.如下: @Aspect @Configuration public class CheckAs ...

  3. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  4. springmvc中获取request对象,加载biz(service)的方法

    获取request对象: 首先配置web.xml文件--> <listener> <listener-class> org.springframework.web.con ...

  5. 在SpringMVC中获取request对象

    1.注解法 @Autowired private  HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...

  6. 在SpringMVC中获取request对象的几种方式

    1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...

  7. java webservice服务器端获取request对象的三种方式

    有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...

  8. webservice服务器端获取request对象的三种方式

    有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...

  9. 如何在SpringMVC中获取request对象

    1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...

随机推荐

  1. [WCF REST] Web消息主体风格(Message Body Style)

    对于Web HTTP编程模型来说,服务契约中作为操作的方法无须应用OperationContractAttribute特性,只需要根据需要应用WebGetAttribute与WebInvokeAttr ...

  2. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q125-Q126)

    Question 125You are creating an application for SharePoint Server 2010.The application will run on a ...

  3. Jsoup解析Html中文文档

    jsoup 简介Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HTM ...

  4. Android项目实战(十四):TextView显示html样式的文字

    项目需求: TextView显示一段文字,格式为:(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我想的是用(转) SpannableStr ...

  5. android 隐藏标题栏

    在onCreate()方法中添加如下代码: public class FirstActivity extends Activity { @Override protected void onCreat ...

  6. Force.com平台基础--前言

    云计算平台 云计算模式有三种:(下面介绍来自百科) 1. SaaS:提供给客户的服务是运营商运行在云计算基础设施上的应用程序,用户可以在各种设备上通过客户端界面访问,如浏览器.消费者不需要管理或控制任 ...

  7. Hive安装(三)之奇怪的错误

    启动hive命令报错 “Metastore contains multiple versions” 解决方案: 因为hive metastore存储在mysql中,所以登录mysql,use hive ...

  8. sql 把特定数据排在最前面

    感谢www.baidu.com/p/dongfanghong_1 sql大神,简单的语法运用起来简直活了. 第一法] select * from table where name='D' UNION ...

  9. 将Session写入Memcache

    通过session_set_save_handler()方法自定义Session写入Memcache <?php class MemSession{ private static $handle ...

  10. 如何切入 Linux 内核源代码

    Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天就是逃课,上网,玩游戏,睡觉.毕业的时候,人家跟我说Makefile我完全不知,但是一说Make Love我就来劲了 ...