从Servlet访问JSF managedBean

时间:2010-08-26 19:11:55

标签: jsf servlets javabeans

我需要知道从servlet访问JSF managedBean(定义了应用程序范围)的最佳方法是什么。 目前我的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(如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);
        }
    }     
}

尝试从servlet访问bean时,我总是得到null。
你有什么建议? 我在Tomcat 6上运行JSF 1.2

谢谢你的帮助。

1 个答案:

答案 0 :(得分:20)

JSF仅在ServletContext中存储应用程序范围的托管bean。在servlet中,ServletContext仅由继承的getServletContext()方法提供。您无需在其周围手动创建整个FacesContext。对于这个目的,这只是一项不必要的昂贵任务。

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

如果它返回null,那么它只是意味着JSF还没有开始为你自动创建bean(即servlet被过早调用)。然后,您需要自己创建和存储它。如果托管bean名称(属性键)相同,它将由JSF使用。

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

那说,这个servlet的目的是什么?你是不是想以错误的方式实现某些功能需求?