Book Image

Mastering JavaServer Faces 2.2

By : Anghel Leonard
Book Image

Mastering JavaServer Faces 2.2

By: Anghel Leonard

Overview of this book

Table of Contents (20 chapters)
Mastering JavaServer Faces 2.2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The JSF Life Cycle
Index

Working with @ListenerFor and @ListenersFor


The @ListenerFor annotation is an interesting annotation available from JSF 2.0. This annotation allows a component to subscribe to particular events with the component itself being the listener. For this, we need to follow the ensuing steps:

  1. Implement the ComponentSystemEventListener interface (the name indicates that the event will always be associated with a UIComponent instance).

  2. Override the processEvent method (here we can play with the component).

  3. Use the @ListenerFor to indicate the event that the UI component will subscribe for, and the source class of the UI component.

For example, the UIInput component can subscribe to the PostAddToViewEvent event for adding attributes to the component, for example, following is a case, where we add some CSS to each UIInput component:

@ListenerFor(systemEventClass = PostAddToViewEvent.class, sourceClass = javax.faces.component.UIInput.class)
public class PlayerRenderer extends TextRenderer
             implements...