Thx Matthias.

Your comments makes sense.  The reason I am confused is because I am making a 
call from a JSF page
to a utility class stored in the session.  Within this utility class I am 
trying to call
"FacesContext.getCurrentInstance()" and I get a null pointer.

Mike


--- Matthias Wessendorf <[EMAIL PROTECTED]> wrote:

> Well,
> 
> if you are *inside* a Faces Request (gone through the FacesServlet)
> you get a FacesContext object when calling FC.getCurrentI...();
> 
> outside of a faces req. it will return null, since per each faces req
> the FacesContext is *created*
> 
> HTH,
> Matthias
> 
> On 5/17/06, Mike Duffy <[EMAIL PROTECTED]> wrote:
> > I guess my basic question is, shouldn't I be able to call 
> > "FacesContext.getCurrentInstance()"
> from
> > anywhere in my JSF application and have it return a context?  Why would it 
> > ever return null?
> >
> > Mike
> >
> >
> >
> > --- Julian Ray <[EMAIL PROTECTED]> wrote:
> >
> > > Hi Mike,
> > >
> > > Here is some code which shows how we get the faces context in a servlet. 
> > > It also has a
> utility
> > > method retrieving managed beans.
> > >
> > > public abstract class AbstractFacesServlet extends HttpServlet {
> > >
> > >     /** Creates a new instance of AbstractFacesServlet */
> > >     public AbstractFacesServlet() {
> > >         super();
> > >     }
> > >     public void init(ServletConfig config) throws ServletException {
> > >         super.init(config);
> > >     }
> > >     protected abstract void processRequest(HttpServletRequest request, 
> > > HttpServletResponse
> > > response) throws ServletException, IOException;
> > >
> > >     /** Handles the HTTP <code>GET</code> method.
> > >     * @param request servlet request
> > >     * @param response servlet response
> > >     */
> > >     protected void doGet(HttpServletRequest request, HttpServletResponse 
> > > response) throws
> > > ServletException, IOException {
> > >         processRequest(request, response);
> > >     }
> > >     protected void log(FacesContext facesContext, String message) {
> > >         facesContext.getExternalContext().log(message);
> > >     }
> > >     /** Handles the HTTP <code>POST</code> method.
> > >     * @param request servlet request
> > >     * @param response servlet response
> > >     */
> > >     protected void doPost(HttpServletRequest request, HttpServletResponse 
> > > response) throws
> > > ServletException, IOException {
> > >         processRequest(request, response);
> > >     }
> > >     protected FacesContext getFacesContext(HttpServletRequest request, 
> > > HttpServletResponse
> > > response) {
> > >         FacesContext facesContext = FacesContext.getCurrentInstance();
> > >         if (facesContext == null) {
> > >
> > >             FacesContextFactory contextFactory  =
> > > (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
> > >             LifecycleFactory lifecycleFactory =
> > > (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
> > >             Lifecycle lifecycle =
> > > lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
> > >
> > >             facesContext =
> > > contextFactory.getFacesContext(request.getSession().getServletContext(), 
> > > request, response,
> > > lifecycle);
> > >
> > >             // Set using our inner class
> > >             
> > > InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
> > >
> > >             // set a new viewRoot, otherwise context.getViewRoot returns 
> > > null
> > >             UIViewRoot view =
> > > facesContext.getApplication().getViewHandler().createView(facesContext, 
> > > "cms");
> > >             facesContext.setViewRoot(view);
> > >         }
> > >         return facesContext;
> > >     }
> > >     protected Object getBean(String beanName, FacesContext facesContext) {
> > >         return
> getApplication(facesContext).getVariableResolver().resolveVariable(facesContext,
> > > beanName);
> > >     }
> > >     // You need an inner class to be able to call 
> > > FacesContext.setCurrentInstance
> > >     // since it's a protected method
> > >     private abstract static class InnerFacesContext extends FacesContext {
> > >         protected static void 
> > > setFacesContextAsCurrentInstance(FacesContext facesContext) {
> > >             FacesContext.setCurrentInstance(facesContext);
> > >         }
> > >     }
> > > }
> > >
> > >
> > > ----- Original Message ----
> > > From: Mike Duffy <[EMAIL PROTECTED]>
> > > To: [email protected]
> > > Sent: Wednesday, May 17, 2006 12:43:28 PM
> > > Subject: Can the FacesContext be Accessed from Outside of a Backing Bean?
> > >
> > > If I call the following from inside a backing bean, everything works fine:
> > >
> > > User user = (User)
> > >
> FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.USER_KEY);
> > >
> > > However, if I make the same call from an object in the session or from a 
> > > utilty class, I get
> a
> > > null pointer on "FacesContext.getCurrentInstance()".
> > >
> > > Can the FacesContext be accessed from outside of a backing bean?
> > >
> > > Thx.
> > >
> > > Mike
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Tired of spam?  Yahoo! Mail has the best spam protection around
> > > http://mail.yahoo.com
> > >
> > >
> > >
> > >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> >
> 
> 
> -- 
> Matthias Wessendorf
> Aechterhoek 18
> 48282 Emsdetten
> http://jroller.com/page/mwessendorf
> mwessendorf-at-gmail-dot-com
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to