Web Application Framework - Javaserver Faces
Web Application Framework - Javaserver Faces
JavaServer Faces
TM
● Architecture Overview
● Key Concepts
● Development Steps
● Car Demo
JSF Struts
JSTL
S1AF
JSP TM
Servlet
session tracking, filtering, listener
Model View Controller
• Separate control
• From model data
Request Servlet
• From presentation of view
Controller
Forward Business
Event Event
Response
JSP Bean
View Model
Data
MVC Pattern
JSF Background
● Building high-quality web application user
interfaces is hard
- HTTP request/response model
- HTML dialect(s) and browser capabilities
- Need to support multiple client device types
● Opportunity to attract a new developer
community to the Java Platform
- Corporate Developers
- IT Developers
Presentation Agenda
● Overview
● JavaServer Faces
TM
● Architecture Overview
● Key Concepts
● Development Steps
● Car Demo
Faces Technology?
Response
(markup)
JavaServer Faces
● Emerging Java technology (JSR 127)
– Designed to provide richer UI
– Server-side object model for client-side UI
– Server-side handling of simple client-side events
Model
Page Form Object
c1 A c1 A A
c2 B c2 B B
c3 C c3 C C
Client Server
Relationship to Existing APIs
JSF App
JSF App
JSF Tags
JSP
(1.2 or later) JSF API
Developer Implementer
Page Tool
Author Provider
● Architecture Overview
● Key Concepts
● Development Steps
● Car Demo
naming patterns
● Strongly typed events
● Strongly typed listener registration
● Standard events and listeners
– ActionEvent— UICommand component activated by
the user
– ValueChangedEvent— UIInput component whose
value was just changed
Converters and Validators
● Converters—Plug-in for conversions:
– Output: Object to String
– Input: String to Object
– Standard implementations for common cases
● 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
Faces-config.xml:
Validator
<!-- Validator -->
and Converter
<validator>
<description>FormatValidator Description</description>
<validator-id>FormatValidator</validator-id>
<validator-class>cardemo.FormatValidator</validator-class>
<attribute>
<description>List of format patterns separated by '|'</description>
<attribute-name>formatPatterns</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
</validator>
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
Presentation Agenda
● Overview
● JavaServer Faces
TM
● Architecture Overview
● Key Concepts
● Development Steps
● Car Demo
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
myapp.LoginFormBean
</managed-bean-class>
<managed-bean-scope>
request
</managed-bean-scope>
</managed-bean>
Managed Bean Creation Facility
● Value reference expressions that included
“loginFormBean”
● The server will attempt to find a bean under this
name in some scope
● If there is no such bean, optionally:
– Instantiate a bean of the specified type
– Configure scalar/array/list/map properties
– Store bean instance in specified scope
● Creation rules configured in a faces-config.xml
document
Binding UI to Managed Bean
login.jsp
<h:input_text id=”userName”
valueRef=”LoginFormBean.userName”/>
faces-config.xml
<managed-bean> LoginFormBean.java
<managed-bean-name> public class LoginFormBean
LoginFormBean ...
</managed-bean-name> public void setUserName(...) {
<managed-bean-class> public String getUserName(...) {
myapp.LoginFormBean
</managed-bean-class>
3. Create JSF Pages
● Must include JSF tag library
– HTML and core tags
● All JSF tags must enclosed between a set of
use_faces 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
Two Tag Libraries
● html_basic
- Defines tags for representing common HTML user
interface components
● jsf_core
- Defines other JSF related tags
- Independent of any rendering technology
● JSP page need to declare them
- <%@ taglib uri="http://java.sun.com/jsf/html/"
prefix="h" %>
- <%@ taglib uri="http://java.sun.com/jsf/core/"
prefix="f" %>
Sample JSF Page TM
<f:use_faces>
<f:form formName=”logonForm”>
<h:panel_grid columns=”2”>
<h:output_text value=”Username:”/>
<h:input_text id=”username” length=”16”
valueRef=”loginFormBean.userName”/>
<h:output_text value=”Password:”/>
<h:input_secret id=”password” length=”16”
valueRef=”loginFormBean.passWord”/>
<h:command_button type=”submit”
label=”Log On”
action=”success”/>
<h:command_button type=”reset”
label=”Reset”/>
</h:panel_grid>
</f:form>
</f:use_faces>
Develop Application Interface
● Page author references the business logic
method to be executed:
<h:command_button ... type=”submit”
actionRef=”loginFormBean.login”/>
● Application provides a business object
bean accessible via a key
(“loginFormBean”)
● Business object provides one or more
accessible actions (“login”)
● Actions return outcome used to navigate to
the next page appropriately
Login method
Public Action login() {
err
or
new_usr.jsp
error.jsp
4. Define Page Navigations
Rules: Faces-config.xml
<navigation-rule>
<from-view-id>/login.jsp</from-tree-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/menu.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/menu.jsp</from-tree-id>
<navigation-case>
<from-outcome>selectItems</from-outcome>
<to-view-id>/itemList.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
JSF and Development Tools
● All the major tool vendors are part of the
expert group
– Expect to see support in these tools
● Java Web Services Developer Pack 1.3
● Sun Java System Application Service (Sun
TM
● Architecture Overview
● Key Concepts
● Development Steps
● Car Demo
Finish
Storefront.jsp Customer.jsp Thanks.jsp
Change
options Give personal
information
Buy.jsp
Combining SVG and JavaServer™
Faces Technologies
● Scalable Vector Graphics (SVG)
is an industry standard for rich, interactive, 2D
graphics
● JavaServer™ Faces technology
is an extensible, markup independent server-side
user interface framework that works with SVG
● There is an exciting synergy when
you use the two technologies together
How to Work Together?
● SVG and JavaServer™ Faces technologies are
great complements:
– SVG is a markup language for interactive graphics
which can be generated server-side
– JavaServer™ Faces technology is a
server-side component framework that can
generate any user interface markup
● To combine them, you would:
– Build a RenderKit that emits SVG markup instead of
HTML or WML markup
Car Demo: Select a Locale
Car Demo
Car Demo: JSF&SVG
Car Demo
Car Demo: JSF & HTML
Car Demo: JSF & SVG
Car Demo: JSF & SVG
Car Demo
Car Demo: Validation
Car Demo
Presentation Agenda
● Overview
● Frameworks
– Struts
– JavaServer Faces
TM
components
● JavaServer Faces is designed to
TM