如何在spring中获取request对象
1.通过注解获取(很简单,推荐):
public class Hello { @Autowired HttpServletRequest request; //这里可以获取到request }
2.在web.xml中配置一个监听:
<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
Java代码:HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
3.直接在参数中传递(此方法不好用,不建议使用):
public String hello(HttpServletRequest request)
4.如果有Struts:
HttpServletRequest request = ServletActionContext.getRequest();