You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Eurig Jones <eu...@gmail.com> on 2006/08/14 21:39:32 UTC

Getting a JSF EL session bean value from an external Servlet

I'm trying to do the above with the following. Usually this code works 
fine in my beans:

(This code is within the doGet() method of the servlet)...

FacesContext c = FacesContext.getCurrentInstance();
Application application = c.getApplication();
SessionBean session = (SessionBean) 
application.createValueBinding(#{sessionBean}).getValue(c);
Game game = session.getCurrentGame();

The first line is not fetching the current FacesContext instance, just 
giving me a null.

How can I get the value from the EL from within the servlet?

Regards,
Eurig Jones
www.eurig.co.uk

Re: Getting a JSF EL session bean value from an external Servlet

Posted by Mike Kienenberger <mk...@gmail.com>.
    //  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);
        }
    }

    private FacesContext createFacesContext(ServletRequest request,
            ServletResponse response)
    {
        FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
        LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
        Lifecycle lifecycle = lifecycleFactory
                .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

        // Either set a private member servletContext =
        // filterConfig.getServletContext();
        // in you filter init() method or set it here like this:
        ServletContext servletContext =
((HttpServletRequest)request).getSession().getServletContext();
        // Note that the above line would fail if you are using any other
        // protocol than http

        // Doesn't set this instance as the current instance of
        // FacesContext.getCurrentInstance
        FacesContext facesContext =
contextFactory.getFacesContext(servletContext, 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, "yourOwnID");
        facesContext.setViewRoot(view);

        return facesContext;
    }


On 8/14/06, Andrew Robinson <an...@gmail.com> wrote:
> You can't access the faces context in a servlet that isn't included
> run from within the scope of the JSF servlet filter. You will need to
> get the faces context factory and build a new one. I don't have an
> example handy, but a google search should turn it up.
>
> -Andrew
>
> On 8/14/06, Eurig Jones <eu...@gmail.com> wrote:
> > I'm trying to do the above with the following. Usually this code works
> > fine in my beans:
> >
> > (This code is within the doGet() method of the servlet)...
> >
> > FacesContext c = FacesContext.getCurrentInstance();
> > Application application = c.getApplication();
> > SessionBean session = (SessionBean)
> > application.createValueBinding(#{sessionBean}).getValue(c);
> > Game game = session.getCurrentGame();
> >
> > The first line is not fetching the current FacesContext instance, just
> > giving me a null.
> >
> > How can I get the value from the EL from within the servlet?
> >
> > Regards,
> > Eurig Jones
> > www.eurig.co.uk
> >
>

Re: Getting a JSF EL session bean value from an external Servlet

Posted by Andrew Robinson <an...@gmail.com>.
You can't access the faces context in a servlet that isn't included
run from within the scope of the JSF servlet filter. You will need to
get the faces context factory and build a new one. I don't have an
example handy, but a google search should turn it up.

-Andrew

On 8/14/06, Eurig Jones <eu...@gmail.com> wrote:
> I'm trying to do the above with the following. Usually this code works
> fine in my beans:
>
> (This code is within the doGet() method of the servlet)...
>
> FacesContext c = FacesContext.getCurrentInstance();
> Application application = c.getApplication();
> SessionBean session = (SessionBean)
> application.createValueBinding(#{sessionBean}).getValue(c);
> Game game = session.getCurrentGame();
>
> The first line is not fetching the current FacesContext instance, just
> giving me a null.
>
> How can I get the value from the EL from within the servlet?
>
> Regards,
> Eurig Jones
> www.eurig.co.uk
>