JavaServer Faces (JSF) and Web Components
Domain 3 covers the JSF request lifecycle, Facelets templating, managed bean scopes and the Ajax partial-processing model. Questions usually show a page fragment and ask which phase runs, which listener fires, or which scope survives a navigation.
Lifecycle and processing
- Six phases: Restore View, Apply Request Values, Process Validations, Update Model Values, Invoke Application, Render Response. Conversion and validation happen in Process Validations, before model values are written.
- immediate=true on an action component jumps from Apply Request Values straight to Render Response, skipping validation; it suits cancel buttons.
- A conversion or validation failure ends the lifecycle at Render Response, so the model is never updated.
Scopes and Ajax
- JSF 2.2 scopes: @RequestScoped, @ViewScoped (survives postbacks to the same view), @SessionScoped, @ApplicationScoped, @FlowScoped, plus CDI @ConversationScoped.
- f:ajax execute decides what is processed and render decides what is redrawn; keywords are @this, @form, @all and @none.
- An outcome ending in faces-redirect=true performs POST-Redirect-GET, which ends request scope and keeps only flash and view-scoped data.
- Facelets uses ui:composition, ui:insert and ui:define; JSF 2.2 adds h:inputFile, f:viewAction, faces flows and HTML5-friendly passthrough attributes.
Worked example
A search page loses its results after a browser refresh. The bean is @RequestScoped and the action returns the outcome results without a redirect, so a refresh re-posts the form and builds an empty request scope. Making the outcome results?faces-redirect=true and moving the results into @ViewScoped or flash scope fixes it.
Traps
- Expecting @RequestScoped data to survive a redirect.
- Adding immediate=true and then wondering why validators never executed.
- Mixing javax.faces.bean scopes with CDI javax.enterprise.context scopes on one bean.