JSF 2 outputText example

In JSF 2.0 web application, “h:outputText” tag is the most common used tag to display plain text, and it doesn’t generate any extra HTML elements. See example…

1. Managed Bean

A managed bean, provide some text for demonstration.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
public class UserBean{
	public String text = "This is Text!";
	public String htmlInput = "<input type='text' size='20' />";
	//getter and setter methods...

2. View Page

Page with few “h:outputText” tags example.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml"   
    	<h1>JSF 2.0 h:outputText Example</h1>
 	   <li><h:outputText value="#{user.text}" /></li>
	   <li><h:outputText value="#{user.text}" styleClass="abc" /></li>
	   <li><h:outputText value="#{user.htmlInput}" /></li>
	   <li><h:outputText value="#{user.htmlInput}" escape="false" /></li>

Generate following HTML code…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml">
    	<h1>JSF 2.0 h:outputText Example</h1> 
    	   <li>This is Text!</li> 
           <li>This is Text!</li> 
	   <li><span class="abc">This is Text!</span></li> 
	   <li>&lt;input type='text' size='20' /&gt;</li> 
	   <li><input type='text' size='20' /></li> 
  1. For case 1 and 2
    In JSF 2.0, you don’t really need to use “h:outputText” tag, since you can achieve the same thing with direct value expression “#{user.text}”.
  2. For case 3
    If any of “styleClass”, “style”, “dir” or “lang” attributes are present, render the text and wrap it with “span” element.
  3. For case 4 and 5
    The “escape” attribute in “h:outputText” tag, is used to convert sensitive HTML and XML markup to the corresponds valid HTML character.
    For example,

    1. < convert to &lt;
    2. > convert to &gt;
    3. & convert to &amp;

    By default, “escape” attribute is set to true.

See complete list of sensitive HTML and XML markup here…

3. Demo

URL : http://localhost:8080/JavaServerFaces/


Download Source Code

Download It – JSF-2-OutputText-Example.zip (9KB)


  1. JSF <h:outputText /> JavaDoc
Tags :

About the Author

Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.