views:

282

answers:

1

Hi all! I have a problem - I am trying to get FacesContext by calling FacesContext.getCurrentInstance(), but it returns null.

I am using jsf with richfaces, here my web.xml:

     <?xml version="1.0" encoding="UTF-8"?>
     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>sfront</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
  <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
 <listener>
  <listener-class>no.sfront.web.listeners.StartupListener</listener-class>
 </listener>
 <context-param>
  <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>client</param-value>
 </context-param>
 <context-param>
  <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
  <param-value>resources.application</param-value>
 </context-param>
 <listener>
  <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
 </listener>
 <context-param>
  <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
  <param-value>.xhtml</param-value>
 </context-param>
 <context-param>
  <param-name>facelets.LIBRARIES</param-name>
  <param-value> /WEB-INF/lib/fusioncharts.taglib.xml </param-value>
 </context-param>

 <!-- Plugging the "Blue Sky" skin into the project -->
 <context-param>
  <param-name>org.richfaces.SKIN</param-name>
  <param-value>blueSky</param-value>
 </context-param>

 <!-- Making the RichFaces skin spread to standard HTML controls -->
 <context-param>
  <param-name>org.richfaces.CONTROL_SKINNING</param-name>
  <param-value>enable</param-value>
 </context-param>

 <!-- Defining and mapping the RichFaces filter -->
 <filter>
  <display-name>RichFaces Filter</display-name>
  <filter-name>richfaces</filter-name>
  <filter-class>org.ajax4jsf.Filter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>richfaces</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher>
 </filter-mapping>

 <filter>
  <filter-name>Extensions Filter</filter-name>
  <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>Extensions Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
 </filter-mapping>

 <filter>
  <filter-name>Login Filter</filter-name>
  <filter-class>no.sfront.login.LoginFilter</filter-class> 
 </filter>
 <filter-mapping>
  <filter-name>Login Filter</filter-name>
  <url-pattern>*</url-pattern>
 </filter-mapping>
</web-app>

Maybe the problem is in that - I am trying to get facesContext from run() method of some Runnable class.

Any help? Please!

+2  A: 

I am trying to get FacesContext by calling FacesContext.getCurrentInstance(), but it returns null. I am trying to get facesContext from run() method of some Runnable class.

The FacesContext is stored as a ThreadLocal variable in the thread responsible for the HTTP request. This thread usually goes through the managed bean methods only. The FacesContext is not available in all other threads. You should actually also not have the need for it in other threads. You need to solve your problem differently. Ask yourself: what do you need it for? To obtain some information? Just pass that information to the Runnable yourself during its construction instead.

BalusC