Spring Mvc 在非controller层 实现获取request对象
一般我们在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对象的更多相关文章
- Spring MVC 注解之controller层
第一层注解:@Controller 和 @RestController. 这两个注解的作用是:处理页面的HTTP请求,不同点 @RestController相当于@Controller +@Respo ...
- spring mvc aop拦截controller层获取RequestBody反序列化后参数
最近,为了解耦,把一逻辑从interceptor抽出来,放在aop中处理,需要得到RequestBody.如下: @Aspect @Configuration public class CheckAs ...
- spring mvc中几种获取request对象的方式
在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...
- springmvc中获取request对象,加载biz(service)的方法
获取request对象: 首先配置web.xml文件--> <listener> <listener-class> org.springframework.web.con ...
- 在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...
- 在SpringMVC中获取request对象的几种方式
1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...
- java webservice服务器端获取request对象的三种方式
有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...
- webservice服务器端获取request对象的三种方式
有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...
- 如何在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...
随机推荐
- Android 开源库和项目
1.手势解锁 史上最完美的 手势密码解锁 2.数据库操作 Android数据库框架itePal https://github.com/LitePalFramework/LitePal 轻量级数据库:a ...
- Python: 关于nose
1. 使用rednose增强输出 pip install rednose nosetests --rednose tests 2. 使用coverage pip install coverage no ...
- 关于bitcode~
最近在做语音识别- 在真机调试的时候一直报 ld: '/Users/Chenglijuan/Documents/语音识别/lib/iflyMSC.framework/iflyMSC(IFlyRecog ...
- sublime text 3 如何安装 package control
sublime text3 是个很好的编辑工具,前端程序员觉得她很好,我是在一次视频中看到她能帮助自动完成很多快捷的操作. 为什么安装? 如果想要给sublime text 中安装别的插件(这里称呼为 ...
- android 进程间通信---Service Manager(2)
关于servicemanager的设计: 还是这张结构图,由于ProcessState & IPCThreadState是与binder deriver交互的, 所以对于client端来说Bp ...
- UNIX/Linux下C语言的学习路线
一.工具篇 “公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工具. 1.操作系统 在UNIX或 ...
- 你连Bug都抓不住,还谈什么参与感?
林子大了什么鸟都有,APP市场也是这样.举个例子,有段时期图片社交井喷式发展,各类图片社交APP一时充斥着市场.各种或重视图片加工或主打社交元素的APP“来得快去得快”.“你方唱罢我登场”,这些短命A ...
- ppt
放映时 F5是从头开始放映, shift+F5是从当前页开始放映 在菜单->幻灯片放映->勾选 “使用演讲者视图” 就可以在播放时看到自己的备注
- 一个人的Scrum之准备工作
在2012年里,我想自己一人去实践一下Scrum,所以才有了这么一个开篇. 最近看了<轻松的Scrum之旅>这本书,感觉对我非常有益.书中像讲述故事一样描述了在执行Scrum过程中的点点滴 ...
- 【API】短信通106端口验证短信的实现
信息时代,无论是电商还是网络营(chuan)销(xiao)都希望得道更多的用户信息.所以很多的网站注册上用到了手机验证码功能.网上有很多的SMS接口提供商.在选择的时候无非就是考虑到1.发送速度:2. ...