April 2004
Intermediate to advanced
606 pages
20h 4m
English
A less common scenario is that a request is first processed by a regular servlet, but a JSF view is used to generate the response. One example of where this approach is needed is for an application framework that needs to process the request before passing it on to a JSF view.
To let a JSF view generate a response to a non-JSF request, the
servlet must create a FacesContext instance,
configure it with the view, and ask JSF to render the response.
Here’s the code for doing this:
import javax.faces.FactoryFinder; import javax.faces.application.Application; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import javax.faces.context.FacesContextFactory; import javax.faces.lifecycle.LifecycleFactory; import javax.faces.lifecycle.Lifecycle; ... public class MyServet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { // Whatever processing is needed ... LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); FacesContext context = fcFactory.getFacesContext(context, request, response, lifecycle); Application application = context.getApplication( ); ViewHandler ...Read now
Unlock full access