日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot : RequestContextHolder

發布時間:2024/9/19 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot : RequestContextHolder 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • springboot 2.0.0.RELEASE
  • sping-web 提供了 RequestContextHolder 。RequestContextHolder 讓開發者可以在請求的任意位置獲取到 request、response、session 等。
  • 因 RequestContextHolder 是 sping-web 提供的,所以 springboot 中也可以使用。

獲取請求參數

RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); //RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//獲取請求參數 String str = (String) requestAttributes.getAttribute("name", RequestAttributes.SCOPE_REQUEST);

從SESSION取值

RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();//從session里面獲取對應的值 String str = (String) requestAttributes.getAttribute("name", RequestAttributes.SCOPE_SESSION);

獲取request、response、session

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); HttpServletResponse response = attributes.getResponse(); HttpSession session = attributes.getRequest().getSession();

getRequestAttributes vs currentRequestAttributes

/*** Return the RequestAttributes currently bound to the thread.* @return the RequestAttributes currently bound to the thread,* or {@code null} if none bound*/@Nullablepublic static RequestAttributes getRequestAttributes() {RequestAttributes attributes = requestAttributesHolder.get();if (attributes == null) {attributes = inheritableRequestAttributesHolder.get();}return attributes;} /*** Return the RequestAttributes currently bound to the thread.* <p>Exposes the previously bound RequestAttributes instance, if any.* Falls back to the current JSF FacesContext, if any.* @return the RequestAttributes currently bound to the thread* @throws IllegalStateException if no RequestAttributes object* is bound to the current thread* @see #setRequestAttributes* @see ServletRequestAttributes* @see FacesRequestAttributes* @see javax.faces.context.FacesContext#getCurrentInstance()*/public static RequestAttributes currentRequestAttributes() throws IllegalStateException {RequestAttributes attributes = getRequestAttributes();if (attributes == null) {if (jsfPresent) {attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();}if (attributes == null) {throw new IllegalStateException("No thread-bound request found: " +"Are you referring to request attributes outside of an actual web request, " +"or processing a request outside of the originally receiving thread? " +"If you are actually operating within a web request and still receive this message, " +"your code is probably running outside of DispatcherServlet: " +"In this case, use RequestContextListener or RequestContextFilter to expose the current request.");}}return attributes;}

總結

以上是生活随笔為你收集整理的Springboot : RequestContextHolder的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。