Home arrow Java arrow Page 4 - Introducing Java Server Faces (JSF)
JAVA

Introducing Java Server Faces (JSF)


This article will introduce you to Java Server Faces, including the terminology, how to set it up, and using it in the real world.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
October 17, 2006
TABLE OF CONTENTS:
  1. · Introducing Java Server Faces (JSF)
  2. · JSF: Terminology Continued
  3. · JSF: Steps for Implementation
  4. · JSF in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Introducing Java Server Faces (JSF) - JSF in the Real World
(Page 4 of 4 )

So to put the theories discussed till now into practice, I will be creating a simple login module with two navigation cases. If the validation succeeds it will show the welcome page or else it will show the login page again. The components of this application are:

  1. Login.jspx: the login page view. It uses JSF UI Components such as Input Text and the command button.
  2. Login.java: the managed bean for Login.jspx. It contains the event handler for the submit button.
  3. Welcome.jsp: the page to be shown when the validation succeeds.
  4. faces-config.xml: the descriptor that contains the navigation case.

So here is the code of Login.jspx. This file uses the XHTML format, hence the .jspx extension.

<?xml version='1.0' encoding='windows-1252'?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core">

  <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=windows-1252"/>

  <f:view>

    <html>

      <head>

        <meta http-equiv="Content-Type"

              content="text/html; charset=windows-1252"/>

        <title>untitled1</title>

      </head>

      <body><h:form binding="#{backing_Login.form1}" id="form1">

          <table cellspacing="2" cellpadding="3" border="1" width="100%">

            <tr>

              <td width="50%">User Name</td>

              <td width="50%">

                <h:inputText binding="#{backing_Login.inputText1}"

                             id="inputText1"/>

              </td>

            </tr>

            <tr>

              <td width="50%">Password</td>

              <td width="50%">

                <h:inputText binding="#{backing_Login.inputText2}"

                             id="inputText2"/>

              </td>

            </tr>

            <tr>

              <td width="50%"> </td>

              <td width="50%">

                <h:commandButton value="commandButton1"

                                 binding="#{backing_Login.commandButton1}"

                                 id="commandButton1"

                                 action="#{backing_Login.commandButton1_action}"/>

              </td>

            </tr>

          </table>

        </h:form></body>

    </html>

  </f:view>

  </jsp:root>

Since the file uses the XHTML format, the root tag is jsp:root indicating that XHTML has to be the output. Next each component is bound with its reference in the managed bean using the binding attribute. Finally the commandButton Component is bound to the Listener using the action attribute.

Now let's see the managed bean for Login.jspx.

package view.backing;

import javax.faces.component.html.HtmlCommandButton;

import javax.faces.component.html.HtmlForm;

import javax.faces.component.html.HtmlInputText;

public class Login {

    private HtmlForm form1;

    private HtmlInputText inputText1;

    private HtmlInputText inputText2;

    private HtmlCommandButton commandButton1;

    public void setForm1(HtmlForm form1) {

        this.form1 = form1;

    }

    public HtmlForm getForm1() {

        return form1;

    }

    public void setInputText1(HtmlInputText inputText1) {

        this.inputText1 = inputText1;

    }

    public HtmlInputText getInputText1() {

        return inputText1;

    }

    public void setInputText2(HtmlInputText inputText2) {

        this.inputText2 = inputText2;

    }

    public HtmlInputText getInputText2() {

        return inputText2;

    }

    public void setCommandButton1(HtmlCommandButton commandButton1) {

        this.commandButton1 = commandButton1;

    }

    public HtmlCommandButton getCommandButton1() {

        return commandButton1;

    }

    public String commandButton1_action() {

        String user=(String)inputText1.getValue();

        String pass=(String)inputText2.getValue();

       

        if(user.equalsIgnoreCase("test")&&pass.equalsIgnoreCase("test123"))

        {

            return "success";

        }

        else

        {

           return "failure";   

        }

    }

}

The commandButton1_action() contains the logic to be executed when the submit button is clicked. All others are getters and setters for the UI Components. In essence the managed bean is a JavaBean.

Next is the Welcome.jspx. It just contains a "Welcome" message.

<?xml version='1.0' encoding='windows-1252'?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core">

  <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=windows-1252"/>

  <f:view>

    <html>

      <head>

        <meta http-equiv="Content-Type"

              content="text/html; charset=windows-1252"/>

        <title>welcome</title>

      </head>

      <body>Welcome</body>

    </html>

  </f:view>

</jsp:root>

And last but not least: struts-config.xml. It contains the managed bean's configuration and the navigation cases.

<?xml version="1.0" encoding="windows-1252"?>

<!DOCTYPE faces-config PUBLIC

  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"

  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config xmlns="http://java.sun.com/JSF/Configuration">

  <managed-bean>

    <managed-bean-name>backing_untitled1</managed-bean-name>

    <managed-bean-class>view.backing.Untitled1</managed-bean-class>

    <managed-bean-scope>request</managed-bean-scope>

  </managed-bean>

  <managed-bean>

    <managed-bean-name>backing_welcome</managed-bean-name>

    <managed-bean-class>view.backing.Welcome</managed-bean-class>

    <managed-bean-scope>request</managed-bean-scope>

  </managed-bean>

  <navigation-rule>

    <from-view-id>/untitled1.jspx</from-view-id>

    <navigation-case>

      <from-outcome>success</from-outcome>

      <to-view-id>/welcome.jspx</to-view-id>

    </navigation-case>

    <navigation-case>

      <from-outcome>failure</from-outcome>

      <to-view-id>/untitled1.jspx</to-view-id>

    </navigation-case>

  </navigation-rule>

</faces-config>

That brings us to the end of this discussion. What I have discussed is just the tip of the iceberg known as JSF. There are certain pieces that I have not explained completely, especially the UI Components. These pieces will be the focus of the next discussion. Till then...


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials