Showing posts with label JSF Lifecycle. Show all posts
Showing posts with label JSF Lifecycle. Show all posts

Tuesday, December 8, 2009

Debug JSf LifeCycle

You can use a PhaseListener to trace the phases of the JSF lifecycle and execute some processes where required. But you can also use a "dummy" PhaseListener to debug the phases to see what is happening in which phase. Here is a basic example of such a LifeCycleListener:
package mypackage;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener; 
public class LifeCycleListener implements PhaseListener { 
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    } 
    public void beforePhase(PhaseEvent event) {
        System.out.println("START PHASE " + event.getPhaseId());
    } 
    public void afterPhase(PhaseEvent event) {
        System.out.println("END PHASE " + event.getPhaseId());
    } 
}
Add the following lines to the faces-config.xml to activate the LifeCycleListener.
    mypackage.LifeCycleListener
This produces like the following in the system output:
START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE APPLY_REQUEST_VALUES 2
END PHASE APPLY_REQUEST_VALUES 2
START PHASE PROCESS_VALIDATIONS 3
END PHASE PROCESS_VALIDATIONS 3
START PHASE UPDATE_MODEL_VALUES 4
END PHASE UPDATE_MODEL_VALUES 4
START PHASE INVOKE_APPLICATION 5
END PHASE INVOKE_APPLICATION 5
START PHASE RENDER_RESPONSE 6
END PHASE RENDER_RESPONSE 6

Monday, December 7, 2009

JSF with JavaScript

Could define a composition component for as below to support the javascript in JSF and use them in normal xhtml / jsf pages.
So we would have the flexibility of rendering and not rendering the Javascript in the pages with the JSF life-cycle.
Javascript.xhtml (below, replace "[" with "<")

[ui-composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"]

    [c:choose]
      [c:when test="#{not empty path}"]

    [script type="text/javascriptt"                    
      srcc="#{facesContext.externalContext.requestContextPath}
           #{path}"]
    [/script]
       [/c:when]
       [c:0therwise]
          [script type="text/javascript"]
              [ui:insert /]
          [/script]
       [/c:otherwise]
   [/c:ch00se]

[/ui:composition]

In JSF/xhtml page, we could render javascript functions as below.
[h:form]
[yournameSpace:javascriptt]
   Function hello(){ alert(‘hello…’); }
[/yournamespace:javascript]

[yournameSpace:javascript rendered=”#{condition}”]
   Function hello1(){ alert(‘hello…’); }
[/yournamespace:javascript]
[/h:form]

So we have the flexibility to render/ not-render/ re-Render by AJAX.
Handling data between the JSF & Javascript will be easy

Thursday, August 20, 2009

JSF Life Cycle

The six phases of the JSF application lifecycle are as follows (note the event processing at each phase):
  1. Restore View
  2. Apply request values; process events
  3. Process validations; process events
  4. Update model values; process events
  5. Invoke application; process events
  6. Render response

The six phases show the order in which JSF typically processes a form GUI. The list shows the phases in their likely order of execution with event processing at each phase, but the JSF lifecycle is hardly set in stone. You can change the order of execution by skipping phases or leaving the lifecycle altogether. For example, if an invalid request value were copied to a component, the current view would be redisplayed, and some of the phases might not execute. In this case, you could issue a FacesContext.responseComplete method invocation to redirect the user to a different page, then use the request dispatcher (retrieved from the request object in the FacesContext) to forward to an appropriate Web resource. Alternately, you could call FacesContext.renderResponse to re-render the original view.

Focusing your efforts

Some developers using JSF may never write a component or extend the framework, while others may focus on just those tasks. While the JSF lifecycle will be the same for almost any project, you'll likely tap into it at different stages based on your role in the project. If you're concentrating more on the overall application development, you'll likely be concerned with the middle phases of the request processing lifecycle:
  • Apply requests values
  • Process validations
  • Update model values
  • Invoke application
If you're concentrating on JSF component development, you'll probably focus on the first and last phases of the lifecycle:
  • Restore view
  • Render response
In the sections that follow, I'll walk you through every phase of the JSF request processing lifecycle, including event handling and validation. Once you have a basic understanding of each phase, I'll introduce a sample application that shows how they all come together. Before we get started, take a look at Figure 1, a diagram of the JSF lifecycle.
From - http://www.ibm.com/developerworks/library/j-jsf2/