springmvc 获取用户报错:No thread-bound request found: Are you referring to request attributes outside of a

获取用户报错:No thread-bound request found: Are you referring to request attributes outside of an aceb 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.

注释掉 HttpServletRequest request;

/*    @Autowired
    private HttpServletRequest request;*/

 

 

 

1.解释:RequestContextHolder顾名思义,持有上下文的Request容器


RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
2.获取session对象的内容:
requestAttributes.
        getAttribute(AccountAdminFilter.DEFAULT_ADMIN_ATTR_NAME, RequestAttributes.SCOPE_REQUEST);

第二个值可以有三个值:

int SCOPE_REQUEST = 0;
int SCOPE_SESSION = 1;
int SCOPE_GLOBAL_SESSION = 2;
分别获取request,session,全局session对象;

3.获取的对象需要经过强转为所需对象;
————————————————
版权声明:本文为CSDN博主「万年精魄」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34638435/article/details/79911002

你可能感兴趣的:(springmvc 获取用户报错:No thread-bound request found: Are you referring to request attributes outside of a)