Professional Documents
Culture Documents
JSF 2 0: Introduction JSF 2.0: Introduction and Overview: For Live Training On JSF 2.x, Please See THTT// LT
JSF 2 0: Introduction JSF 2.0: Introduction and Overview: For Live Training On JSF 2.x, Please See THTT// LT
JSF 2.0:
2 0: Introduction
and Overview
Originals of Slides and Source Code for Examples:
http://www.coreservlets.com/JSF-Tutorial/jsf2/
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Servlets,
JSP, Hibernate/JPA,
JSF 2.0, JavaEJB3,
6, Ajax,
GWT,
Spring,Web
Hibernate,
Spring,
GWT, jQuery,
SOAP-based
and RESTful
Services RESTful Web Services, Android.
Contact
hall@coreservlets.com
for details
Developed and taught by well-known
author
and developer. At public
venues or onsite at your location.
Overview
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
What is JSF?
A set of Web-based GUI controls and handlers?
JSF provides
id many prebuilt
b ilt HTML
HTML-oriented
i t d GUI controls,
t l along
l
with code to handle their events.
An Ajax library?
JSF 2.0 provides very easy-to-use Ajax support. So, JSF 2.0 can be
viewed as an alternative to jQuery or GWT.
But
B t which
hi h is
i the
th proper way to
t view
i
JSF?
6
The answer depends on what you are going to use it for, but 1 & 3
are the most common wasy of looking at JSF.
Issues
Alternatives: traditional Web apps
Servlets/JSP (with MVC)
Struts 2.0
JSF
S 22.0
0
MVC
A fair comparison is to look at the use of the framework
vs. the use of MVC with servlets and JSP
9
Form
submit form
(Form ACTION matches
url-pattern of servlet)
Servlet
JSP1
JSP2
JSP3
10
Applying MVC:
Bank Account Balances
Bean
BankCustomer
B kC t
Business Logic
BankCustomerLookup
11
12
14
<fieldset>
<legend>Bank Account Balance</legend>
<form action="./show-balance">
p
type="text"
yp
name="id"><br>
Customer ID: <input
<input type="submit" value="Show Balance">
</form>
</fieldset>
For the servlet, use the address http://host/appName/show-balance that is set via url-pattern in web.xml
15
<BODY>
<TABLE BORDER=5 ALIGN="CENTER">
<TR><TH CLASS="TITLE">
We Know Where You Live!</TABLE>
<P>
<IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">
Watch out, ${customer.firstName},
we know where you live.
<P>
Pay us the $${customer.balanceNoSign}
you owe us before
b f
it i
is t
too l
late!
t !
</BODY></HTML>
16
17
18
Event handling
JSF makes it easy to designate Java code that is invoked when forms are
submitted. The code can respond to particular buttons, changes in
pparticular values,, certain user selections,, and so on.
Managed beans
In JSP, you can use property="*" with jsp:setProperty to automatically
ppopulate
p
a bean based on request
q
pparameters. JSF extends this capability
p
y
and adds in several utilities, all of which serve to greatly simplify request
param processing.
19
You can use jQuery, Dojo, or Ext-JS with servlets and JSP. However, JSF
lets you use Ajax without explicit JavaScript programming and with very
simple tags. Also, the Ajax calls know about the server-side business logic.
Page templating
Although JSP has jsp:include for reuse of content, JSF has a full-fledged
ppage
g templating
p
g system
y
that lets yyou build ppages
g that share layout
y
or
content
Consistent approach
JSF encourages consistent use of MVC throughout your application.
20
T
To use MVC with
ith the
th standard
t d d RequestDispatcher,
R
tDi t h you
need to be comfortable with the standard JSP and servlet
APIs. To use MVC with JSF, you have to be comfortable
with the servlet API and a large and elaborate framework
that is almost equal in size to the core system.
Similarly, if you have an existing app and want to add in
some small amounts of Ajax functionality
functionality, it is
moderately easy with jQuery (quite easy if you know
JavaScript already). Switching your app to JSF 2.0 is a
bigg investment.
Worse documentation
21
Undeveloped
p tool support
pp
There are many IDEs with strong support for standard
servlet and JSP technology. JSF 2.0 is pretty new, and
JSF 2.0 support
pp was weak in Eclipse
p 3.5,, but good
g
in
NetBeans and Eclipse 3.6.
Rigid approach
22
Problem
JSF is not the only game in town
Alternatives
Alt
ti
Struts is the most popular alternative to JSF
But there are many more
24
Spring MVC
Apache Wicket
Apache Tapestry
jMaki
Advantages of JSF
(vs Struts)
(vs.
Custom components
JSF makes
k it relatively
l ti l easy to
t combine
bi complex
l GUIs
GUI into
i t a single
i l
manageable component; Struts does not
There are many existing third-party component libraries for JSF,
virtually
i t ll none for
f Struts
St t
PrimeFaces, JBoss RichFaces, Oracle ADF, IceFaces, Apache
Tomahawk, Woodstock, Web Galileo,
Official
Offi i l partt off Java
J
EE
25
JSF is part of the Java EE spec, and all servers that support Java EE
include JSF (JSF 2.0 is part of Java EE 6)
26
Advantages of JSF
(vs Struts),
(vs.
Struts) Continued
Expression language
Th
The JSF expression
i llanguage iis more concise
i and
d
powerful than the Struts bean:write tag.
This is less advantageous vs. Struts 2.0
Disadvantages of JSF
(vs Struts)
(vs.
Established base
28
Counter-Arguments re
Established Base
29
Self-submit approach
30
With
i h Struts,
S
the
h form
f
(blah.jsp)
(bl h ) and
d the
h hhandler
dl
(blah.do)have different URLs; with JSF they are the same
(blah.jsf).
31
JSF 2.0
2 0 vs
vs.
Other Ajax
j Approaches
pp
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
GWT
Write everything in Java. Use RPC mechanism to talk to
server. Client-side code g
gets compiled
p
to JavaScript.
p
Best approach
Use JavaScript library with rich widgets and Ajax support
jQuery, Ext-JS, Dojo, YUI, Prototype/Scriptaculous,
Google Closure, Mootools
Goals
Do not reload the page
Use existing server-side logic
Approach
Use jQuery to pass ID to server via Ajax request and to
insert result into page
Extend server to format customer as <ul> list
35
<fieldset>
<legend>Find Customer Info</legend>
<form id="customer-form">
Customer ID:
<input type="text" name="customerID"/>
<input type="button" value="Show Customer"
id "
id="customer-button"/>
t
b tt "/>
</form>
<div id="customer-region"/>
</fieldset>
/
36
37
38
39
Using GWT
Scenario
You are starting a new Web app
You want a large pure-Ajax app
Looks
L k lik
like a d
desktop
kt application
li ti
Has complex client-server communication
For client-side
client side code, you want strong typing, many data
structures, and few runtime errors
You want Java, not JavaScript for the client!
Best approach
Use the Google Web Toolkit
Client-side Java code gets compiled to JavaScript
Powerful RPC mechanism for talking to server
40
Goals
Do not reload the page
Use straightforward server-side logic (no HTTP methods)
Return Customer object, not just String, to client
Approach
U
Use GWT
Use Customer class on both client and server
41
43
45
46
47
Best approach
pp
Use a Web app framework that includes rich controls and
integrated Ajax support
Java developers: JSF 2.0, Struts 2.0, or Spring MVC 3.0
.NET developers: ASP.NET Ajax
48
Goals
Do not reload the page
Use existing server-side logic and bean names
Use familiar JSF tags, not explicit JavaScript
Approach
Use
U JSF 22.0
0 <f
<f:ajax>
j > ttag
Leave server code unchanged
49
<h:form>
Customer ID:
<h:inputText value="#{bankingBeanAjax.customerId}"/><br/>
Password:
<h:inputSecret value="#{bankingBeanAjax.password}"/><br/>
<h:commandButton value="Show Current Balance"
{
g
j
}
action="#{bankingBeanAjax.showBalance}">
<f:ajax execute="@form"
render="ajaxMessage1"/>
</h:commandButton>
<br/>
<h2><h:outputText value="#{bankingBeanAjax.message}"
id="ajaxMessage1"/></h2>
</h:form>
50
51
52
53
54
Bottom Line
Use traditional JavaScript library when:
You have existing app already built
You want to incrementally add rich GUIs/Ajax to it
Smartt defaults
S
d f lt
Annotations to replace many faces-config.xml entries
Ajax support
Integrated support for facelets
Simpler custom components
More components
p
and validators
Support for Groovy
Ability to bookmark results pages
Lots more
Downside
Simple installation and testing instructions hard to find
57
Rectifying
R tif i thi
this iis th
the main
i point
i t off nextt section
ti
Next section gives a whirlwind tour of main new JSF 2 features
Later sections give detailed tutorial on JSF 2.0
Apache MyFaces
Main page: http://myfaces.apache.org/core20/
http://myfaces apache org/core20/
Runs in any server supporting servlets 2.5 or later
Also integrated into Apache Geronimo 3
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
y
General Web apps
Other Java
Java-based
based frameworks: No
For mainstream corporate
projects, other frameworks
(except possibly Spring MVC)
have too small of a market share
to be taken seriously, regardless
of technical features.
60
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.