Where is the proper ‘place’ in JSF to put initialisation snippet that follows, in order to get it executed just one time when the server starts?
1. ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
2. HttpSession sesion = (HttpSession)extContext.getSession(true);
3. String parA = extContext.getInitParameter("parA");
4. String parB = someCalculations(parA);
5. sesion.setAttribute("parB", parB);
Basically I want to read a parameter parA from web.xml context-param section, do some transformations, and include it in session (as new parB parameter).
PostConstructApplicationEvent and eager=true techniques doesn’t works because session is null at this point (line 4).
ServletContextListener technique doesn’t works because FacesContext isn’t available.
Thanks!
There are no sessions at application start time; this requirement is impossible to meet.
I interpret your requirements as:
The JSF way to do this is via managed beans. Here is an application-scope bean to perform the one-time transformation of the context parameter:
This value can then be injected into other scopes as you need it:
Code untested. This implementation assumes JSF annotation support but you can do the same thing with
faces-config.xmlbean configuration.