Академический Документы
Профессиональный Документы
Культура Документы
2#Semestre#2014/15,#Introduo#a#JSF#
Filipe Arajo
filipius@uc.pt
Filipe Amaral
filipemartinsamaral@gmail.com
Typical)JavaServer)Faces)Web)Page)
! A#set#of#namespace#declara?ons#that#declare#the#
JavaServer#Faces#tag#libraries#
! <html#xmlns="hIp://www.w3.org/1999/xhtml"#
xmlns:h="hIp://xmlns.jcp.org/jsf/html"#
xmlns:f="hIp://xmlns.jcp.org/jsf/core">###
! Op?onally,#the#HTML#head#(h:head)#and#body#
(h:body)#tags##
! A#form#tag#(h:form)#that#represents#the#user#input#
components#
3 - JSF
Example)
3 - JSF
HTML)head)and)body)tags)
3 - JSF
page you must use the prefix that you have chosen for the tag library. For example, in
the following web page the form tag must be referenced using the h prefix because the
preceding tag library directive uses the h prefix to distinguish the tags defined in the
HTML tag library:
<h:form ...>
The sections Adding Components to a Page Using HTML Tag Library Tags and Using
Core Tags describe how to use the component tags from the JavaServer Faces standard
HTML tag library and the core tags from the JavaServer Faces core tag library.
Tag
Functions
h:inputHidden
No appearance
h:inputSecret
An HTML <input
type="password">
element
A field that
displays a row of
characters instead
of the actual
string entered
h:inputText
An HTML <input
type="text">
element
A field
Appearance
h:inputTextarea
An HTML
<textarea> element
A multirow field
A column in a
table
h:message
Displays a localized
message
h:messages
Displays localized
messages
A set of HTML
<span> tags if styles
are used
A text string
The)HTML)Tag)Library)
The tags defined by the JavaServer Faces standard HTML tag library represent HTML
form components and other basic HTML elements. These components display data or
accept data from the user. This data is collected as part of a form and is submitted to
the server, usually when the user clicks a button. This section explains how to use each
of the component tags shown in Table 101.
Table 101
Tag
Functions
h:column
Represents a column of
A column of data in
data in a data component an HTML table
h:commandButton
An HTML <input
A button
type=value> element
for which the type
value can be
"submit", "reset", or
"image"
h:outputFormat
Displays a formatted
message
Plain text
Plain text
h:commandLink
A link
h:outputLabel
An HTML <label>
element
Plain text
h:dataTable
Represents a data
wrapper
An HTML <table>
element
Displays a nested
component as a label for
a specified input field
h:outputLink
An HTML <a>
element
A link
h:outputText
Plain text
Plain text
h:panelGrid
Displays a table
An HTML <table>
element with <tr>
and <td> elements
A table
h:panelGroup
Groups a set of
components under one
parent
A HTML <div> or
<span> element
A row in a table
An HTML <input
type="checkbox">
element
A check box
A set of HTML
<input> elements of
type checkbox
A group of check
boxes
h:form
h:graphicImage
Rendered As
Appearance
No appearance
Displays an image
An image
An HTML <img>
element
type="file">
element
A field with a
Browse... button
Appearance
h:selectOneListbox
Allows a user to select
10-2 Java Platform, Enterprise Edition The one
Javaitem
EE Tutorial
from a set of
items all displayed at
once
Tag
Functions
An HTML <select>
element
A box
h:selectOneMenu
An HTML <select>
element
A menu
h:selectOneRadio
An HTML <input
type="radio">
element
A group of
options
3 - JSF
h:selectManyListbox
An HTML <select>
element
A box
h:selectManyMenu
An HTML <select>
element
A menu
Attribute
Description
binding
id
immediate
If set to true, indicates that any events, validation, and conversion associated
with the component should happen when request parameter values are
applied.
rendered
style
styleClass
value
All the tag attributes except id can accept expressions, as defined by the EL, described
in Expression Language.
An attribute such as rendered or value can be set on the page and then modified in
!
Other#tags#that#are#
the backing
bean for the page.
part#of#a#form#
The id attribute is not usually required for a component tag but is used when another
component or a server-side class must refer to the component. If you don't include an
id attribute, the JavaServer Faces implementation automatically generates a
! h:inputFile#
! h:inputHidden#
! h:inputSecret#
! h:inputText#
! h:inputTextarea#
! #
3 - JSF
The)h:form)
Tag)
Text)Components)
!
Input#Tags#
!
!
!
!
h:inputHidden#
h:inputSecret#
h:inputText#
h:inputTextarea#
Output#Tags#
!
!
!
!
h:outputFormat#
h:outputLabel#
h:outputLink#
h:outputText#
Input#Tag#AIributes#
!
!
!
!
!
!
!
!
!
!
converter#
converterMessage#
dir#
label#
lang#
required#
requiredMessage#
validator#
validatorMessage#
valueChangeListener#
3 - JSF
Displaying)Components)for)Selecting)
Values)
! h:selectBooleanCheckbox#
! h:selectManyCheckbox##
! h:selectOneRadio#
! h:selectManyListbox##
! h:selectOneMenu#
! h:selectManyMenu##
! h:selectOneListbox#
3 - JSF
h:selectBooleanCheckBox)
3 - JSF
10
h:selectManyCheckBox)
3 - JSF
h:selectManyCheckBox)
11
3 - JSF
12
h:dataTable))
3 - JSF
13
h:dataTable))
3 - JSF
14
Common)Component)Tag)Attributes)
! binding#
! id#
! immediate#
! rendered#
! style#
! styleClass#
! value#
3 - JSF
15
The)immediate)Attribute)
! If#set#to#true,#indicates#that#any#events,#valida?on,#and#
conversion#associated#with#the#component#should#happen#
when#request#parameter#values#are#applied#
Submit
Good for a
Cancel button
3 - JSF
Submit
Immediate
16
Immediate)on)Action)Component)
3 - JSF
17
Immediate)on)Input)Components)
3 - JSF
18
Converters)
! Note:#JSF#automa?cally#converts#component#data#when#the#
component#value#binding#type#is#a#primi?ve#type,#
BigDecimal,#or#BigInteger.#For#Date#values,#you#need#to#add#
an#explicit#converter#because#you#can#specify#the#forma`ng#
style#to#convert#to#for#date#and#?me#por?ons#
3 - JSF
19
Converters)
! Perhaps#more#interes?ng:#
3 - JSF
20
Listeners)
! ValuecChange#Listener#
! Ac?on#Listener#
3 - JSF
21
Writing)a)Method)to)Handle)an)Action)
Event)
value="#{bundle.CartAdd}">
<f:setPropertyActionListener target="#{requestScope.book}"
value="#{book}"/>
</h:commandButton>
</h:column>
The h:commandLink and h:commandButton tags are within an h:dataTable tag, which
iterates over the list of books. The var attribute refers to a single book in the list of
books.
3 - JSF
The object referenced by the var attribute of an h:dataTable tag is in page scope.
However, in this case you need to put this object into request scope so that when the
user activates the commandLink component to go to bookdetails.xhtml or activates the
commandButton component to go to bookcatalog.xhtml, the book data is available to
those pages. Therefore, the f:setPropertyActionListener tag is used to set the
current book object into request scope when the commandLink or commandButton
component is activated.
In the preceding example, the f:setPropertyActionListener tag's value attribute
references the book object. The f:setPropertyActionListener tag's target attribute
references the value expression requestScope.book, which is where the book object
referenced by the value attribute is stored when the commandLink or the
22
commandButton component is activated.
Validators)
JavaServer Faces technology provides a set of standard classes and associated tags that
page authors and application developers can use to validate a component's data.
Table 115 lists all the standard validator classes and the tags that allow you to use the
validators from the page.
Table 115
Validator Class
Tag
Function
BeanValidator
validateBean
DoubleRangeValidator
validateDoubleRange
LengthValidator
validateLength
LongRangeValidator
validateLongRange
RegexValidator
validateRegex
RequiredValidator
validateRequired
3 - JSF
23
Expression)Language)(EL))
! The#EL#provides#a#way#to#use#simple#expressions#to#
perform#the#following#tasks:#
! Dynamically#read#applica?on#data#stored#in#
JavaBeans#components,#various#data#structures,#and#
implicit#objects
Dynamically#write#data,#such#as#user#input#into#
forms,#to#JavaBeans#components#
Invoke#arbitrary#sta?c#and#public#methods#
Dynamically#perform#arithme?c,#boolean,#and#string#
opera?ons#
Dynamically#construct#collec?on#objects#and#
perform#opera?ons#on#collec?ons#
#
!
!
3 - JSF
24
Immediate)and)Deferred)Evaluation)
Syntax)
! Immediate:#${}#
! The#expression#is#evaluated#and#the#result#returned#
as#soon#as#the#page#is#rst#rendered##
! Deferred:##{}#
! The#technology#using#the#expression#language#can#
use#its#own#machinery#to#evaluate#the#expression#
some?me#later#during#the#page's#lifecycle,#
whenever#it#is#appropriate#to#do#so#
! There#is#no#dierence#in#Facelets#
3 - JSF
25
Value)Expressions)
!
Referencing#Objects#
!
A#topclevel#iden?er#(such#as#
customer#in#the#expression#
customer.name)#can#refer#to#
the#following#objects:##
Lambda#parameters#
EL#variables#
! Managed#beans#
! Implicit#objects#
! Classes#of#sta?c#elds#and#
methods#
!
!
Referencing#Object#Proper?es#or#
Collec?on#Elements#
!
!
!
Referencing#Literals#
!
!
!
${customer.address["street"]}#
${Boolean.FALSE}#
${customer.orders[1]}##
${"literal"}#
${true}#
${57}#
Parametrized#Method#Calls#
!
<h:inputText#
value="#{userNumberBean.userNum
ber('5')}">##
3 - JSF
26
Method)&)Lambda)Expressions,)
Collections)
!
EL#supports#deferred#method#expressions#
Lambda#Expressions#
!
!
!
xc>x+1#
(x,#y)#c>#x#+#y#
((x,y)#c>#x#+#y)(3,4)#
!
Opera?on#on#Collec?on#Objects#
!
!
!
3 - JSF
Evaluates#to#7#
A#set:#{1,2,3}#
A#list:#[1,2,3]#
A#map:#{"one":1,#"two":2,#"three":3}##
27
Example Expressions
EL Expression
Result
${1> (4/2)}
false
${4.0>= 3}
true
${100.0 == 100}
true
${(10*10) ne 100}
false
false
${'hip' lt 'hit'}
true
${4> 3}
true
${1.2E4 + 1.4}
12001.4
${3 div 4}
0.75
${10 mod 4}
8.5
[1,2,3,4].stream().sum()
10
Examples)
[1,3,5,2].stream().sorted().toList()
[1, 2, 3, 5]
${!empty param.Add}
${pageContext.request.contextPath}
${sessionScope.cart.numberOfItems}
${param['mycom.productId']}
${header["host"]}
The host
${departments[deptName]}
#{customer.calcTotal}
3 - JSF
28
Writing)a))Managed)Bean))Method)to)
Handle)Navigation)
Expression Language
9-11
3 - JSF
29
The)Managed)Bean)
3 - JSF