spring web應用中其它層如何在controller不直接傳遞HttpSerrvletRequest對象的情況下直接獲取HttpSerrvletRequest對象

一般情況下,我們要想獲得當前請求的HttpServletRequest對象,都是在Controller中獲得或者從該層傳遞到下層,那麼如何在Controller沒有傳遞該對象時獲取到呢,在spring中,就要利用RequestContextHolder這個對象,代碼如下:

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