Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

This

...

application

...

will

...

make

...

you

...

understand

...

how

...

Model (M),

...

View (V),

...

Controller (C)

...

architecture

...

is

...

implemented

...

in

...

JavaServer Faces.

...

This

...

application

...

will

...

make

...

use

...

of

...

UI

...

components,

...

Validator,

...

Navigation

...

and

...

Bean

...

component

...

available

...

with

...

JSF.

...

This

...

application will take a user First Name and Last Name. Later these fields will be validated by JSF and using the controller bean and Navigation rule the output will be displayed. This application will also introduce a UI component which is a submit button.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software:

  • Sun JDK 6.0+ (J2SE 1.6)
  • Eclipse IDE for Java EE Developers, which is platform specific
  • Apache Geronimo Eclipse Plugin 2.1.x
  • Apache Geronimo Server 2.1.x

    Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

...

Once you have all the prerequisites installed, follow the following steps to create a project with Eclipse.

Setting Eclipse for application development

  1. Launch Eclipse and create a dynamic Web project as shown in the figure.
    Image Added


  2. Give the fields for the Web Project as shown in the following figure.
    Image Added


  3. Select Finish.
  4. Right click on the SimpleJSF project and select Properties, then select Project Facets.
  5. Check the box for JavaServerFaces and under the Version tab select 1.2 as the version. Select the (error) Further configuration required... indicator to display the JSF Capabilities pane.
    Image Added


  6. On the JSF Capabilities window check the box and select new as shown in the figure.
    Image Added


  7. The next window is used to create a JSF Implementation library. Give the library name as JSFCustomLibrary and add the following jars. Select Finish once done. See the figure below:
    • <GERONIMO_HOME>\repository\commons-beanutils\commons-beanutils\1.

...

    • 7.

...

    • 0\commons-beanutils-1.

...

    • 7.

...

    • 0.jar

...

    • <GERONIMO_HOME>\repository\commons-collections\commons-collections\3.

...

    • 2\commons-collections-3.

...

    • 2.jar

...

    • <GERONIMO_HOME>\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar

...

    • <GERONIMO_HOME>\repository\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar

...

    • <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-api\1.2.

...

    • 3\myfaces-api-1.2.

...

    • 3.jar

...

    • <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.

...

    • 3\myfaces-impl-1.2.

...

    • 3.jar


      Image Added


  1. Check Deploy and modify the URL pattern to *.jsf as shown in the figure. Select Finish.
    Image Added


    This finishes the setting up of the Eclipse IDE for application development.

Define and Implement the application Model (M)

The Model as suggested by MVC architecture handles data and logic of the application. In an enterprise application. Java Beans are used to represent collection of data and operation on that data. In JSF we use Java Beans to define the Model.

  1. Under the project explorer right click on the SimpleJSF project and create a new class.
    Image Added


  2. Fill the New Java Class form with jsf as the package name and FirstName as the bean class name. Select Finish once done.

    Image Added


  3. Add the following code to the FirstName bean class:
    JAVAsolidFirstName.java package jsf; public class FirstName { String username; public String getName() { return username; } public void setName(String name) { username = name; } }
  4. Create a second Bean class LastName and add the following code to the class: JAVAsolidLastName.java package jsf; public class LastName { String lastname; public String getLName() { return lastname; } public void setLName(String lname) { lastname = lname; } } This completes the Model definition and implementation of the bean classes.

Define and implement Model (M) objects to Controller

  1. In a JSF application the Controller is implemented by a configuration file called WebContent/WEB-INF/faces-config.xml.

...

  1. Double

...

  1. click on

...

  1. the

...

  1. file.

...

  1. This

...

  1. will

...

  1. open

...

  1. the Faces

...

  1. Configuration

...

  1. Editor.

...


  1. Image Added


  2. Select the ManagedBean tab in the editor. Select the request Managed Bean Element and select Add.

    Image Added


  3. Choose the Using an existing Java class option, select Browse. Give the search element as FirstName and select OK.

    Image Added


  4. Select Finish on the next window. Similarly add the other bean LastName. Now select the Source tab in the Faces configuration Editor. It displays the bean components (i.e., the Model) in the controller.

    Image Added


    This completes the description of Model to Controller.

Define and implement View (V) in application

  1. Right click on WebContent and create a new folder with the name pages.
    Image Added


  2. Right click on pages folder and create a JSP called login.jsp. Select Finish.
    Image Added


  3. Similarly create another JSP page called welcome.jsp.

  4. Now we have to include the Tag Library Descriptors (TLD) in our application. Geronimo comes packaged with the required TLD's, which can be found in:
    solidLocation of TLD <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.

...

  1. 3\myfaces-impl-1.2.

...

  1. 3.jar\META-INF\myfaces-html.tld

...

  1. and

...

  1. <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.

...

  1. 3\myfaces-impl-1.2.

...

  1. 3.jar\META-INF\myfaces_core.tld

...


  1. To

...

  1. add

...

  1. these

...

  1. two

...

  1. TLD's

...

  1. in

...

  1. the

...

  1. application,

...

  1. in Eclipse under the Project Explorer right click on WEB-INF.

...

  1. Create

...

  1. a

...

  1. folder

...

  1. called tld.

...

  1. Copy

...

  1. myfaces-html.tld

...

  1. and

...

  1. myfaces_core.tld

...

  1. to

...

  1. this

...

  1. folder.

...



  1. The next step is to populate login.jsp

...

  1. and

...

  1. welcome.jsp

...

  1. with

...

  1. data ActionScriptsolidlogin.jsp

...

  1. <%@

...

  1. taglib

...

  1. uri="/WEB-INF/tld/myfaces-html.tld"

...

  1. prefix="h"

...

  1. %>

...

  1. <%@

...

  1. taglib

...

  1. uri="/WEB-INF/tld/myfaces_core.tld"

...

  1. prefix="f"

...

  1. %>

...

  1. <html>

...

  1. <head>

...

  1. <meta

...

  1. http-equiv="Content-Type"

...

  1. content="text/html;

...

  1. charset=ISO-8859-1">

...

  1. <title>Welcome

...

  1. to

...

  1. Apache

...

  1. Geronimo</title>

...

  1. </head>

...

  1. <body>

...

  1. <f:view>

...

  1. <h1><h:outputText

...

  1. value="Welcome

...

  1. to

...

  1. Apache

...

  1. Geronimo"

...

  1. /></h1>

...

  1. <h:form>

...

  1. <h:message

...

  1. for="firstName"

...

  1. style="color:

...

  1. red;"

...

  1. />

...

  1. <h:message

...

  1. for="lastName"

...

  1. style="color:

...

  1. red;"

...

  1. />

...

  1. <br>

...

  1. <h:outputText

...

  1. value="Enter

...

  1. your

...

  1. first

...

  1. name"

...

  1. />

...

  1. <br>

...

  1. <h:inputText

...

  1. id="firstName"

...

  1. value="#{firstName.name}"

...

  1. required="true">

...

  1. <f:validateLength

...

  1. minimum="4"

...

  1. maximum="10"

...

  1. />

...

  1. </h:inputText>

...

  1. <br>

...

  1. <h:outputText

...

  1. value="Enter

...

  1. your

...

  1. last

...

  1. name"

...

  1. />

...

  1. <br>

...

  1. <h:inputText

...

  1. id="lastName"

...

  1. value="#{lastName.LName}"

...

  1. required="true"

...

  1. > <f:validateLength

...

  1. minimum="3"

...

  1. maximum="10"

...

  1. />

...

  1. </h:inputText>

...

  1. <br>

...

  1. <h:commandButton

...

  1. id="submit"

...

  1. action="validated"

...

  1. value="Enter"

...

  1. />

...

  1. </h:form>

...

  1. </f:view>

...

  1. </body>

...

  1. </html>

...

  1. ActionScript

...

  1. solid

...

  1. welcome.jsp

...

  1. <%@

...

  1. taglib

...

  1. uri="/WEB-INF/tld/myfaces-html.tld"

...

  1. prefix="h"%>

...

  1. <%@

...

  1. taglib

...

  1. uri="/WEB-INF/tld/myfaces_core.tld"

...

  1. prefix="f"%>

...

  1. <%@

...

  1. page

...

  1. language="java"

...

  1. contentType="text/html;

...

  1. charset=ISO-8859-1"

...

  1. pageEncoding="ISO-8859-1"%>

...

  1. <!DOCTYPE

...

  1. html

...

  1. PUBLIC

...

  1. "-//W3C//DTD

...

  1. HTML

...

  1. 4.01

...

  1. Transitional//EN"

...

  1. "http://www.w3.org/TR/html4/loose.dtd">

...

  1. <html>

...

  1. <head>

...

  1. <meta

...

  1. http-equiv="Content-Type"

...

  1. content="text/html;

...

  1. charset=ISO-8859-1">

...

  1. <title>Welcome</title>

...

  1. </head>

...

  1. <body>

...

  1. <f:view>

...

  1. <h3><h:outputText

...

  1. value="You

...

  1. have

...

  1. successfully

...

  1. logged in: "

...

  1. />

...

  1. <h:outputText

...

  1. value="#{firstName.name}

...

  1. "

...

  1. />

...

  1. <h:outputText

...

  1. value="#{lastName.LName}"

...

  1. />

...

  1. <h:outputText

...

  1. value="!"

...

  1. /></h3>

...

  1. </f:view>

...

  1. </body>

...

  1. </html>

...

  1. Lets

...

  1. now

...

  1. try

...

  1. to

...

  1. understand

...

  1. what

...

  1. each

...

  1. line

...

  1. of

...

  1. code

...

  1. represents.

...

    • The

...

    • first

...

    • two

...

    • lines

...

    • in

...

    • login.jsp

...

    • defines

...

    • two

...

    • tag

...

    • libraries

...

    • ActionScript

...

    • solid

...

    • Code

...

    • Snippet

...

    • from

...

    • login.jsp

...

    • <%@

...

    • taglib

...

    • uri="/WEB-INF/tld/myfaces-html.tld"

...

    • prefix="h"

...

    • %>

...

    • and

...

    • <%@

...

    • taglib

...

    • uri="/WEB-INF/tld/myfaces_core.tld"

...

    • prefix="f"

...

    • %>

...

    • These

...

    • two

...

    • sets

...

    • of

...

    • tags

...

    • are

...

    • defined

...

    • by

...

    • JSF.

...

    • The first one

...

    • with

...

    • the

...

    • namespace

...

    • "h"

...

    • is

...

    • used

...

    • to

...

    • generate

...

    • html

...

    • views.

...

    • The second one

...

    • with

...

    • the

...

    • namespace

...

    • "f"

...

    • handles

...

    • the

...

    • core

...

    • functionalities

...

    • of

...

    • JSF

...

    • like

...

    • type

...

    • conversions,

...

    • validations

...

    • and

...

    • listeners

...

    • for

...

    • input

...

    • from

...

    • user.

...

    • The

...

    • next

...

    • few

...

    • lines

...

    • contains

...

    • the

...

    • usual

...

    • html

...

    • tags

...

    • ActionScript

...

    • solid

...

    • Code

...

    • Snippet

...

    • from

...

    • login.jsp

...

    • <html>

...

    • <head>

...

    • <meta

...

    • http-equiv="Content-Type"

...

    • content="text/html;

...

    • charset=ISO-8859-1">

...

    • <title>Welcome

...

    • to

...

    • Apache

...

    • Geronimo</title>

...

    • </head>

...

    • <body>
    • The tag <f:view>

...

    • represents

...

    • the

...

    • start

...

    • of

...

    • JSF

...

    • code.

...

    • This line of code Represents the input tag. The id="firstName"

...

    • and value="

...

    • firstName.name

...

    • " comes from the Managed Bean. ActionScriptsolidCode Snippet from login.jsp <h:inputText id="firstName"

...

    • value="#{firstName.name}

...

    • " required="true">
  1. Using the Faces Configuration Editor, select firstName bean under Managed Bean tab. The Managed Bean Name is firstName. See the figure below.

    Image Added

    This completes the implementation of View (V) in the application. The other tags <f:validateLength> and <h:commandButton> will be explained in the next section.

Define the Validator Component

The code <f:validateLength minimum="4" maximum="10"/>

...

defines

...

the

...

input

...

text

...

length

...

to

...

be

...

minimum

...

of

...

4

...

characters

...

and

...

maximum

...

of

...

10

...

characters.

...

This

...

is

...

the

...

standard

...

validation

...

provided

...

by

...

core

...

tag

...

libraries.

...

Other

...

examples

...

of

...

validators

...

are

...

Validate

...

Long

...

Range

...

tag,

...

Validate

...

Double

...

Range tag , and so on. JSF also provides a Validator interface which can be implemented to create custom validators.

The code <h:message for=""

...

style="color:

...

red;"/>

...

defines

...

the

...

error

...

message.

...

When

...

the

...

user

...

inputs

...

the

...

controller

...

validates each

...

of

...

the

...

inputs.

...

If

...

the

...

inputs

...

are

...

invalid

...

Controller

...

displays

...

the

...

same

...

page

...

again

...

with

...

an

...

error

...

message

...

for

...

the

...

errors.

...

The

...

color:red

...

suggests

...

that

...

the

...

error

...

message

...

will

...

be

...

displayed

...

in

...

red

...

color.

...

Define

...

and

...

implement

...

the

...

View

...

navigation

...

by

...

Controller (C)

...

This

...

step

...

uses the

...

JSP

...

page

...

navigation

...

in

...

the

...

order

...

of

...

user

...

inputs

...

and

...

validation

...

by

...

controller.

...

If

...

all

...

the

...

inputs

...

are

...

valid

...

than

...

the

...

controller

...

performs

...

the

...

action

...

as

...

suggested

...

by

...

the

...

HTML

...

form.

...

This

...

action

...

is

...

submitted

...

by

...

the

...

HTML

...

form as

...

a

...

command

...

button.

...


The

...

code

...

in

...

the

...

input.jsp

...

<h:commandButton

...

id="submit"

...

action="validated"

...

value="Enter"

...

/>

...

checks to determine if all the inputs are valid. This is the button which submits the form to controller if all inputs are valid. In this case the commandButton tells the controller to execute the validated action if all the inputs are valid.
The pages navigation in a JSF applicaiton is defined by faces-config.xml.

...

Follow

...

the

...

steps

...

before to

...

define

...

the

...

pages

...

navigation.

...


  1. Launch

...

  1. the

...

  1. Faces

...

  1. Configuration

...

  1. Editor

...

  1. by

...

  1. double

...

  1. clicking

...

  1. on

...

  1. faces-config.xml

...



  1. Select

...

  1. the

...

  1. Navigation

...

  1. Rule tab

...

  1. in

...

  1. the

...

  1. Configuration

...

  1. Editor.

...

  1. Under

...

  1. the

...

  1. Palette

...

  1. window

...

  1. select

...

  1. Page.

...

  1. This

...

  1. will

...

  1. select

...

  1. a

...

  1. PageFlow Page GUI

...

  1. object

...

  1. .
    Image Added


  2. Drag the mouse over the Navigation Rule Window and click on the window. This will give a Select JSP File window. Select the login.jsp as shown in the figure and select OK.

    Image Added


  3. Similarly add the welcome.jsp page on the Navigation Rule window. See the figure below:

    Image Added


  4. Select Link from the Palette window and join the two pages as shown in the figure:

    Image Added


  5. Select the link between the two pages and go to properties view and set the value for From Outcome field as validated. This is because of the tag <h:commandButton id="submit" action="validated" value="Enter" />. Once all the inputs are valid the action taken is validated. See the figure.
    Image Added


  6. Once done have a look the Source tab in the Faces Navigation Editor. A <navigation-rule> tag has been introduced into the faces-config.xml. This rule instructs the Controller that if all the inputs are valid from a form in the /pages/login.jsp,

...

  1. and

...

  1. the

...

  1. action

...

  1. is

...

  1. validated

...

  1. ,

...

  1. then

...

  1. go

...

  1. to

...

  1. page

...

  1. /pages/welcome.jsp.

...



  1. Image Added


  2. Now lets add a index.jsp

...

  1. under

...

  1. WebContent as

...

  1. follows

...

  1. : ActionScript

...

  1. solid

...

  1. index.jsp

...

  1. <%@

...

  1. page

...

  1. language="java"

...

  1. contentType="text/html;

...

  1. charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>

...

  1. <\!DOCTYPE

...

  1. html

...

  1. PUBLIC

...

  1. "-//W3C//DTD

...

  1. HTML

...

  1. 4.01

...

  1. Transitional//EN"

...

  1. "http://www.w3.org/TR/html4/loose.dtd">

...

  1. <html>

...

  1. <body>

...

  1. <jsp:forward

...

  1. page="/pages/login.jsf"

...

  1. />

...

  1. </body>

...

  1. </html>

...

  1. What is the login.jsf

...

  1. in

...

  1. the

...

  1. forward

...

  1. pathtag.

...

  1. If

...

  1. you

...

  1. look

...

  1. at

...

  1. the

...

  1. web.xml

...

  1. , *.jsf

...

  1. is

...

  1. used

...

  1. as

...

  1. the

...

  1. URL

...

  1. pattern

...

  1. to

...

  1. suggest

...

  1. that

...

  1. forwarded

...

  1. page

...

  1. be

...

  1. taken

...

  1. care

...

  1. by

...

  1. Java

...

  1. Server

...

  1. Faces

...

  1. Servlet.

...


  1. This

...

  1. completes

...

  1. the

...

  1. Application

...

  1. Development

...

  1. process.

...

  1. The next step

...

  1. is

...

  1. to

...

  1. deploy

...

  1. and

...

  1. test

...

  1. the

...

  1. application.

...

Deploy

...

and

...

Test

...

the

...

application

...

Right click on the project SimpleJSF and select Run As ->

...

Run

...

On

...

Server.

...

This

...

will

...

deploy

...

the

...

sample

...

on

...

the Apache

...

Geronimo

...

Server

...

and

...

a

...

Login

...

page

...

will

...

be

...

launched.

...


Image Added


Lets give some sample inputs:
Sample Input #1:
First Name: Mickey
Last Name: Mouse
Both the First Name as well as Last Name fulfills the validation rules, so this form will be submitted to controller and according to the navigation rule controller will launch a welcome.jsp page.
Image Added


Sample Input #2:
First Name: Mic
Last Name: Mouse
First Name should be minimum of length=4 but in this case First Name is of length=3. In this case validation will fail and an error message will be generated by controller for First Name field.
Image Added


Sample Input #3:
First Name: Mickey
Last Name: Mo
Last Name should be minimum of length=3 but in this case Last Name is of length=2. In this case validation will fail and an error message will be generated by controller for Last Name field.
Image Added