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