Saturday, September 4, 2010

Navigation Rule in JSF

The JavaServer Faces (JSF) Navigation Framework provides navigation rules that allow you to define navigation from view to view (mostly JSP pages) in a Web application. These navigation rules are defined in JSF configuration files along with other definitions for a JSF application. Usually, this file is named faces-config.xml. 
Here we will see  Navigation Rule Example in JSF
First create two jsp's login.jsp and home.jsp
In login.jsp type the below code
<h:outputLabel id="lb_USR_NAME" value="UserName:" styleClass="label"/>
<h:inputText  id="tf_USR_NAME"  autocomplete="off"  value="#{userlogin.tf_USR_NAME}"   />
 <h:outputLabel id="lb_PASS" value="Password:" styleClass="label"/>
 <h:inputText  id="tf_PASS" autocomplete="off" value="#{userlogin.tf_PASS}" />
<h:commandButton id="cb_login" value="Login" action="#{userlogin.submit}"/>
In home.jsp
 <body>
         <h1>Welcome to homepage </h1>
    </body>
In login.java paste the below code login.java is inside main package
package client.gui.main;


/**
 *
 * @author Subin Suresh
 */
public class Login{

  public String submit(){
 String status = null;
String userName = (String) getTf_USR_NAME().toString();
        String passWord = (String) getTf_PASS().toString();
 if (((userName.equals("subin"))) && ((passWord.equals("subin")))) {
status = "sucess";
}else{
status = "failure";
}

}

}
In faces-config.xml paste below code  

<managed-bean>
        <managed-bean-name>userlogin</managed-bean-name>
        <managed-bean-class>client.gui.main.Login</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

 <!-- ========== Navigation Rules Start  ========== -->
    <navigation-rule>
        <description>Navigation Rule for Login page</description>
        <from-view-id>/login.jsp</from-view-id>
        <navigation-case>
            <description>Redirects the page on "SUCCESS" outcome from login.bt_LOGINActionPerformed()</description>
            <from-action>#{userlogin.submit}</from-action>
            <from-outcome>sucess</from-outcome>
            <to-view-id>/home.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <description>Redirects the page on "Failure" outcome from login.bt_LOGINActionPerformed()</description>
            <from-action>#{userlogin.submit}</from-action>
            <from-outcome>failure</from-outcome>
            <to-view-id>/login.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
  <!-- =========== Navigation Rules Ends ========== -->



JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF and more.....

No comments:

Post a Comment