Tuesday, October 12, 2010

How the immediate attribute work in jsf


Immediate in jSF :immediate is attribute of UIComponent it may be UICommand or  UIInput.
The main  used of the immediate is to interrupt the flow of the jsf life cycle .
This is vital prosperities  in jsf .
The biggest myth about the immediate is that it’s skip the Process Validation which is not totally true .
Life cycle :the life cycle of the jsf is divided into six following step

1-Restore view
2-apply request values
3-Process validation 
4-Update Model values
5-Invoake Application
6-Render Response

The main used of the immediate attribute to skip the some phases and also process phases before it’s normal flow .
First immediate have two state .it’s means it a Boolean .either true or false .by default it value is false .
Case 1:
What happen if we used the immediate attribute true on the command button or command link ?

by default the immediate is false in this situation it’s validate  all the current ui component before going to navigate to different pages .
to solve this problem define the immediate as true on the command button .in this case it escape all the validation on the component  and navigation you different pages . 

Code of simple.jsp is following 


<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html">
  <jsp:output omit-xml-declaration="true" doctype-root-element="HTML"
              doctype-system="http://www.w3.org/TR/html4/loose.dtd"
              doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>simpleonetwozeo</title>
      </head>
      <body>
        <h:form>
          <p>
            userName<h:inputText  value="#{backingbean.userName}"
                                 valueChangeListener="#{backingbean.valueString}"  >
            <f:validateLength maximum="5" minimum="2" >
                         </f:validateLength>
            </h:inputText>
          </p>
          <p>
            passWord<h:inputText  value="#{backingbean.passWord}">
            <f:validateLength maximum="5" minimum="3" >
                         </f:validateLength>
     </h:inputText>
          </p>
          <p>
            <h:commandButton value="Submit"
                             actionListener="#{backingbean.stringMethod}"
                             action="success"   immediate="true"  />
            <h:messages ></h:messages>
          </p>
        </h:form>
      </body>
    </html>
  </f:view>
</jsp:root>



Behind the screen :Here the only one  main aspect is that jsf skip the three phases and direct come on the render response phase  from the apply request values .
Apply request values   ----------------------------------------------------------------àRender Response

Case 2:what happened if you put immediate true on the UIInput component ?
In this situation the immediate true component are validated before normal component .so the validation message is less. And this case you are not able to call the action listener or action attribute of the command button .this just like make one or more input components high priority for validation .
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html">
  <jsp:output omit-xml-declaration="true" doctype-root-element="HTML"
              doctype-system="http://www.w3.org/TR/html4/loose.dtd"
              doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>simpleonetwozeo</title>
      </head>
      <body>
        <h:form>
          <p>
            userName<h:inputText  value="#{backingbean.userName}" 
                                 valueChangeListener="#{backingbean.valueString}"
                                 binding="#{backingbean.usernamebind}" immediate="true">
            <f:validateLength maximum="5" minimum="2" >
                         </f:validateLength>
            </h:inputText>
          </p>
          <p>
            passWord<h:inputText id="az"  value="#{backingbean.passWord}"   binding="#{backingbean.passwordbind}">
            <f:validateLength maximum="5" minimum="3" >
                         </f:validateLength>
     </h:inputText>
          </p>
          <p>
            <h:commandButton value="Submit"
                             actionListener="#{backingbean.stringMethod}"
                             action="success"  />
            <h:messages ></h:messages>
          </p>
        </h:form>
      </body>
    </html>
  </f:view>
</jsp:root>