2. Lecture 8 - Java Server Faces
(JSF)
โข Introduction to JSF
โข JSF Architecture
โข JSF Lifecycle
โข JSF Development Steps
2 JEE - Java Server Faces (JSF)
3. 3 Layers of Information System
3 JEE - Java Server Faces (JSF)
presentation
layer
application logic
layer
resource management
layer
Client
informationsystem
EJB
JSP JSF
Servlets
JPA
Database
4. Java Server Faces
โข JavaServer Faces technology is a server-side user
interface component framework for Java technology-
based web applications.
4 JEE - Java Server Faces (JSF)
5. Components of JSF
โข An API for
โข representing UI components and managing their state
โข handling events
โข server-side validation
โข data conversion
โข defining page navigation
โข supporting internationalisation and accessibility
โข Two JavaServer Pages (JSP) custom tag libraries for
expressing UI components within a JSP page and for
wiring components to server-side objects
5 JEE - Java Server Faces (JSF)
6. Why JSF ?
โข Drop components onto a page by adding component
tags
โข Wire component-generated events to server-side
application code
โข Bind UI components on a page to server-side data
โข Construct a UI with reusable and extensible
components
โข Save and restore UI state beyond the life of server
requests
6 JEE - Java Server Faces (JSF)
11. JSF User Interface
โข The UI for the web application manages the objects referenced by the
JSP page.
โข These objects include
โข The UI component objects that map to the tags on the JSP page
โข Any event listeners, validators, and converters that are registered on the
components
โข The JavaBeans components that encapsulate the data and application-
specific functionality of the components
11 JEE - Java Server Faces (JSF)
12. JSF Lifecycle
12 JEE - Java Server Faces (JSF)
Client Browser
client
Restore Value
Re-constitute component
tree
Apply Request
Values
Process Events,
Validators
Render Response
Invoke Application
Logic
Update Model
Values
13. Restore View Phase
โข JSF implementation
โข builds the view of the page
โข wires event handlers and validators to components in the
view
โข saves the view in the FacesContext instance
โข Initial Request - creates an empty view, advances to
the render response phase
โข Subsequent Request - restores the (already existing)
view by using the state information saved on the
client or the server
13 JEE - Java Server Faces (JSF)
14. Apply Request Values Phase
โข Each component in the tree extracts its new value from the
request parameters by using its decode method
โข Implementation skips to the render response phase if
renderResponse is called
โข If events have been queued, the they are broadcasted to
interested listeners
โข validation, conversion, and events associated with
components having immediate attributes will be processed
during this phase
14 JEE - Java Server Faces (JSF)
15. Process Event, Validators Phase
โข JavaServer Faces implementation processes all
validators registered on the components in the tree
โข If validation fails, error message is added to
FacesContext
โข Events from previous phase and this phase are used to render
errors by advancing to render response phase
โข Implementation skips to the render response phase if
renderResponse is called
โข If events have been queued, the they are broadcasted to
interested listeners
15 JEE - Java Server Faces (JSF)
16. Update Model Values Phase
โข If data is valid, the implementation can traverse the
component tree and set the corresponding server-side
object properties to the componentsโ local values.
โข Implementation will update the bean properties
pointed at by an input componentโs value attribute.
โข Implementation skips to the render response phase to
display any error messages
โข If events have been queued, the they are broadcasted to
interested listeners
16 JEE - Java Server Faces (JSF)
17. Invoke Application Logic Phase
โข JSF implementation handles any application-level
events, such as submitting a form or linking to
another page
โข If the view being processed was reconstructed from
state information from a previous request and if a
component has fired an event, these events are
broadcast to interested listeners.
17 JEE - Java Server Faces (JSF)
18. Render Response Phase
โข JSF implementation delegates authority for rendering the
page to the JSP container if the application is using JSP
pages.
โข For initial request, the components represented on the page
will be added to the component tree
โข components donโt need to added again for subsequent
requests
โข Components will be rendered as the JSP container
traverses the tags in the page.
โข queued error messages are displayed, if any
โข Once rendered, the state of the response is saved so that
subsequent requests can access it
18 JEE - Java Server Faces (JSF)
19. 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=โfNameInput"
value="#{UserRegistrationBean.firstName}"/>
19 JEE - Java Server Faces (JSF)
20. JSF Validators
โข 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
โข Example
<h:input_text valueRef=โtestingBean.todayโ
<f:validator_length minimum=โ6โ maximum='10โ />
20 JEE - Java Server Faces (JSF)
21. JSF Converters
โข ConvertersโPlug-in for conversions:
โข Output: Object to String
โข Input: String to Object
โข Standard implementations for common cases
โข Example
<h:input_text valueRef=โtestingBean.todayโ
convertor=โDateTimeโ/>
21 JEE - Java Server Faces (JSF)
22. Navigation
โข Application developer defines the navigation model of
the web application
โข in Application configuration file (Facesconfig.xml)
โข Navigation rules
โข Determine where (page) to go.
โข Precise navigation case
22 JEE - Java Server Faces (JSF)
23. JSF Development Steps
1. Build Model from Java Beans
โข Lifetime Configured by developer and managed by JSF
โข Request, Session, or Application Scope
โข Setters and getters accessed through JSF pages
2. Add model objects (managed bean) declarations to
Application Configuration File faces-config.xml
3. Use UI Components to build JSF pages
โข Include JSF Tags, Validation and Event Listeners
4. Define Page Navigation rules in faces.config.xml
5. Configure web.xml
23 JEE - Java Server Faces (JSF)
24. Step 1 - Build Model
โข The model (M) in MVC
โข A regular JavaBeans with getters / setters
โข 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
24 JEE - Java Server Faces (JSF)
25. Step 2 - Declare Model Objects
โข In Faces-config.xml
<managed-bean>
<managed-bean-name>
LoginFormBean
</managed-bean-name>
<managed-bean-class>
myapp.LoginFormBean
</managed-bean-class>
<managed-bean-scope>
request
</managed-bean-scope>
</managed-bean>
25 JEE - Java Server Faces (JSF)
26. Step 3 - Create JSF Pages
โข Must include JSF tag library
โข HTML and core tags
โข All JSF tags must enclosed between a set of view tag
โข Use JSF form and form component tags
โข <h:input_text> not <input type=โtextโ>
โข <h:command_button> not <input type=โsubmitโ>
โข May include validators and event listeners on any
form components
26 JEE - Java Server Faces (JSF)
30. JSF vs. JSP for UI
30
JSF JSP
Components โข Rich UI-data-bound
components with events
provided
โข Custom components
โข Standard tags (JSTL) that are
non-UI and very basic
โข Custom components through
tag libraries
Device
independence
โข Reader kits that provide
device independence
โข None
Error handling
and validation
โข Validation framework
โข Many predefined validators
โข None
Scripting โข Scripts can be attached to
events
โข All components accessible
from scripts
โข Embedded Java in the page
Page flow โข Simple navigation file (faces-
config.xml)
โข None