Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JSF
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

JSF Forms - Developing form based application

                          

Complete Java Server Faces (JSF) Tutorial - JSF Tutorials. JSF Tutorials at Rose India covers everything you need to know about JSF.

Developing Forms in JSF 1.2
In this tutorial we will show you how to develop UI forms using JSF 1.2.  For this tutorial we will use the project developed in the last section Installing JSF 1.2 on tomcat. After completing this tutorial you will be able to develop user entry forms in JSF 1.2 technology.

About JSF Forms Application

The application we are going to develop displays a form to the user asking user to enter the name. When user enters the name and clicks on the "OK" button, welcome message "Welcome <user> to JSF 1.2 World!" is displayed to the user in new screen.

Creating web application

Download the application created in the Installing JSF 1.2 on tomcat section and rename this application to "jsf12forms" and deploy on the Tomcat 6.0 server. 

Steps to develop the Application

We can divide the process into following steps:

  1. Create user interface components (JSP files)
  2. Create manage bean
  3. Write navigation rules
  4. Compile the application
  5. Deploy and test the application

Let's follow the steps to develop the application.

  1. Create UI Screens (JSP files)
    In this application we are going to develop two pages inputname.jsp and welcome.jsp. The inputname.jsp prompts the user to enter his/her name. Code of inputname.jsp is:
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    <html>
    <head>
    <title>enter your name page</title>
    </head>
    <body>
    <f:view>
    <h1>
    <h:outputText value="JSF 1.2 Tutorial"/>
    </h1>
      <h:form id="UserEntryForm">
        <h:outputText value="Enter Your Name:"/>
        <h:inputText value="#{UserBean.userName}" />
        <h:commandButton action="welcome" value="OK" />
       </h:form>

    </f:view>
    </body>
    </html> 

    Save the above file in "webapps\jsf12forms\user" directory. 

    Now let's examine the code of inputname.jsp. The first two lines of the actually are the directives which specify the location to find the JSF tags that defines HTML elements and core JSF tags respectively.

    JSF HTML Tags: <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   
    JSF Core Tags: <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    The tag
    <h:outputText value="JSF 1.2 Tutorial"/> simply generates  "JSF 1.2 Tutorial" text that is displayed on the user screen.

    The code that generates forms and buttons:
    1 <h:form id="UserEntryForm">
    2    <h:outputText value="Enter Your Name:"/>
    3    <h:inputText value="#{UserBean.userName}" />
    4    <h:commandButton action="welcome" value="OK" />
    5   </h:form>

    Line 1: Generates HTML code for the form.
    Line 2: Prints the message "Enter Your Name:" on the screen.
    Line 3: Creates HTML text input element, where user can enter his/her name. The attribute
    value="#{UserBean.userName}", actually bind this filed with the managed  beans property "userName".
    Line 4: Creates HTML submit button with the text "OK" on it.
    Line 5: Creates HTML forms end tag </form>


    Next file is welcome.jsp, which displays the welcome message to the user. The  welcome.jsp contains following code:

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <html>
    <head>
    <title>Welcome</title>
    </head> 
    <body>
    <f:view>
    <h3>
    <h:outputText value="Welcome" />,
    <h:outputText value="#{UserBean.userName}" /> to JSF 1.2 World!

    </h3>
    </f:view>
    </body> 
    </html> 

    Save above file in "webapps\jsf12forms\user" directory. Above file displays welcome message to user.
    The code
    <h:outputText value="#{UserBean.userName}" /> to JSF 1.2 World! is used to display the message on the welcome page.
     

  2. Create managed Bean
    Now we will create a bean that will hold the form data (user name in this case) entered by user. This bean is also know as backing bean or JSF managed bean. The JSF managed bean is a regular JavaBeans components whose property and methods are used by JSF components.

    Here is the code of our Managed Bean (
    UserNameBean.java):
    package net.roseindia;

    public class UserNameBean {

    String userName;

    /**
    * @return User Name
    */
    public String getUserName() {
    return userName;
    }

    /**
    * @param User Name
    */
    public void setUserName(String name) {
    userName = name;
    }
    }

    Save UserNameBean.java into "webapps\jsf12forms\WEB-INF\src\java\net\roseindia" directory.
    We can define the BackingBean in the "faces-config.xml" using the following code:
    <managed-bean>
    <managed-bean-name>UserBean</managed-bean-name>
    <managed-bean-class>net.roseindia.UserNameBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
       

  3. Writing the navigation rule
    Open the faces-config.xml and add the following code to define the navigation rule:
    <navigation-rule>
    <from-view-id>/user/inputname.jsp</from-view-id>
    <navigation-case>
    <from-outcome>welcome</from-outcome>
    <to-view-id>/user/welcome.jsp</to-view-id>
    </navigation-case>
    </navigation-rule>

    The file version of faces-config.xml is:
    <?xml version='1.0' encoding='UTF-8'?>

    <!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>

    <navigation-rule>
    <from-view-id>/user/inputname.jsp</from-view-id>
    <navigation-case>
    <from-outcome>welcome</from-outcome>
    <to-view-id>/user/welcome.jsp</to-view-id>
    </navigation-case>
    </navigation-rule>
    <managed-bean>
    <managed-bean-name>UserBean</managed-bean-name>
    <managed-bean-class>net.roseindia.UserNameBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

    </faces-config>

  4. Compiling the application
    Now we are at the final stage of the development. We will use ant tool to compile the application. So, you download the final version of the application that contains the build.xml file and directories necessary to build the application. Download the full application now.

    To compile the application go to "apache-tomcat-6.0.13\webapps\jsf12forms\WEB-INF\src" directory and issue ant command.
    C:\>cd C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12forms\WEB-INF\src

    C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12forms\WEB-INF\src>ant
    Buildfile: build.xml

    clean:
    [delete] Deleting directory C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12form
    s\WEB-INF\classes
    [mkdir] Created dir: C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12forms\WEB-
    INF\classes

    prepare:

    resources:

    compile:
    [javac] Compiling 1 source file to C:\testjsf\apache-tomcat-6.0.13\webapps\j
    sf12forms\WEB-INF\src\classes
    [jar] Building jar: C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12forms\WEB
    -INF\lib\jsf12tutorial.jar

    project:

    all:

    BUILD SUCCESSFUL
    Total time: 10 seconds
    C:\testjsf\apache-tomcat-6.0.13\webapps\jsf12forms\WEB-INF\src>

    To test the application run tomcat and type http://localhost:8080/jsf12forms/user/inputname.jsf. You browser should display the Input screen. Enter your name and press ok:


    The welcome screen should look like:

Download the full application discussed in this section

                          

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (post your own) View All Comments Latest 10 Comments:

Hi I followed the above steps but I could not submit the inputname.jsp page.

Please help me what I am missing.

Thanks
Raj

Posted by Raj on Thursday, 05.1.08 @ 23:48pm | #58326

Save UserNameBean.java into "webapps\jsf12forms\WEB-INF\src\net\roseindia" directory.

Posted by neelanjan on Friday, 03.28.08 @ 12:23pm | #54562

Hi preetham ,

I have seem your problem and this happened probably because you forgot to put in the FacesConfig the UserBean:

<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>net.roseindia.UserNameBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>

If you put, double check the case sensitive

Check if you didn't do it:

Faces Config:
<managed-bean-name>UserBean</managed-bean-name>

JSP:
<h:outputText value="#{userBean.userName}" /> to JSF 1.2 World!




Take a look in the letter "U"... it must be lower-case in both cases or upper-case in both cases

Posted by Welysson on Friday, 02.22.08 @ 18:31pm | #49425

the tutorial is ok.

I recomend u to check ur code.

XD.

Posted by Javiero on Monday, 10.29.07 @ 09:26am | #35040

i follwed the steps given ,but it is showing the
"javax.servlet.ServletException: /user/inputname.jsp(15,4) '#{UserNameBean.userName}' Target Unreachable, identifier 'UserNameBean' resolved to null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
"
when clicking on the OK Button.
need help?

thanks

Posted by preetham on Monday, 08.20.07 @ 11:35am | #23761

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.