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 ...
随机推荐
- 每日微软面试题——day 6(打印所有对称子串)
每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...
- CSS选择器性能分析
写了几篇关于js的博客,也是关于性能的,现在,我觉得有必要那css来认真分析一下了.之前只是看别人这么写就跟着写,但是没有去研究这样写或者是不是正确的写法,性价比怎么样,渲染的效率好么!这些都没有考虑 ...
- SharePoint 使用代码为页面添加WebPart
传统的SharePoint实施中,我们通常会创建SharePoint页面,然后添加webpartzone,而后在上面添加webpart:但是有些情况下,也要求我们使用代码,将webpart添加到相应w ...
- 总结隐藏Ribbon菜单的方法
1. 重载 using (SPSite site = new SPSite("http://SP2010-01")) { using (SPWeb web = site.OpenW ...
- 我的GTD中收集的书单
在几年的GTD过程中,收集了一些想读的书目,没有系统地整理,每当读完一本之后,就翻翻书单,寻找下一本感兴趣的书,书是不可能读完的,只能找有兴趣的.符合自己目标方向的.有些书记录了豆瓣上的评分,虽然不是 ...
- 关于https的Error:Error Domain=NSURLErrorDomain Code=-1012
昨天闲着没事就随便搞点demo,随便找了一个https的接口,运行之后,一直发现Error Domain=NSURLErrorDomain Code=-1012.好奇怪,请求https的配置我基本都配 ...
- Android中ListView 控件与 Adapter 适配器如何使用?
一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...
- c++虚函数,纯虚函数,抽象类,覆盖,重载,隐藏
C++虚函数表解析(转) ——写的真不错,忍不住转了 http://blog.csdn.net/hairetz/article/details/4137000 浅谈C++多态性 http://bl ...
- Effective Java 38 Check parameters for validity
For public methods, use the Javadoc @throws tag to document the exception that will be thrown if a r ...
- 浅谈Python时间模块
浅谈Python时间模块 今天简单总结了一下Python处理时间和日期方面的模块,主要就是datetime.time.calendar三个模块的使用.希望这篇文章对于学习Python的朋友们有所帮助 ...