Main Tutorials

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;

@ManagedBean(name="user")
@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.

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">

    <h:body>
    	<h1>JSF 2.0 h:outputText Example</h1>
    	<ol>
    	   <li>#{user.text}</li>
    	
 	   <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>
	 </ol>
    </h:body>
</html>

Generate following HTML code…


<!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">
   <body> 
    	<h1>JSF 2.0 h:outputText Example</h1> 
    	<ol> 
    	   <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> 
	</ol>
   </body> 
</html>
  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.

Note
See complete list of sensitive HTML and XML markup here…
http://www.ascii.cl/htmlcodes.htm

3. Demo

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

jsf2-outputtext-example

Download Source Code

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

Reference

  1. JSF <h:outputText /> JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
DougMH
1 year ago

Before I retired, I know there was a way to tell my .xhtml files that their outputText would be drawn from an external text file… but I cannot remember how I did it. I somehow indicated the text file at the top of the .xhtmlfile then instead of putting say “Login Name:” in an h:outputText I indicated the file and key of the external file. Have you or anyone done that? Mades it tremendously easy to change text for any .xhtml form file from one location

Walter L.
5 years ago

Wouldn’t #4 in the generated HTML output be:

<li>&lt;input type=’text’ size=’20’ /&gt </li>

if it’s going to be rendered literally?

balachander s
9 years ago

Dear all,
I need to implement comment box and reply for the comment in my (lab assignment’s) website using jsf or primefaces.
Please help me if you have some ideas.

Felipe Huggler
9 years ago

Hi. I have a include . I need print in my page the src of the component. Sample: mycomponente. Is possible?

rajesh
10 years ago

As value I added html content generated by birt report . it works fine but when if update it the change is not reflected.

Janak
11 years ago

Hi,
Nice thing…but what will you say for preventing XSS.
escape : true or false??

Ninja
11 years ago
Reply to  Janak

It does not work when I use MyFaces implementation.

Apeksh
10 years ago
 You are an Idiot