javaserver faces - colorado software summit · pdf filejsf and jsp lifecycle were in conflict...

40
Colorado Software Summit: October 21 – 26, 2007 Gary Murphy – JavaServer Faces Slide 1 © Copyright 2007, Hilbert Computing, Inc. JavaServer Faces Gary Murphy Hilbert Computing, Inc. http://www.hilbertinc.com/ [email protected]

Upload: truongdieu

Post on 09-Mar-2018

221 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 1

© Copyright 2007, Hilbert Computing, Inc.

JavaServer Faces

Gary MurphyHilbert Computing, Inc.

http://www.hilbertinc.com/[email protected]

Page 2: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 2

© Copyright 2007, Hilbert Computing, Inc.

JSF Presentations at CSS

“Extending JSF to Build a Product-

specific UI Framework” - Bryan Basham

“Comparing Java Frameworks” - Matt

Raible

Page 3: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 3

© Copyright 2007, Hilbert Computing, Inc.

What is JSF?

JavaServer Faces is a Java-based user-

interface framework

Creates a model for user interface

components

Manages the state of user interface

components

Creates an event model for notification of

user interface changes, similar to Swing or

SWT

Handles input validation and conversion

Page 4: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 4

© Copyright 2007, Hilbert Computing, Inc.

What is JSF?

JavaServer Faces is (continued)

Handles page navigation

Handles the construction of POJOs via

dependency injection

JSF is the J2EE web development

standard

Page 5: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 5

© Copyright 2007, Hilbert Computing, Inc.

JSF is NOT...

...an application framework

No controller (as in MVC)

Scope is primarily user-interface

Some support for navigation

Some support for POJO construction

...JSPs

The reference implementation uses JSPs

for rendering, but that is not a

requirement

JSF is at a much higher level of abstraction

Page 6: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 6

© Copyright 2007, Hilbert Computing, Inc.

Comparison to Struts

Model-View-Controller

Struts includes an MVC controller

JSF does not have a controller, but

provides a mechanism for page navigation

UI Model

Struts is form-based. That is, it processes

one page at a time.

JSF is component based. Each component

(e.g. button, list box) can have associated

actions and events.

Page 7: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 7

© Copyright 2007, Hilbert Computing, Inc.

Brief History

JSF 1.0 (2004-03-11) under JSR 127

JSF 1.1 (2004-05-27)

Bug fixes

Note two month span between 1.0 and 1.1

JSF 1.2 (2006-05-11) under JSR 252

Support for multi-frame, multi-window

designs

Facelet support

Bug fixes, especially for Portlets, duplicate

press

Page 8: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 8

© Copyright 2007, Hilbert Computing, Inc.

Brief History

JSF 1.2_03 (2006-12-04)

Bug Fixes

JSF 1.2_04 (2007-03-5)

JSF 1.2_05 (2007-10-17)

Page 9: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 9

© Copyright 2007, Hilbert Computing, Inc.

Criticisms

Initial implementation was weak.

Need to distinguish architecture from

implementation.

Architecture is very robust

Initial implementation was very weak

Heavier Memory Requirements

Must manage a component tree

Heavier than Struts where mainly a

wrapper over the POST data is maintained

Page 10: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 10

© Copyright 2007, Hilbert Computing, Inc.

JSF Implementations

Open Source choices exist other than

the reference implementation:

ICEfaces

JBoss RichFaces

MyFaces Tomahawk from Apache

Commercial choices:

Infragistics NetAdvantage

IBM Rational Application Developer

Page 11: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 11

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Models user-interface at the component

level

Supports component-level actions and

events

Similar conceptually to Swing or SWT

Separates the description of a

component from the implementation

Reference implementation uses JSP

Implementations could render WML, HTML,

XHTML, XUL, etc.

Page 12: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 12

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Component Model

Class that implements the tag (e.g.

SpinnerTag)

Class that implements the component

class (e.g. UISpinner):

Maintains state

Renders the implementation of user interface

Processes the user input (e.g. HTTP POST

values)

Page 13: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 13

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Components can delegate rendering

and input processing

Allows for plugging in different UI

technologies such as WML, XUL

Highly recommended for custom

components

Page 14: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 14

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Data Conversion Framework

HTTP POST contains String values

Built-in conversions for Java data types

int, Integer, Date, etc.

Supports custom converters. For

example:

Phone number. Parsing and formatting special

chars

Credit cards. Parsing and formatting blanks

Declared in faces-config.xml

Page 15: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 15

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Data Validation Framework

Similar to data conversion, but can

validate data as well

Page 16: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 16

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Support for internationalization via

resource bundles.<f:loadBundle basename=”com.acme.msgs” var=”msgs” /><f:outputText value=”#{msgs.useridLabel}”

Page 17: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 17

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Support for visual development

environments

Eclipse has a JSF project in development

MyEclipse has a visual development

environment for MyFaces

IBM RAD has a visual development

environment for their JSF implementation

Not open source

Poorly documented

Some components poorly implemented

Page 18: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 18

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Managed beans

Uses the JavaBean specification

Configured in the faces-config.xml

Name

Scope

Class

Automatically factory-constructed with

dependency injection

Name is used in EL in the JSP:

<h:outputText value=”#{user.name}” />

Page 19: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 19

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Backing beans

Java object that contains the UIComponent

objects that are the Java representation of

the components on a given page

Typically generated by a visual

development environment (VDE).

Should be used as a binding between the

component as HTML and the component as

a Java object.

Should not contain business rules

Page 20: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 20

© Copyright 2007, Hilbert Computing, Inc.

Architecture

JSF Lifecycle consists of six phases:

Restore view

Apply request values

Process validations

Update model values

Invoke application (actions and events)

Render response

Applications can get callbacks before

and after each phase by implementing

a phase listener

Page 21: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 21

© Copyright 2007, Hilbert Computing, Inc.

Architecture

JSF includes an event model:

Value change events are attached via the

valueChangeListener attribute of a

component.

These should submit the form via JavaScript

onchange events.

Provides access to the old value and the new value

Action listeners called during normal HTTP

POST via button or command hyperlink

Page 22: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 22

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Action Events

These are generated via submit buttons or

command links.

These action events are bound to any no-

arg method on any Java object in any

scope.

This should be bound to an explicit controller

method, but there is no controller as a part of

JSF.

This is the primary way in which

information gets from the browser to the

application

Page 23: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 23

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Navigation

Actions on user interface components run

code that returns a String that is an

outcome name. (Similar to Struts)

The string is matched to an outcome in the

Faces configuration along with the current

view ID (i.e. JSP)

Also can map to global outcomes, which

map a logical name to a JSP. (More like

Struts).

Page 24: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 24

© Copyright 2007, Hilbert Computing, Inc.

Architecture

Navigation

An invalid or null outcome will stay on the

same page

I will show an example of Faces

config...

Page 25: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 25

© Copyright 2007, Hilbert Computing, Inc.

Facelets

Introduced in JSF 1.2

Provides templating

Does not use JSPs

JSF and JSP lifecycle were in conflict at

times.

Uses the new EL-API that will be in JSP 2.1

without requiring JSP 2.1

Eliminates view and subView

Compensated for JSP binding issues

Page 26: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 26

© Copyright 2007, Hilbert Computing, Inc.

Configuring Facelets

jsf-facelets.jar must be in

classpath

faces-config.xml override view

handler:

<faces-config> <application> <view-handler>

com.sun.facelets.FaceletViewHandler</view-handler>

</application></faces-config>

Page 27: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 27

© Copyright 2007, Hilbert Computing, Inc.

Configuring Facelets

web.xml - Change file type for defining

views from JSPs (*.jsp) to XHTML:

<context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value></context-param>

Page 28: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 28

© Copyright 2007, Hilbert Computing, Inc.

Facelet Aliasing

You can specify references to JSF

components in the facelet much like a

JSP.

If HTML comes from a design group,

you can alias

<input type="text" jsfc="h:inputText" value="#{credentials.userid}"/>

Page 29: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 29

© Copyright 2007, Hilbert Computing, Inc.

Facelets Reference

http://www.ibm.com/developerworks/java/library/j-facelets/

Page 30: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 30

© Copyright 2007, Hilbert Computing, Inc.

Let's Take a Look

Code with Facelets and JSF 1.2 RI

Page 31: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 31

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Overcoming some diagnostic

deficiencies

Reference implementation has verbose

debug logging using commons-logging

Recommend a phase listener

Page 32: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 32

© Copyright 2007, Hilbert Computing, Inc.

Techniques

JSF is supportive of MVC, but doesn't

provide a controller implementation.

You should implement an explicit controller

You can (but shouldn't) implement implicit

controllers, which is what most JSF

examples show.

Page 33: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 33

© Copyright 2007, Hilbert Computing, Inc.

Techniques

The controller should:

Provide a navigation outcome

Become the anchor point for non-

functional requirements:

Logging, authorization, performance metrics,

serialization checking, etc.

Page 34: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 34

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Use JSP includes appropriately as you

would in a Struts or plain JSP

application. (Better to use facelets):

Headers

Footers

Navigation bars

Groups of controls for which you don't

want to build a custom component

Use the <f:view> tag to avoid rendering

multiple <body> tags.

Page 35: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 35

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Backing bean vs. Updating state on

view objects.

Backing beans are typically generated by

visual development environments (VDEs)

VDEs may update the backing bean with

business rules entered in the VDE.

My recommendation is to not use them

Page 36: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 36

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Favor global navigation outcomes over

view-to-view navigations:

This pushes navigation into your controller

implementation

Avoids the combinatorial explosion

problem when supporting multiple

destinations

Page 37: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 37

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Strive for 100% JSF tags in the JSP.

Mixing JSF tags and other JSP tags, like

JSTL, cause problems because both write

to the response stream.

Don't use tables – use DataTable JSF

objects instead.

Wrap HTML in <f:verbatim> tags.

Almost no HTML is needed.

<script> tags, <link> to CSS only, <meta>, <title>

Note some VDEs drop HTML into the page

Page 38: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 38

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Push styling into CSS

All JSF components have references to one

or more style sheets

Some JSF components directly support

HTML styling, such as width, but I tend to

stay away from those so that look-and-feel

is external to the application.

That's a recommendation for non-JSF

web applications as well.

See http://www.csszengarden.com/

Page 39: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 39

© Copyright 2007, Hilbert Computing, Inc.

Techniques

Most web applications involve some

DHTML.

These aren't JSF-specific, but are worth

mentioning:

Firefox with the FireBug plugin is useful for

debugging JavaScript

JSF confuses FireBug because the URL

doesn't change with the view change.

Page 40: JavaServer Faces - Colorado Software Summit  · PDF fileJSF and JSP lifecycle were in conflict at ... debug logging using commons-logging ... JavaServer Faces Slide 37

Colorado Software Summit: October 21 – 26, 2007

Gary Murphy – JavaServer Faces Slide 40

© Copyright 2007, Hilbert Computing, Inc.

Discussion

Any questions?

Comments or advice from your

experiences?

Comparison with Struts or other web

frameworks.

Thank you for taking the time to

learn about JSF.