JSF 2 OutputText Example

JavaServer Faces (JSF 2) provides a number of user interfaces components that cover the most common requirements, one of the most important component is a <h:outputText/> component. The outputText is a component that has a value, optionally retrieved from a model tier bean via expression, that is displayed to the user. The user cannot directly modify the rendered value; it is for display purpose only. If you are looking for the configuration of complete application, please read our JSF 2 Setup Tutorial.

Also Read:

1. Managed Bean

The following lines of code should provide you the proper ManagedBean for containing the greeting message, that is the message shall be displayed several times using the outputText component. That’s not the all things that you would see in this example; also, the outputText can be used for displaying a text containing a HTML tags.

package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

public class UserBean {
	private String userHelloWords = "Hello World Message !";
	private String userHelloHTMLWords = "<h2>Hello World Message ! Contains HTML Tags</h2>";

	public String getUserHelloHTMLWords() {
		return userHelloHTMLWords;

	public void setUserHelloHTMLWords(String userHelloHTMLWords) {
		this.userHelloHTMLWords = userHelloHTMLWords;

	public String getUserHelloWords() {
		return userHelloWords;

	public void setUserHelloWords(String userHelloWords) {
		this.userHelloWords = userHelloWords;
  • The UserBean managed bean provides two types of messages; one is a plain text and the other is a text containing a HTML tags.

2.The Views

The page that you would see contains different uses of <h:outputText/>; as you will note the some messages is a plain Text while the other uses a HTML tags inside it.

index.xhtml view

<html xmlns="http://www.w3.org/1999/xhtml"
	<title>JavaBeat JSF OutputText</title>
			<h:outputText value="JavaBeat JSF 2.2 Examples" />
		<br />
				value="OutputText Tag used for diaplying values That Provided By : " />
			<br />
			<h:outputText value=" " escape="false" />
			<h:outputText value="Dyanmically Using Managed Beans" />
			<h:outputText value="  &amp;  " escape="false" />
			<h:outputText value="Statically By Using an Inline Text" />
		<br />
				value="Also, it could be used for displaying a text containing a HTML Tags Located at the Managed Beans"
				escape="false" />
		<br />
			value="Managed Greeting Message Is : #{userBean.userHelloWords}" />
		<br />
			value="Managed Greeting Message Containg HTML Is : #{userBean.userHelloHTMLWords}"
			escape="false" />
  • Once you would be displaying a messages containing HTML Tags, you should use the property escape that provided by the <h:outputText/>.
  • The escape property can take two values; true or false, the true value indicates that the HTML tags inside the component should be discarded, while the false is an indication of being the HTML Tags are considered.
  • The outputText component is able to accept values from the managed beans; by using the value property that could have an expression connected to a value in a certain managed bean or by type an inline message within it.

3. JSF 2 OutputText Demo

The below snapshots will show you a running example.

JSF 2 OutputText Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!