HTML and CSS Reference
In-Depth Information
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import javax.faces.model.SelectItem;
public class ListingLoader implements SystemEventListener {
public static final String PROFESSIONS_KEY = "professions";
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
Map<String, Object> applicationMap = FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap();
if (event instanceof PostConstructApplicationEvent) {
//Load the listing data in the startup ...
applicationMap.put(PROFESSIONS_KEY, getSampleProfessionList());
} else if (event instanceof PreDestroyApplicationEvent) {
//Unload the listing data in the shutdown ...
applicationMap.remove(PROFESSIONS_KEY);
}
}
@Override
public boolean isListenerForSource(Object source) {
return source instanceof Application;
}
private List<SelectItem> getSampleProfessionList() {
List<SelectItem> sampleProfessions = new ArrayList<SelectItem>();
sampleProfessions.add(new SelectItem("Profession1"));
sampleProfessions.add(new SelectItem("Profession2"));
sampleProfessions.add(new SelectItem("Profession3"));
sampleProfessions.add(new SelectItem("Other"));
return sampleProfessions;
}
}
In order to implement
SystemEventListener
interface, we need to provide the implementation for two methods:
1.
isListenerForSource(Object source):
This method should return true if the event
listener is interested in receiving events from the source object. For our listener, the event
listener is interested only in receiving events from
Application
object.
processEvent(SystemEvent event):
This method will be called once the
SystemEvent
is received and
ready for processing. In our listener, event object is checked to be either
PostConstructApplicationEvent
or
PreDestroyApplicationEvent
. If it is a
PostConstructApplicationEvent
, the profession list is set in a map entry