A map of the lands where the Trobadors flourished.
IntroductionThere are a number of
SystemEvents supported by JSF 2.x. A question that comes up frequently is how to implement them. In a number of cases on stackoverflow, it is implemented using a
PhaseListener. I was looking for a way to cleanup the view map, or just get values from it before it was destroyed. I decided that the simplest way to do so was to implement a
ViewMapListener. I also noticed that there were very few posts on how to implement it using the
faces-config.xmlso I decided to use that approach since it was instructive and more clear to me.
ImplementationThe basic implementation requires that you add our listener implementation to the
faces-config.xml. The example I have here is designed to get called on a
PreDestroyViewMapEventwhich is called on a normal navigation. We can force it though by adding a
@PreDestroyannotation to a method to invoke before being destroyed. Inside the method we would need to get the
UIViewrootview map, and call
clear(). This would cause our listener to be invoked too. It would be a good cleanup mechanism for cleaning up resources on session expiration too, but at the moment this does not work on JSF 2.1. The
@PreDestroyis not called on session timeout on JSF 2.1. This is expected to be an enhancement in JSF 2.2+.
The code for the project can be downloaded from Bitbuket here: viewmaplistener-example