Introduction To Java Server Faces JSF Sun
Introduction To Java Server Faces JSF Sun
Sun Microsystems
Objective
2
Agenda
What is and why JSF?
Architecture Overview
UI Component Model
Development Steps
JavaServer™ Faces (JSF)
Framework Is…
4
What is JSF?
● A specification and reference
implementation for a web application
development framework
– Components
– Events
– Validators & converters
– Navigation
– Back-end-data integration
5
Why JSF? (page 1)
● MVC for web applications
● Clean separation of roles
● Easy to use
● Extendable Component and Rendering
architecture
● Support for client device independence
● Standard
● Huge vendor and industry support
6
Why JSF? (page 2)
● JSP and Servlet
– No built-in UI component model
● Struts (I am not saying you should not use Struts)
– No built-in UI component model
– No built-in event model for UI components
– No built-in state management for UI components
– No built-in support of multiple renderers
– Not a standard (despite its popularity)
● Struts and JSF can be used together
7
How the JSF Specification Fits In
JSF App
JSF App
JSF Tags
Servlets (2.3)
8
Agenda
What is and why JSF?
Architecture Overview
UI Component Model
Development Steps
JSF Architecture [MVC]
Server
JSF Page
HTML
Desktop
HTML
Browser RenderKit
Front App
ctrl JSF Page
Backend
WML
RenderKit
Phone
WML
10
Request processing Lifecycle
FacesServlet
Lifecycle FacesContext
3. Process FacesContext in various phase
Request Processing Lifecycle Phases
Faces
Request Reconstitute Apply
Process
Component Request
Validations
Tree Values
Faces
Response Update
Render Invoke
Model
Response Application
Values
12
Request Processing Lifecycle
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Process Process Process
Component Request
Events Validations Events
Tree Values
Render Response
Response Response
Complete Complete
Faces
Response Update
Render Process Invoke Process
Model
Responder Events Application Events
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
13
Request Processing Lifecycle
Phases
1.Reconstitute component tree phase
2.Apply request values phase
3.Process validations phase
4.Update model values phase
5.Invoke application phase
6.Render response phase
14
Agenda
What is and why JSF?
Architecture Overview
UI Component Model
Development Steps
User Interface Component Model
● UI components
● Event handling model
● Conversion and Validation model
● Rendering model
● Page navigation support
16
UI Components
UIComponent/UIComponentBase
Base class for all user interface components
Standard UIComponent Subclasses
UICommand, UIForm, UIOutput
UIGraphic, UIInput, UIPanel, UIParameter
UISelectBoolean, UISelectMany, UISelectOne
Example:
<h:inputText id="userNo"
value="#{UserNumberBean.userNumber}"/>
Validators and Converters
Validators—Perform correctness checks on
UIInput values
Register one or more per component
Enqueue one or more messages on errors
Standard implementations for common cases
Converters—Plug-in for conversions:
Output: Object to String
Input: String to Object
Standard implementations for common cases
Converters and Validators
Example:
Converters:
<h:input_text valueRef=”testingBean.today”
convertor=”DateTime”/>
Validators:
<h:input_text valueRef=”testingBean.today”
<f:validator_length minimum=”6” maximum='10” />
Rendering Model
Renderers-Adapt components to a specific markup
language
Decoding
Encoding
RenderKits—Library of Renderers
Map component classes to component tags
Is a custom tag library
Basic HTML RenderKit
Events and Listeners
Follows JavaBeans™ Specification design and
naming patterns
Standard events and listeners
ActionEvent—UICommand component activated
by the user
ValueChangedEvent—UIInput component whose
value was just changed
Navigation Model
Navigation rules
Determine which page to go.
Navigation case
Navigation Model
<navigation-rule>
<from-tree-id>/login.jsp</from-tree-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-tree-id>/menu.jsp</to-tree-id>
</navigation-case>
<navigation-case>
<from-outcome>failed</from-outcome>
<to-tree-id>/error.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
Agenda
What is and why JSF?
Architecture Overview
UI Component Model
Development Steps
Steps in Development Process
1. Develop model objects which hold the data
2. Add model objects (managed bean) declarations to
Application Configuration File faces-config.xml
3. Create Pages using UI component and core tags
4. Define Page Navigation in faces-config.xml
5. Configure web.xml
Step1: Develop model Objects
(Managed Bean)
The model (M) in MVC
A regular JavaBeans with read/write properties
May contain application methods and event handlers
Use to hold data from a UI (page)
Creation and lifetime is managed by JSF runtime
application, session, request
JSF keeps the bean's data in sync with the UI
Step 2. Managed Bean Declaration
(Faces-config.xml)
01 <managed-bean>
02 <managed-bean-name>
03 LoginFormBean
04 </managed-bean-name>
05 <managed-bean-class>
06 myapp.LoginFormBean
07 </managed-bean-class>
08 <managed-bean-scope>
09 request
10 </managed-bean-scope>
11 </managed-bean>
Step 3: Create JSF Pages
<h:input_text id=”userName”
valueRef=”LoginFormBean.userName”/>
faces-config.xml LoginFormBean.java
Required Jars:
WEB-INF/lib/jsf-api.jar
WEB-INF/lib/jsf-ri.jar
WEB-INF/lib/jstl.jar
WEB-INF/lib/jsf-el.jar
WEB-INF/lib/standard.jar
WEB-INF/lib/commons-beanutils.jar
WEB-INF/lib/commons-digester.jar
WEB-INF/lib/commons-collections.jar
WEB-INF/lib/commons-logging.jar
Summary
JSF: Server side UI component framework
MVC
Developing application in JSF
Reference
http://www.jsfcentral.com/reading/index.html
http://java.sun.com/j2ee/javaserverfaces/
http://www.jcp.org/en/jsr/detail?id=127
Q&A
deepak.goyal@sun.com
vikas.varma@sun.com