木头人
2017-12-19 21:30:00
spring mvc在service层获取request对象
只知道在用struts2的时候,要在controller层使用request对象,在原有的基础上集成ActionSupport,实现ServletRequestAware最终实现servletRequest的set方法。就可以在action层使用request对象了。其他的cookie,session,response都可以通过request对象获取到。如果在service层面获取request对象,这个时候使用ServletActionContext就直接可以获取到上下文的request对象
ServletActionContext.getRequest(); //或者使用 ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
在springmvc中怎么获取呢?springmvc现在被热捧,Struts2频繁的爆出漏洞。在设计上,springmvc更灵活,我在controller中的方法里面想获取什么参数就传入什么参数(当然不是胡乱的传值,请求参数,session,request,response这些是允许的)。但是相同的问题在service层怎么获取request对象的。一个笨办法是把request对象当做参数一层一层的往下传。但是对方法的可读性,维护性太影响了。再说像我这样的代码强迫症患者怎么办。springmvc也和struts2一样,也可以获取。
<!--1.在web.xml中配置监听器--> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
//2.在service层通过RequestContextHolder获取 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
评论

thw666 LV2
2022年5月24日
hhuangh LV5
2019年11月21日
niulang LV2
2019年9月4日
zuidaima0012 LV4
2019年8月5日
Cheart LV5
2019年7月2日
suxiansheng LV3
2019年6月11日
韩毅飞 LV11
2019年4月13日
a316956515 LV1
2019年3月12日
tmh9612255 LV2
2019年1月28日
hlzcy LV13
2019年1月3日