// 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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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
>