本文参考:http://blog.sina.com.cn/s/blog_7085382f0102v9jg.html
http://www.cnblogs.com/xusir/p/4135419.html
1.最简单的方式(注解法)
1
2
|
@Autowired
private
HttpServletRequest request;
|
2.最麻烦的方法
a. 在web.xml中配置一个监听
<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
b.之后在程序里可以用
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
3.最直接的方法
1
|
public
String hello(HttpServletRequest request,HttpServletResponse response)
|
4.注解(现在用的)
在BaseController中加入:
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
this.request = request;
this.response = response;
this.session = request.getSession();
}
说明:ModelAttribute的作用