一般我们在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. MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )

    The maximum number of controls allowed on MSCRM dashboards are 6. You cannot put the more than 6 gra ...

  2. ubuntu14.0安装arm-linux-gcc交叉编译环境

    1.下载文件: 安装包:arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 下载地址  http://pan.baidu.com/s/1pJwQ6Sj 2.开始安装(建议大 ...

  3. 在Android开发中使用Ant 二:进行一次完整的打包

    一次完整的Android打包要进行以下的几步:编译.代码混淆.打包apk.签名apk.apk优化. 为了能包涵使用NDK的情况,在这里使用一个有native代码的工程TestJni. 在工程根目录下新 ...

  4. 【转】c++中引用的全方位解读

    对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...

  5. C语言。自定义函数简单版

    #include <stdio.h> //函数声明 void sayHi(); //函数实现 void sayHI() { printf("大家好!!\n"); } i ...

  6. Cocos2d入门--3--向量的应用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. Effective Java 23 Don't use raw types in new code

    Generic types advantage Parameterized type can provide erroneous check in compile time. // Parameter ...

  8. Effective Java 65 Don't ignore exceptions

    Principle An empty catch block defeats the purpose of exceptions, which is to force you to handle ex ...

  9. eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中

    eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...

  10. mybatis java.lang.UnsupportedOperationException

    mybatis抛出下面的异常: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exc ...