Thodoris Bais

About Thodoris Bais

Thodoris Bais is currently working as a Junior Java Developer while on parallel, pursuing a diploma at Informatics & Telecommunications Engineering. He specializes in Java Core, Android and J2EE and looks forward to being a successfull professional. He is interested in entrepreneurship and is also a quick learner and curious about new technologies.

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:

UserBean.java

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;

@ManagedBean
@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

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

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" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
   <body> 
    	<h1>JSF 2.2 OutputText Example</h1> 
    	<ol> 
    	   <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> 
	</ol>
   </body> 
</html>

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

img

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close