由于是使用spring mvc来做项目,因此脱离了HttpServletRequest作为参数,不能够直接使用request,要想使用request可以使用下面的方法:
在web点xml中配置一个监听
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>之后就可以在程序使用了
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
由于是使用spring mvc来做项目,因此脱离了HttpServletRequest作为参数,不能够直接使用request,要想使用request可以使用下面的方法:
在web点xml中配置一个监听
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>之后就可以在程序使用了
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
本文介绍在SpringMVC框架下如何配置并获取HttpServletRequest对象。通过在web.xml中配置RequestContextListener监听器,并利用RequestContextHolder.getRequestAttributes()方法,可以在不将HttpServletRequest作为参数传递的情况下使用request对象。
2725

被折叠的 条评论
为什么被折叠?



