Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

In JSF 2.0 or later it is better to create a custom ResourceHandler wrapper and use FacesServlet instead. In that way, JSF will create FacesContext instance.

...

There have been several posts asking how to get a Faces Context from outside of JSF such as in a servlet. There are many reasons for doing this such as:1

  1. Getting to a managed bean in session

...

  1. scope

...

  1. Getting or setting the current view

...

  1. id

...

  1. Generating additional content to be displayed in a faces

...

  1. component

...

  1. Performing authentication and session

...

  1. management

...

  1. Various other reasons you can come up with...

...

Below is one way to do this.

...

We used several servlets and filters to drive external content into our JSF apps. In particular we use this approach to generate Jasper Reports, JFreeChart images and generate PDFs on the fly using iText. The same code is also used in a filter to manage sessions.

...

...

This is simple to use and extend and, as I stated earlier, has been handed around several lists. We provide convienience methods for access to managed beans and to the Application. Concrete classes extend the base class and provide getters for session-managed components etc.

Note: You should always relase the FacesContext after you're done with it (see http://ocpsoft.com/java/jsf-java/please-tell-your-developers-to-call-facescontextrelease/). I've added the removeFacesContext() method to the above code, and you should call that after calling .release() on the context.

In closing, I'm sure there may be better ways for doing this but more importantly, there are probably good reasons for not doing this at all and changing to Phase Listeners and staying within the JSF framework which I think is an important topic for continued discussion.

...