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?
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
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

