Home » Enterprise Java » jsf » OutputText Example with JSF 2.0

About Thodoris Bais

Avatar photo
Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics & Telecommunications Engineering and is interested in continuous development.

OutputText Example with JSF 2.0

Hi there, pretty short time since my last example! I’ ll firstly try to give a short explanation about the connection of my last example with this. So, let’s give it a try!

The reason that we ‘re now won’t have a full example of multiple selectable dropdown list, as we used to in the last few examples, is that the nested elements are not displayed consistently in different browsers.

We could of use the <h:selectManyMenu /> to render a multiple selectable dropdown list, but just take a look of how this JSF element could be rendered across Internet Explorer, Mozilla Firefox and Google Chrome. That is, this case is one of the worst nightmares of a developer, so please avoid using it.

Back to this example and according to output text in JSF, I’ll try to get you deep into the meaning of it, by showing a multiple case example.

The useable tag is obvious as you might thought, just give a tag of <h:outputText /> to render an output text element.

1. Managed Bean

A demonstration bean to contain two sample strings:


package com.javacodegeeks.enterprise.jsf;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

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

public class UserBean implements Serializable{

	private static final long serialVersionUID = 4256272866128337548L;

	public String text = "Hello Java Code Geeks!" ;
	public String htmlInput = "<input type='text' size='20' /> " ;
	public String getText() {
		return text;
	public void setText(String text) {
		this.text = text;
	public String getHtmlInput() {
		return htmlInput;
	public void setHtmlInput(String htmlInput) {
		this.htmlInput = htmlInput;

2. Our JSF Page


<?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"    
      xmlns:f="http://java.sun.com/jsf/core" >
    	<h1>JSF 2.2 OutputText Example</h1>
    		<li><h:outputText value="#{user.text}" /></li>
    		<li><h:outputText value="#{user.text}" styleClass="sampleClass" /></li>
    		<li><h:outputText value="#{user.htmlInput}" /></li>
    		<li><h:outputText value="#{user.htmlInput}" escape="false" /></li>

And if you didn’t make it so clear, this is what is being generated to HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml">
    	<h1>JSF 2.2 OutputText Example</h1> 
    	   <li>Hello Java Code Geeks!</li> 
           <li>Hello Java Code Geeks!</li> 
           <li><span class="sampleClass">Hello Java Code Geeks!</span></li> 
           <li><input type='text' size='20' /></li> 
           <li><input type='text' size='20' /></li> 

And a little bit of more analysis:

  1. Cases 1 and 2: we don’t have to really use the h:outputText tag, since this can be achieved using the value expression "#{user.text}".
  2. Case 3: if we have any tag of kind styleClass, style, dir or lang, just render the text and wrap it around a span element.
  3. Case 4 and 5: we use the escape attribute in h:outputText tag in order to convert sensitive HTML and XML markup to the corresponding valid HTML characters (i.e. “<” is converted to “<“); escape attribute is set to true by default.

3. Demo


This was an example of ListBox in JSF 2.0. You can also download the source code for this example: OutputTextJSF

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments