第一种、参数
1
2
3
|
@RequestMapping
(
"/test"
)
@ResponseBodypublic
void
saveTest(HttpServletRequest req, HttpServletResponse resp){
}
|
第二种、注解
1
2
|
@Autowired
private
HttpServletRequest request;
|
第三种、上下文获取
1、在web.xml配置监听器
1
2
|
<listener>
<listener-
class
>org.springframework.web.context.request.RequestContextListener</listener-
class
></listener>
|
2、程序使用
1
2
|
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
|
根据个人喜好使用
推荐第三种
本文转自 Y幕徐 51CTO博客,原文链接:http://blog.51cto.com/765133133/1954465