0

I have a filter: UserSessionFilter.java, that I use to handle user sessions and a ApplicationScoped bean: Config.java that primarily gives me access to a DAO factory.

Config.java

@ManagedBean (eager = true)
@ApplicationScoped
public class Config implements Serializable {...

The filter calls a method in Config to get a DAOFactory object:

Filter method

@Override
public void init(FilterConfig filterConfig) {
    daoFactory = Config.getInstance().getDAOFactory();
}

Config method

public static Config getInstance() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return (Config) facesContext.getApplication().evaluateExpressionGet(
                 facesContext, "#{config}", Config.class);
}

My problem is that the facesContext gets set to null. This problem started occurring after switch from Mojarra to MyFaces, although it seems weird that that would cause it.

nivis
  • 913
  • 3
  • 17
  • 34

1 Answers1

0

FacesContext cannot be used inside the Filter. See this or this answers. Reason:

The FacesContext is created by the FacesServlet and thus only available within any Java code which is processed by the FacesServlet, which covers all JSF artifacts, such as managed beans and phase listeners

Community
  • 1
  • 1
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • Yes. But this is happening inside the `ApplicationScoped and eager` bean called `Config` and does not give an error when using `Mojarra`, only when using `MyFaces` – nivis Feb 16 '14 at 22:38
  • I had exact the same behavior in Mojarra too. – Vasil Lukach Feb 17 '14 at 00:18
  • If you need the instance of Config, then you can try `request.getSession().getAttribute("config")` inside `doFilter` method of your filter class in case if Config already exist in session. – Vasil Lukach Feb 17 '14 at 00:31