Accessing a JSF managedBean from a Servlet [duplic

2019-02-12 13:02发布

This question already has an answer here:

I need to know what's the best method for accessing a JSF managedBean (which is defined having application scope) from a servlet. Currently I have something like this in my servlet:

  MyApplicationScopeBean bean = null;
  try {
   FacesContext fContext = FacesUtil.getFacesContext(req, resp);
   ServletContext sc = (ServletContext) fContext.getExternalContext().getContext();
   bean = (MyApplicationScopeBean) sc.getAttribute("myManagedBean");   
  } catch (Exception e) {
   e.printStackTrace();
  }

FacesUtil.java (as described in http://balusc.blogspot.com/2006/06/communication-in-jsf.html):

import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FacesUtil {
    // Getters -----------------------------------------------------------------------------------
    public static FacesContext getFacesContext(
        HttpServletRequest request, HttpServletResponse response)
    {
        // Get current FacesContext.
        FacesContext facesContext = FacesContext.getCurrentInstance();
        // Check current FacesContext.
        if (facesContext == null) {
            // Create new Lifecycle.
            LifecycleFactory lifecycleFactory = (LifecycleFactory)
                FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
            Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
            // Create new FacesContext.
            FacesContextFactory contextFactory  = (FacesContextFactory)
                FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
            facesContext = contextFactory.getFacesContext(
                request.getSession().getServletContext(), request, response, lifecycle);
            // Create new View.
            UIViewRoot view = facesContext.getApplication().getViewHandler().createView(
                facesContext, "");
            facesContext.setViewRoot(view);                
            // Set current FacesContext.
            FacesContextWrapper.setCurrentInstance(facesContext);
        }
        return facesContext;
    }
    // Helpers -----------------------------------------------------------------------------------
    // Wrap the protected FacesContext.setCurrentInstance() in a inner class.
    private static abstract class FacesContextWrapper extends FacesContext {
        protected static void setCurrentInstance(FacesContext facesContext) {
            FacesContext.setCurrentInstance(facesContext);
        }
    }     
}

I always get a null when trying to access the bean from the servlet.
What are your suggestions? I'm running JSF 1.2 on Tomcat 6

Thanks for your help.

1条回答
再贱就再见
2楼-- · 2019-02-12 13:46

JSF stores application scoped managed beans just in the ServletContext. In servlets, the ServletContext is just available by the inherited getServletContext() method. You don't need to manually create a whole FacesContext around it. That's only an unnecessarily expensive task for this purpose.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Bean bean = (Bean) getServletContext().getAttribute("bean");
    // ...
}

If it returns null, then it simply means that JSF hasn't kicked in yet to auto-create the bean for you (i.e. the servlet is called too early). You would then need to create and store it yourself. It will be used by JSF if the managed bean name (the attribute key) is the same.

    if (bean == null) {
        bean = new Bean();
        getServletContext().setAttribute("bean", bean);
    }

That said, what's the purpose of this servlet? Aren't you trying to achieve some functional requirement the wrong way?

查看更多
登录 后发表回答