RequestContextListener有什么用
发布网友
发布时间:2022-04-23 00:43
我来回答
共1个回答
热心网友
时间:2022-04-26 13:30
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();