首页 > 其他分享 >springmvc中获得HttpServletRequest request方法

springmvc中获得HttpServletRequest request方法

时间:2023-03-20 20:01:29浏览次数:62  
标签:HttpServletRequest web BaseController springmvc request Controller public


在web。xml中配置一个监听

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>


之后就可以在程序使用了


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






我用Spring MVC , 在Controller方法里每次都要写一个HttpServletRequest request参数,Spring才会注入request,然后我就产生了一个神奇的想法,写一个BaseController把request注入,然后其它Controller继承后就不需要再写request就可以直接使用父类的request:


abstract class BaseController {
private HttpServletRequest request;

public HttpServletRequest getRequest() {
return request;
}

@Resource
public void setRequest(HttpServletRequest request) {
this.request = request;
}

}


事实上这么也成功注入了,子Controller可以使用request。


标签:HttpServletRequest,web,BaseController,springmvc,request,Controller,public
From: https://blog.51cto.com/u_3871599/6138556

相关文章