Professional Documents
Culture Documents
0, all your web resources files like css, images or JavaScript, should put in resources
folder, under the root of your web application (same folder level with WEB-INF ).
The sub-folder under resources folder is consider as library or project theme, later you can
reference those resources with library attribute. This new JSF resources management
mechanism is really useful, it allow developer to change the web resources easily by theme/library
or versioning.
See below examples :
Figure 1-0 : Example of a JSF2 project folder structure.
1. Normal Example
Here are some examples using resources and library in JSF 2.0.
1.
HTML output
<link type="text/css" rel="stylesheet"
href="/JavaServerFaces/faces/javax.faces.resource/css/style.css?
ln=theme1" />
2.
HTML output
<img src="/JavaServerFaces/faces/javax.faces.resource/img/sofa.png?
ln=theme1" />
3.
HTML output
<script type="text/javascript"
src="/JavaServerFaces/faces/javax.faces.resource/js/hello.js?
ln=theme1">
2. Versioning Example
Refer to Figure 1-0, create a version folder matching regex \d+(_\d+)* under library folder, and
the default JSF ResourceHandler will always get the highest version to display.
P.S Assume your project is Figure 1-0 structure
Include CSS file h:outputStylesheet
Since default theme contains version 1_0 and 2_0, JSF will always get the resources from the
highest version, and append the version at the end of the resource.
See HTML output :
<link type="text/css" rel="stylesheet"
href="/JavaServerFaces/faces/javax.faces.resource/css/style.css?
ln=default&v=2_0" />
Redirection
By default, JSF 2 is perform a forward while navigating to another page, it caused the page URL is
always one behind :). For example, when you move from page1.xhtml to page2.xhtml, the
browser URL address bar will still showing the same page1.xhtml URL.
To avoid this, you can tell JSF to use the redirection by append the faces-redirect=true to the end
of the outcome string.
<h:form>
<h:commandButton action="page2?faces-redirect=true" value="Move to
page2.xhtml" />
</h:form>
Note
For simple page navigation, this new implicit navigation is more then enough; For complex page
navigation, you are still allow to declare the page flow (navigation rule) in the faces-config.xml file.
Page forward.
<h:form>
<h:commandButton action="page1" value="Page1" />
</h:form>
Page redirection.
<h:form>
<h:commandButton action="page1?faces-redirect=true" value="Page1" />
</h:form>
In the navigation rule, you can enable the page redirection by adding a <redirect /> element within
the <navigation-case /> .
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>page1</from-outcome>
<to-view-id>page1.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
In this tutorial, we demonstrate the use of resource bundle to display messages in JSF 2.0. For
maintainability concern, its always recommended to put all the messages in properties file, instead
of hard-code the message in page directly.
1. Properties File
Create a properties file, contains message for the page, and put it into the projects resource folder,
see figure below
messages.properties
message = This is "message"
message.test1 = This is "message.test1"
message.test2 = This is "<h2>message.test3</h2>"
message.test3 = This is "<h2>message.test4</h2>"
message.param1 = This is "message.param1" - {0}
message.param2 = This is "message.param2" - {0} and {1}
In this case, the messages.properties file is given a name of msg, to access the message, just
use msg.key.
hello.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h2>JSF 2.0 and Resource Bundles Examples</h2>
<ol>
/></li>
/></li>
<li>
<h:outputFormat value="#{msg['message.param1']}">
<f:param value="param0" />
</h:outputFormat>
</li>
<li>
<h:outputFormat value="#{msg['message.param2']}">
<f:param value="param0" />
<f:param value="param1" />
</h:outputFormat>
</li>
</ol>
</h:body>
</html>
4. How it works?
Example 1
A normal way to access the message.
<h:outputText value="#{msg.message}" />
//properties file
message = This is "message"
Example 2
For a key that has a dot . as name, you cant use the normal way #{msg.message.test1}, it will
not work. Instead, you should use bracket like #{msg['message.test1']}.
<h:outputText value="#{msg['message.test1']}" />
//properties file
message.test1 = This is "message.test1"
Example 3
To display HTML tag in the message, just add the escape attribute and set it to false.
<h:outputText value="#{msg['message.test2']}" />
<h:outputText value="#{msg['message.test2']}" escape="false" />
<h:outputText value="#{msg['message.test3']}" />
<h:outputText value="#{msg['message.test3']}" escape="false" />
//properties file
message.test2 = This is "<h2>message.test3</h2>"
message.test3 = This is "<h2>message.test4</h2>"
Example 4
For a parameter message, just use the <h:outputFormat /> and <f:param / > tag.
<h:outputFormat value="#{msg['message.param1']}">
<f:param value="param0" />
</h:outputFormat>
<h:outputFormat value="#{msg['message.param2']}">
<f:param value="param0" />
//properties file
message.param1 = This is "message.param1" - {0}
message.param2 = This is "message.param2" - {0} and {1}
5. Demo
URL : http://localhost:8080/JavaServerFaces/hello.jsf
In JSF application, you can change your application locale programmatically like this :
//this example change locale to france
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale('fr');
1. Project Folder
Directory structure for this example.
2. Properties file
Heres two properties files to store English and Chinese message.
welcome.properties
welcome.jsf = Happy learning JSF 2.0
welcome_zh_CN.properties
welcome.jsf = \u5feb\u4e50\u5b66\u4e60 JSF 2.0
Note
For UTF-8 or non-English characters, for example Chinese , you should encode it
with native2ascii tool.
3. faces-config.xml
Include above properties file into your JSF application, and declared en as your default application
locale.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<resource-bundle>
<base-name>com.mkyong.welcome</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
4. Managed Bean
A managed bean, which provide language selection list , and a value change event listener to
change the locale programmatically.
LanguageBean .java
package com.mkyong;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name="language")
@SessionScoped
public class LanguageBean implements Serializable{
if(entry.getValue().toString().equals(newLocaleValue)){
FacesContext.getCurrentInstance()
.getViewRoot().setLocale((Locale)entry.getValue());
}
}
}
5. JSF Page
A JSF page to display a welcome message from properties file, and attach a value change event
listener to a dropdown box.
<?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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:body>
<h:form>
<h3>
<h:outputText value="#{msg['welcome.jsf']}" />
</h3>
<h:panelGrid columns="2">
Language :
<h:selectOneMenu value="#{language.localeCode}"
onchange="submit()"
valueChangeListener="#{language.countryLocaleCodeChanged}">
<f:selectItems value="#{language.countriesInMap}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
</h:body>
</html>
6. Demo
URL : http://localhost:8080/JavaServerFaces/faces/default.xhtml
Default, locale English is display.
If user changes the dropdown box language, it will fire a value change event listener and change the
application locale accordingly.
In JSF, <h:selectBooleanCheckbox /> tag is used to render a single HTML input element of
checkbox type.
//JSF...
<h:selectBooleanCheckbox value="#{user.rememberMe}" /> Remember Me
//HTML output...
<input type="checkbox" name="j_idt6:j_idt8" /> Remember Me
While <h:selectManyCheckbox /> tag is used to render a set of HTML input element of type
checkbox, and format it with HTML table and label tags.
//JSF...
<h:selectManyCheckbox value="#{user.favNumber1}">
<f:selectItem itemValue="1" itemLabel="Number1 - 1" />
<f:selectItem itemValue="2" itemLabel="Number1 - 2" />
<f:selectItem itemValue="3" itemLabel="Number1 - 3" />
</h:selectManyCheckbox>
//HTML output...
<table>
<tr>
<td>
<input name="j_idt6:j_idt10" id="j_idt6:j_idt10:0" value="1"
type="checkbox" />
<label for="j_idt6:j_idt10:0" class=""> Number1 - 1</label></td>
<td>
<input name="j_idt6:j_idt10" id="j_idt6:j_idt10:1" value="2"
type="checkbox" />
<label for="j_idt6:j_idt10:1" class=""> Number1 - 2</label></td>
<td>
2.
3.
4.
Generate values with an Object array and put it into f:selectItems tag, then represent the
value with var attribute.
1. Backing Bean
A backing bean to hold the submitted checkboxes values.
package com.mkyong;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
//Generated by Array
public String[] getFavNumber2Value() {
return favNumber2;
}
//Generated by Map
private static Map<String,Object> number3Value;
static{
number3Value = new LinkedHashMap<String,Object>();
number3Value.put("Number3 - 1", "1"); //label, value
number3Value.put("Number3 - 2", "2");
number3Value.put("Number3 - 3", "3");
number3Value.put("Number3 - 4", "4");
return number4List;
}
<br />
2. Generated by Array :
<h:selectManyCheckbox value="#{user.favNumber2}">
<f:selectItems value="#{user.favNumber2Value}" />
</h:selectManyCheckbox>
<br />
3. Generated by Map :
<h:selectManyCheckbox value="#{user.favNumber3}">
<f:selectItems value="#{user.favNumber3Value}" />
</h:selectManyCheckbox>
<br />
<br />
</h:form>
</h:body>
</html>
result.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"
>
<h:body>
<h2>result.xhtml</h2>
<ol>
<li>user.rememberMe : #{user.rememberMe}</li>
<li>user.favNumber1 : #{user.favNumber1InString}</li>
<li>user.favNumber2 : #{user.favNumber2InString}</li>
<li>user.favNumber3 : #{user.favNumber3InString}</li>
<li>user.favNumber4 : #{user.favNumber4InString}</li>
</ol>
</h:body>
</html>
3. Demo
When submit button is clicked, link to result.xhtml page and display the submitted checkboxe
values.
//...
h:selectManyCheckbox
The values of f:selectItems tag are checked if it matched to the value of h:selectManyCheckbox
tag. In above example, if you set favNumber3 to {1,3} :
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
//...
The favNumber3 checkboxes, Number 1 and Number 3 values are checked by default.
2.
3.
Generate values with an Object array and put it into f:selectItems tag, then represent the
value with a var attribute.
1. Backing Bean
A backing bean to hold the submitted data.
package com.mkyong;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
//Generated by Map
private static Map<String,Object> color2Value;
static{
color2Value = new LinkedHashMap<String,Object>();
color2Value.put("Color2 - Red", "Red"); //label, value
color2Value.put("Color2 - Green", "Green");
color2Value.put("Color3 - Blue", "Blue");
}
return color3List;
}
2. JSF Page
A JSF page to demonstrate the use h:selectOneRadio tag.
<?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:f="http://java.sun.com/jsf/core"
>
<h:body>
/>
Green" />
Blue" />
</h:selectOneRadio>
<br />
2. Generated by Map :
<h:selectOneRadio value="#{user.favColor2}">
<f:selectItems value="#{user.favColor2Value}" />
</h:selectOneRadio>
<br />
/>
</h:selectOneRadio>
<br />
</h:form>
</h:body>
</html>
result.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"
>
<h:body>
<h2>result.xhtml</h2>
<ol>
<li>user.favColor1 : #{user.favColor1}</li>
<li>user.favColor2 : #{user.favColor2}</li>
<li>user.favColor3 : #{user.favColor3}</li>
</ol>
</h:body>
</html>
3. Demo
When submit button is clicked, link to result.xhtml and display the submitted radio button values.
//...
h:selectOneListbox example
A JSF 2.0 example to show the use of h:selectOneListbox tag to render a single select listbox,
and populate the data in 3 different ways :
1.
2.
3.
Generate values with an Object array and put it into f:selectItems tag, then represent the
value with var attribute.
1. Backing Bean
A backing bean to hold and generate data for listbox values.
package com.mkyong;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
//Generated by Map
private static Map<String,Object> year2Value;
static{
year2Value = new LinkedHashMap<String,Object>();
year2Value.put("Year2 - 2000", "2000"); //label, value
year2Value.put("Year2 - 2010", "2010");
year2Value.put("Year2 - 2020", "2020");
}
this.yearValue = yearValue;
}
return year3List;
}
2. JSF Page
A JSF page to demonstrate the use h:selectOneListbox tag.
<?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:f="http://java.sun.com/jsf/core"
>
<h:body>
/>
/>
/>
</h:selectOneListbox>
<br />
2. Generated by Map :
<h:selectOneListbox value="#{user.favYear2}">
<f:selectItems value="#{user.favYear2Value}" />
</h:selectOneListbox>
<br />
<br />
</h:form>
</h:body>
</html>
result.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"
>
<h:body>
<h2>result.xhtml</h2>
<ol>
<li>user.favYear1 : #{user.favYear1}</li>
<li>user.favYear2 : #{user.favYear2}</li>
<li>user.favYear3 : #{user.favYear3}</li>
</ol>
</h:body>
</html>
3. Demo
When submit button is clicked, link to result.xhtml page and display the submitted listbox values.
//...
1.
2.
OutputFormat example
See few use cases of h:outputFormat tag coded in JSF 2.0 web application.
1. Managed Bean
A managed bean, provide some text for demonstration.
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
2. View Page
Page with few h:outputFormat tags example.
JSF
<?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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h1>JSF 2.0 h:outputFormat Example</h1>
<ol>
<li>
<h:outputFormat value="this is param 0 : {0}, param 1 : {1}" >
3. Demo
URL : http://localhost:8080/JavaServerFaces/
The h:button is a new tag in JSF 2.0, you can declared the navigation outcome directly in the
outcome attribute, no need to call a bean to return an outcome like h:commandButton above.
But, if browsers with JavaScript disabled, the navigation will failed, because the h:button tag is
generate an onclick event to handle the navigation via window.location.href. See examples :
1. Normal button without outcome
//JSF
<h:button value="buton" />
//HTML output
<input type="button"
onclick="window.location.href='/JavaServerFaces/faces/currentpage.xhtml;
return false;"
value="buton" />
P.S if the outcome attribute is omitted, the current page URL will treat as the outcome.
2. Normal button with an outcome
//JSF
<h:button value="buton" outcome="login" />
//HTML output
<input type="button"
onclick="window.location.href='/JavaServerFaces/faces/login.xhtml; return
false;"
value="buton" />
//JSF
<h:button value="Click Me" onclick="alert('h:button');" />
//HTML output
<input type="button"
onclick="alert('h:button');window.location.href='/JavaServerFaces/faces/page.x
html;return false;"
value="Click Me" />
My thought
No really sure why JSF 2.0 released this h:button tag, the JavaScript redirection is not practical,
especially in JavaScript disabled browser. The best is integrate the outcome attribute into the
h:commandButton tag, hope it can be done in future release.
//HTML output
<a href="/JavaServerFaces/faces/login.xhtml">Login page</a>
//HTML output
<a href="/JavaServerFaces/faces/login.xhtml?username=mkyong">Login page +
Param</a>
//HTML output
<a href="/JavaServerFaces/faces/login.xhtml">
<img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?
ln=images" />
</a>
1. commandLink
//JSF
<h:commandLink value="Login page" />
//HTML output
<script type="text/javascript"
src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?
ln=javax.faces&stage=Development">
</script>
<a href="#"
onclick="mojarra.jsfcljs(document.getElementById('j_idt6'),
{'j_idt6:j_idt16':'j_idt6:j_idt16'},'');
return false">
Login page
</a>
P.S if the action attribute is omitted, it will reload current page while the button is clicked.
2. commandLink + action
//JSF
<h:commandLink action="#{user.goLoginPage}" value="Login page" />
//HTML output
<script type="text/javascript"
src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?
ln=javax.faces&stage=Development">
</script>
<a href="#"
onclick="mojarra.jsfcljs(document.getElementById('j_idt6'),
{'j_idt6:j_idt18':'j_idt6:j_idt18'},'');
return false">
Login page
</a>
P.S You cant even find the action value in the HTML output, only JSF will know where it goes.
3. commandLink + action + parameter
//JSF
<h:commandLink action="#{user.goLoginPage}" value="Login page + Param ">
<f:param name="username" value="mkyong" />
</h:commandLink>
//HTML output
<script type="text/javascript"
src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?
ln=javax.faces&stage=Development">
</script>
<a href="#"
onclick="mojarra.jsfcljs(document.getElementById('j_idt6'),
{'j_idt6:j_idt20':'j_idt6:j_idt20','username':'mkyong'},'');
return false">
Login page + Param
</a>
//HTML output
<script type="text/javascript"
src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?
ln=javax.faces&stage=Development">
</script>
<a href="#"
onclick="mojarra.jsfcljs(document.getElementById('j_idt6'),
{'j_idt6:j_idt23':'j_idt6:j_idt23'},'');
return false">
<img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?
ln=images" />
</a>
//HTML output
<a href="currentpage.xhtml">Login page</a>
P.S if the value attribute is omitted, it will put the current page URL as the value of the href
attribute.
2. outputLink + value
//JSF
<h:outputLink value="login.xhtml" >
Login page
</h:outputLink>
//HTML output
<a href="login.xhtml">
Login page
</a>
<h:outputLink value="login.xhtml">
<h:outputText value="Login page" />
<f:param name="username" value="mkyong" />
</h:outputLink>
//HTML output
<a href="login.xhtml?username=mkyong">Login page</a>
//HTML output
<a href="login.xhtml">
<img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?
ln=images" />
</a>
My thought
Some review of above three link tags :
1.
The h:link tag is useful to generate a link which requires to interact with the JSF
outcome , but lack of action support make it hard to generate a dynamic outcome.
2.
The h:commandLink tag is suck, the generated JavaScript is really scary! Not recommend
to use this tag, unless you have a solid reason to support. But it supports the action attribute,
which is what h:link lack of.
3.
The h:outputLink is useful to generate a link which does not require to interact with the
JSF program itself.
At last, it will be perfect if the action attribute is added into the h:link.
In JSF , h:panelGrid tag is used to generate HTML table tags to place JSF components in rows
and columns layout, from left to right, top to bottom.
For example, you used to group JSF components with HTML table tags like this :
HTML
<table>
<tbody>
<tr>
<td>
Enter a number :
</td>
<td>
<h:inputText id="number" value="#{dummy.number}"
size="20" required="true"
label="Number" >
<f:convertNumber />
</h:inputText>
</td>
<td>
<h:message for="number" style="color:red" />
</td>
</tr>
</tbody>
</table>
With h:panelGrid tag, you can get the same table layout above, without typing any of the HTML
table tags :
h:panelGrid
<h:panelGrid columns="3">
Enter a number :
</h:panelGrid>
Note
The column attribute is optional, which define the number of columns are required to lay out the
JSF component, defaults to 1.
h:panelGrid example
A JSF 2.0 example to show you how to use the h:panelGrid tag to lay out the components
properly.
1. Managed Bean
A dummy bean for demo.
package com.mkyong;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="dummy")
@SessionScoped
public class DummyBean implements Serializable{
int number;
2. JSF Page
A JSF XHTML page to use h:panelGrid tag to places JSF components in 3 columns layout.
<?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:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:body>
<h:form>
<h:panelGrid columns="3">
Enter a number :
</h:panelGrid>
</h:form>
</h:body>
</html>
<body>
<h1>JSF 2 panelGrid example</h1>
<table>
<tbody>
<tr>
<td>
Enter a number :
</td>
<td>
<input id="j_idt6:number" type="text"
name="j_idt6:number" value="0" size="20" />
</td>
<td></td>
</tr>
</tbody>
</table>
</html>
3. Demo
Screen shot of this example.
In JSF, you can output message via following two messages tags :
1.
2.
See following JSF 2.0 example to show the use of both h:message and h:messages tags to
display the validation error message.
JSF page
<?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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h:form>
<br />
<h:panelGrid columns="3">
size="20" required="true"
label="Age" >
<f:validateLongRange for="age" minimum="1"
maximum="200" />
</h:inputText>
</h:panelGrid>
</h:form>
</h:body>
</html>
n JSF, f:param tag allow you to pass a parameter to a component, but its behavior is different
depends on which type of component its attached. For example,
1. f:param + h:outputFormat
If attach a f:param tag to h:outputFormat, the parameter is specifies the placeholder.
<h:outputFormat value="Hello,{0}. You are from {1}.">
<f:param value="JSF User" />
<f:param value="China" />
</h:outputFormat>
Heres the output Hello JSF User. You are from China.
In user bean, you can get back the parameter value like this :
Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
1. Managed Bean
A simple managed bean.
UserBean.java
package com.mkyong;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
FacesContext fc = FacesContext.getCurrentInstance();
this.country = getCountryParam(fc);
return "result";
}
Map<String,String> params =
fc.getExternalContext().getRequestParameterMap();
return params.get("country");
2. JSF Page
Two JSF pages for demonstration.
default.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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h:form id="form">
<h:commandButton id="submitButton"
value="Submit - US" action="#{user.outcome}">
</h:commandButton>
</h:form>
</h:body>
</html>
result.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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h3>
<h:outputFormat value="Hello,{0}. You are from {1}.">
<f:param value="#{user.name}" />
<f:param value="#{user.country}" />
</h:outputFormat>
</h3>
</h:body>
</html>
3. Demo
Enter your name, e.g mkyong, and click on the button.
Display the formatted message, name from user input, country from button parameter.
In JSF, f:attribute tag allow you to pass a attribute value to a component, or a parameter to a
component via action listener. For example,
1. Assign a attribute value to a component.
<h:commandButton">
<f:attribute name="value" value="Click Me" />
</h:commandButton>
//is equal to
nickname =
(String)event.getComponent().getAttributes().get("username");
1. Managed Bean
A managed bean named user, with an action listener method.
package com.mkyong;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
nickname =
(String)event.getComponent().getAttributes().get("username");
2. JSF Page
JSF pages to show the use of f:attribute tag to pass a attribute value to a h:commandButton
component.
default.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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h:form id="form">
<h:commandButton action="#{user.outcome}"
actionListener="#{user.attrListener}" >
</h:commandButton>
</h:form>
</h:body>
</html>
result.xhtml
<h:body>
#{user.nickname}
</h:body>
</html>
3. Demo
Heres the result.
In JSF, f:setPropertyActionListener tag is allow you to set a value directly into the property of
your backing bean. For example,
<h:commandButton action="#{user.outcome}" value="Submit">
In above JSF code snippets, if the button is clicked, it will set the mkyong value to the username
property viasetUsername() method.
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
1. Managed Bean
A super simple managed bean named user.
package com.mkyong;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
2. JSF Page
JSF page to show the use of f:setPropertyActionListener to set a value mkyong directly into the
property username of your backing bean.
default.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:f="http://java.sun.com/jsf/core"
>
<h:body>
<h:form id="form">
<f:setPropertyActionListener target="#{user.username}"
value="mkyong" />
</h:commandButton>
</h:form>
</h:body>
</html>
result.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"
>
<h:body>
#{user.username}
</h:body>
</html>
3. Demo
Heres the result after button is clicked.
In JSF, h:dataTable tag is used to display data in a HTML table format. The following JSF 2.0
example show you how to use h:dataTable tag to loop over an array of order object, and display
it in a HTML table format.
1. Project Folder
Project folder structure of this example.
2. Managed bean
A managed bean named order, initialized the array object for later use.
OrderBean.java
package com.mkyong;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="order")
@SessionScoped
public class OrderBean implements Serializable{
return orderList;
String orderNo;
String productName;
BigDecimal price;
int qty;
this.orderNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
}
3. CSS
Create a CSS file to style the table layout.
table-style.css
.order-table{
border-collapse:collapse;
}
.order-table-header{
text-align:center;
background:none repeat scroll 0 0 #E5E5E5;
border-bottom:1px solid #BBBBBB;
padding:16px;
}
.order-table-odd-row{
text-align:center;
background:none repeat scroll 0 0 #FFFFFFF;
border-top:1px solid #BBBBBB;
}
.order-table-even-row{
text-align:center;
background:none repeat scroll 0 0 #F9F9F9;
border-top:1px solid #BBBBBB;
}
4. h:dataTable
A JSF 2.0 xhtml page to show the use of h:dataTable tag to loop over the array of order object.
This example should be self-explanatory.
default.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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css"
/>
</h:head>
<h:body>
<h:column>
<!-- column header -->
<f:facet name="header">Order No</f:facet>
<!-- row record -->
#{o.orderNo}
</h:column>
<h:column>
<f:facet name="header">Product Name</f:facet>
#{o.productName}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{o.price}
</h:column>
<h:column>
<f:facet name="header">Quantity</f:facet>
#{o.qty}
</h:column>
</h:dataTable>
</h:body>
</html>
<table class="order-table">
<thead>
<tr>
<th class="order-table-header" scope="col">Order No</th>
<th class="order-table-header" scope="col">Product Name</th>
<th class="order-table-header" scope="col">Price</th>
<th class="order-table-header" scope="col">Quantity</th>
</tr>
</thead>
<tbody>
<tr class="order-table-odd-row">
<td>A0001</td>
<td>Intel CPU</td>
<td>700.00</td>
<td>1</td>
</tr>
<tr class="order-table-even-row">
<td>A0002</td>
<td>Harddisk 10TB</td>
<td>500.00</td>
<td>2</td>
</tr>
<tr class="order-table-odd-row">
<td>A0003</td>
<td>Dell Laptop</td>
<td>11600.00</td>
<td>8</td>
</tr>
<tr class="order-table-even-row">
<td>A0004</td>
<td>Samsung LCD</td>
<td>5200.00</td>
<td>3</td>
</tr>
<tr class="order-table-odd-row">
<td>A0005</td>
<td>A4Tech Mouse</td>
<td>100.00</td>
<td>10</td>
</tr>
</tbody>
</table>
</body>
</html>
6. Demo
URL : http://localhost:8080/JavaServerFaces/default.xhtml