在SpringMVC中获取request对象

时间:2022-05-07 18:49:16

1.注解法

  1. @Autowired
  2. private  HttpServletRequest request;

2. 在web.xml中配置一个监听

  1. <listener>
  2. <listener-class>
  3. org.springframework.web.context.request.RequestContextListener
  4. </listener-class>
  5. </listener>

之后在程序里可以用

  1. HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

3.直接在参数中引入

  1. public String hello(HttpServletRequest request,HttpServletResponse response)

如何在Struts2中获取request对象

  1. HttpServletRequest request = ServletActionContext.getRequest();