spring web应用中其它层如何在controller不直接传递HttpSerrvletRequest对象的情况下直接获取HttpSerrvletRequest对象

一般情况下,我们要想获得当前请求的HttpServletRequest对象,都是在Controller中获得或者从该层传递到下层,那么如何在Controller没有传递该对象时获取到呢,在spring中,就要利用RequestContextHolder这个对象,代码如下:

HttpServletRequest httpServletRequest = 
    ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章