Unit 5
Unit 5
The Fundamentals
JavaServer Faces (JSF)
Page Author
Creates the user interface of a web application
Familiar with markup language(s) to be used
Assembler of prebuilt components
Component Writer
Creates reusable components, renderers, and libraries
Components – Render-independent properties
Renderers – Render-dependent properties
Roles Definition
Application Developer
Creates the server-side functionality of a web application
not directly related to the user interface
Business logic components implemented in standard J2EE
ways (EJBs, JavaBeans, Connectors)
Persistence tier components implemented in standard
J2EE ways (EJBs, JDBC, Connectors)
Model data exposed to user interface via JavaBean
programming model
Validator, Converter, Event handler
Roles Definition
Tool Provider
Creates tools to assist page authors, component writers,
and application developers
GUI-oriented page development tools
IDEs to facilitate creation of components
Application generators (from high level description)
Web application frameworks that utilize JSF components
for their user interface
JSF Implementor
Provides runtime environment to execute JSF webapps
J2EE SDK 1.4
Criteria for choosing Struts or
JavaServer Faces
JSF JSP JSP and Struts
Scripting Scripts can be attached to Embedded Java™ in the Scripts written in Java Action
events page classes
All components accessible Form data but not
from scripts components accessible
Attributes
Listeners
Converters
Validators
Facets
Parameters
Select items
Jsf html tags
HTML tag attributes
Button
Standard Converters
<h:inputText value="#{payment.amount}">
<f:convertNumber minFractionDigits="2"/>
</h:inputText>
<h:inputText value="#{payment.date}">
<f:convertDateTime pattern="MM/yyyy"/>
</h:inputText>
<h:outputText value="#{payment.amount}">
<f:convertNumber type="currency"/>
</h:outputText>
f:convertNumber tag
f:convertDateTime
Error message
JSF: Validator
Common Validation API beign implemented
for all controls in javax.faces.validator
package and various custom tags.
The validator framework is available with JSF
Pluggable. Using which the developer
provides their own cutom validation class
through the help of configuration files.
Standard Validators
Validating String Lengths and Numeric Ranges
It's easy to use JSF validators within JSF
pages—simply add validator tags to the body
of a component tag, like this:
<h:inputText value="#{payment.card}">
<f:validateLength minimum="13"/>
</h:inputText>
UserRegistration.jsp
UserRegistration.jsp
Success.jsp
UserBean.java
UserBean.java
PhoneNumberValidator.java
PhoneNumberValidator.java
Faces-config.xml
Faces-config.xml
Web.xml
JSF Standard UI Components
<body>
<ui:composition>
<h1>Default Header</h1>
</ui:composition>
</body>
</html>
Footer.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body>
<ui:composition>
<h1>Default Footer</h1>
</ui:composition>
</body>
</html>
Contents.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body>
<ui:composition>
<h1>Default Content</h1>
</ui:composition>
</body>
</html>
Common.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:ui = "http://java.sun.com/jsf/facelets">
<h:head></h:head>
<h:body>
<div style = "border-width:2px; border-color:green; border-style:solid;">
<ui:insert name = "header" >
<ui:include src = "/templates/header.xhtml" />
</ui:insert>
</div>
<br/>
<div style = "border-width:2px; border-color:black; border-style:solid;">
<ui:insert name = "content" >
<ui:include src = "/templates/contents.xhtml" />
</ui:insert>
</div>
<br/>
<div style = "border-width:2px; border-color:red; border-style:solid;">
<ui:insert name = "footer" >
<ui:include src = "/templates/footer.xhtml" />
</ui:insert>
</div>
</h:body>
</html>
Page1.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:body>
<ui:composition template = "templates/common.xhtml">
<ui:define name = "header">
<h2>Page1 header</h2>
</ui:define>
</h:body>
</html>
Page2.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:body>
<ui:composition template = "templates/common.xhtml">
<ui:define name = "header">
<h2>Page2 header</h2>
</ui:define>
</h:body>
</html>
home.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:body>
<ui:composition template = "templates/common.xhtml">
<ui:define name = "content">
<br/><br/>
<h:link value = "Page 1" outcome = "page1" />
<h:link value = "Page 2" outcome = "page2" />
<br/><br/>
</ui:define>
</ui:composition>
</h:body>
</html>
Output
PrimeFaces
PrimeFaces is an open source component library for JSF 2.0
with more than 100 rich components.
PrimeFaces is far better than many other JSF component
libraries because of the following reasons:
<h:head>
</h:head>
<h:body>
<h1>Hello World PrimeFaces</h1>
<h:form>
<p:editor value="#{editor.value}" />
</h:form>
</h:body>
</html>
Output
The End