Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?

后端 未结 1 1343
余生分开走
余生分开走 2021-01-13 01:30

In my Java EE 6-webapp (running on latest GlassFish 3.1), I\'m using JSF2-ManagedBeans and @ManagedProperty to inject them into other ManagedBeans. Now i would

相关标签:
1条回答
  • 2021-01-13 02:03

    Using @ManagedProperty in a servlet is not possible since this works in @ManagedBean classes only. Further, injecting an object which has a lesser scope than the parent itself is also not possible since that would also only end up in concurrency problems. The injector would throw a runtimeexception for that. A servlet is in essence application scoped and shared among all users and your UserIdentity bean seems to be session scoped.

    Since JSF runs on top of the Servlet API and stores the session scoped beans in, well, the session, you could in the servlet just grab it as session attribute:

    UserIdentity identity = (UserIdentity) request.getSession().getAttribute("userIdentity");
    

    Note that the FacesContext is usually also not available in a servlet other than FacesServlet, so using FacesContext in the servlet as suggested in a comment does not make any sense, that would only return null.

    0 讨论(0)
提交回复
热议问题