Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{scrollbar}

This

...

application

...

will

...

make

...

you

...

understand

...

how

...

Model(M),

...

View(V),

...

Controller(C)

...

architecture

...

is

...

implemented

...

in

...

Java

...

Server

...

Faces.

...

This

...

application

...

will

...

make

...

use

...

of

...

UI

...

components,

...

Validator,

...

Navigation

...

and

...

Bean

...

component

...

available

...

with

...

JSF.

...

Briefly

...

describing

...

the

...

application,

...

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

...

  • 5.0+

...

  • (J2SE

...

  • 1.5)

...

  • Eclipse

...

  • 3.3.1.1

...

  • (Eclipse

...

  • Classic

...

  • package

...

  • of

...

  • Europa

...

  • distribution),

...

  • which

...

  • is

...

  • platform

...

  • specific
  • Web Tools Platform (WTP)

...

  • 2.0.1

...

  • Data

...

  • Tools

...

  • Platform

...

  • (DTP)

...

  • 1.5.1

...

  • Eclipse

...

  • Modeling

...

  • Framework

...

  • (EMF)

...

  • 2.3.1

...

  • Graphical

...

  • Editing

...

  • Framework

...

  • (GEF)

...

  • 3.3.1

...

Details

...

on

...

installing

...

eclipse

...

are

...

provided

...

in

...

the

...

Development

...

environment

...

section.

...

This

...

tutorial

...

is

...

organized

...

in

...

the

...

following

...

sections:

...

The

...

application

...

development

...

will

...

take

...

you

...

through

...

the

...

following

...

Table of Contents

...

Once

...

you

...

have

...

all

...

the

...

pre-requisites

...

installed

...

follow

...

the

...

following

...

steps

...

to

...

create

...

a

...

project

...

with

...

Eclipse

...

Setting

...

Eclipse

...

for

...

Application

...

development

...

  1. Launch

...

  1. Eclipse

...

  1. and

...

  1. Create

...

  1. a

...

  1. dynamic

...

  1. web

...

  1. project

...

  1. as

...

  1. shown

...

  1. in

...

  1. the

...

  1. figure


    Image Added


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


    Image Added


  3. Check the box for JavaServerFaces and under the version tab select 1.2 as the version


    Image Added


  4. Once done you can give default values for web module and Geronimo Deployment Plan. On the JSF capabilities window check the box and select new as shown in the figure


    Image Added


  5. The next window suggests to create 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.6.1\commons-beanutils-1.6.1.jar

...

    • <GERONIMO_HOME>\repository\commons-collections\commons-collections\3.1\commons-collections-3.1.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.2\myfaces-api-1.2.2.jar

...

    • <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.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 Eclipse IDE for application development

Define and Implement the application Model(M)

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


    Code Block
    JAVA
    JAVA
    borderStylesolid
    titleFirstName.java
    
    package jsf;
    
    public class FirstName {
    	String username;
    
    	public String getName() {
    		return username;
    	}
    
    	public void setName(String name) {
    		username = name;
    	}
    
    }
    

...


  1. Create a second Bean class LastName and add the following code to the class
    Code Block
    JAVA
    JAVA
    borderStylesolid
    titleLastName.java
    
    package jsf;
    
    public class LastName {
    	String lastname;
    
    	public String getLName() 	{
    		return lastname;
    	}
    
    	public void setLName(String lname)
    	{
    		lastname = lname;
    }
    }
    

...

  1. This

...

  1. completes

...

  1. the

...

  1. Model

...

  1. definition

...

  1. and

...

  1. implementation

...

  1. of

...

  1. bean

...

  1. class

...

  1. .

...

Define

...

and

...

implement

...

Model(M)

...

objects

...

to

...

Controller

...

  1. In

...

  1. an

...

  1. JSF

...

  1. application

...

  1. controller

...

  1. is

...

  1. implemented

...

  1. by

...

  1. a

...

  1. configuration

...

  1. file

...

  1. WebContent/WEB-INF/faces-config.xml.

...

  1. Double

...

  1. Click

...

  1. on

...

  1. the

...

  1. file.

...

  1. This

...

  1. will

...

  1. open

...

  1. a

...

  1. Faces

...

  1. Configuration

...

  1. Editor.


    Image Added


  2. Select the ManagedBean tab in the editor. Choose Managed Bean of scope request and select Add.


    Image Added


  3. Using the 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(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 page login.jsp. Select Finish.


    Image Added


  3. Similarly create another jsp page welcome.jsp.
  4. Moving forward now we now have to include Tag Library Descriptors(TLD) in our application.
    Geronimo comes packaged with the TLD's required for application. The TLD's can be found in
    No Format
    borderStylesolid
    titleLocation of TLD
    
    <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.jar\META-INF\myfaces-html.tld
    and
    <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.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

...

  1. Eclipse

...

  1. Under

...

  1. project

...

  1. Explorer

...

  1. right

...

  1. click

...

  1. on

...

  1. WEB-INF.

...

  1. Create

...

  1. a

...

  1. folder

...

  1. tld.

...

  1. Copy

...

  1. myfaces-html.tld

...

  1. and

...

  1. myfaces_core.tld

...

  1. to

...

  1. this

...

  1. folder.

...


  1. #Next

...

  1. step

...

  1. is

...

  1. to

...

  1. populate

...

  1. login.jsp

...

  1. and

...

  1. welcome.jsp

...

  1. with

...

  1. data

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titlelogin.jsp

...

  1. 
    <%@ taglib uri="/WEB-INF/tld/myfaces-html.tld" prefix="h" %>
    <%@ taglib uri="/WEB-INF/tld/myfaces_core.tld" prefix="f" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Welcome to Apache Geronimo</title>
    </head>
    <body>
    <f:view>
        <h1><h:outputText value="Welcome to Apache Geronimo" /></h1>
        <h:form>
            <h:message for="firstName" style="color: red;" />
            <h:message for="lastName" style="color: red;" />
            <br>
            <h:outputText value="Enter your first name" />
            <br>
            <h:inputText id="firstName" value="#{firstName.name}" required="true">
                <f:validateLength minimum="4" maximum="10" />
            </h:inputText>
            <br>
            <h:outputText value="Enter your last name" />
            <br>
            <h:inputText id="lastName" value="#{lastName.LName}" required="true">
                <f:validateLength minimum="3" maximum="10" />
            </h:inputText>
            <br>
            <h:commandButton id="submit" action="validated" value="Enter" />
        </h:form>
    </f:view>
    </body>
    </html>
    

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titlewelcome.jsp

...

  1. 
    <%@ taglib uri="/WEB-INF/tld/myfaces-html.tld" prefix="h"%>
    <%@ taglib uri="/WEB-INF/tld/myfaces_core.tld" prefix="f"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Welcome</title>
    </head>
    <body>
    <f:view>
    <h3><h:outputText value="You have successfully Logged in" /> <h:outputText value="#{firstName.name} " /> <h:outputText value="#{lastName.LName}" />
    <h:outputText value="!" /></h3>
    </f:view>
    </body>
    </html>
    

...

  1. Lets

...

  1. now

...

  1. try

...

  1. to

...

  1. understand

...

  1. what

...

  1. each

...

  1. line

...

  1. of

...

  1. code

...

  1. represents.

...

  1. The

...

  1. first

...

  1. two

...

  1. lines

...

  1. in

...

  1. login.jsp

...

  1. defines

...

  1. two

...

  1. tag

...

  1. libraries

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titleCode 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" %>
    

...

  1. These

...

  1. two

...

  1. sets

...

  1. of

...

  1. tags

...

  1. are

...

  1. defined

...

  1. by

...

  1. JSF.

...

  1. First

...

  1. one

...

  1. with

...

  1. the

...

  1. namespace

...

  1. "h"

...

  1. is

...

  1. used

...

  1. to

...

  1. generate

...

  1. html

...

  1. views.

...

  1. Second

...

  1. one

...

  1. with

...

  1. the

...

  1. namespace

...

  1. "f"

...

  1. handles

...

  1. the

...

  1. core

...

  1. functionalities

...

  1. of

...

  1. JSF

...

  1. like

...

  1. type

...

  1. conversions,

...

  1. validations

...

  1. and

...

  1. listeners

...

  1. for

...

  1. input

...

  1. from

...

  1. user.

...

  1. The

...

  1. next

...

  1. few

...

  1. lines

...

  1. contains

...

  1. the

...

  1. usual

...

  1. html

...

  1. tags

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titleCode 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>
    

...

  1. <f:view>

...

  1. -

...

  1. This

...

  1. tag

...

  1. represents

...

  1. the

...

  1. start

...

  1. of

...

  1. JSF

...

  1. code.

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titleCode Snippet from login.jsp
    
    <h:inputText id="firstName" value="#{firstName.name}" required="true">
    

...

  1. Represents

...

  1. the

...

  1. input

...

  1. tag.

...

  1. id="firstName"

...

  1. and

...

  1. value="

...

  1. firstName.name

...

  1. "

...


  1. comes

...

  1. from

...

  1. the

...

  1. Managed

...

  1. Bean.

...

  1. Use

...

  1. the

...

  1. Faces

...

  1. Configuration

...

  1. Editor,

...

  1. Select

...

  1. firstName

...

  1. bean

...

  1. under

...

  1. Managed

...

  1. Bean

...

  1. tab.

...

  1. The

...

  1. Managed

...

  1. Bean

...

  1. Name

...

  1. is

...

  1. firstName.

...

  1. See

...

  1. the

...

  1. figure

...

  1. below


    Image Added


    This completes the implementation of View(V)

...

  1. in

...

  1. the

...

  1. application.

...


  1. The

...

  1. other

...

  1. tags

...

  1. <f:validateLength>

...

  1. and

...

  1. <h:commandButton>

...

  1. will

...

  1. be

...

  1. explained

...

  1. in

...

  1. the

...

  1. next

...

  1. section.

...

Define

...

the

...

Validator

...

Component

...

The

...

<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

...

etc.

...

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

...

suggests

...

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

...

for

...

as

...

a

...

command

...

button.

...


The

...

code

...

in

...

the

...

input.jsp

...

<h:commandButton

...

id="submit"

...

action="validated"

...

value="Enter"

...

/>

...

suggests

...

that

...

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

...

is

...

defined

...

by

...

faces-config.xml.

...

Follow

...

the

...

underlined

...

steps

...

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. 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. GUI

...

  1. object,

...

  1. page.


    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 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 and 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 input 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 suggests the Controller that if you all the inputs are valid from a form in the /pages/login.jsp,

...

  1. and

...

  1. the

...

  1. action

...

  1. is

...

  1. 'validated',

...

  1. then

...

  1. go

...

  1. to

...

  1. page

...

  1. /pages/welcome.

...

  1. jsp.


    Image Added


    Now lets add a index.jsp

...

  1. under

...

  1. WEB-INF

...

  1. as

...

  1. follows

...

  1. Code Block
    ActionScript
    ActionScript
    borderStylesolid
    titleindex.jsp

...

  1. 
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
    <\!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <body>
    <jsp:forward page="/pages/login.jsf" />
    </body>
    </html>
    

...

  1. Now

...

  1. what

...

  1. is

...

  1. this

...

  1. login.jsf

...

  1. in

...

  1. the

...

  1. forward

...

  1. path.

...

  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. Next

...

  1. 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

...

Apache

...

Geronimo

...

Server

...

and

...

a

...

Login

...

page

...

will

...

be

...

launched.


Image Added


Lets give some sample inputs
Sample Input1:
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 Input2:
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 Input3:
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