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;

@ManagedBean
@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"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
	<title>JavaBeat JSF OutputText</title>
</h:head>
<f:view>
	<h:form>
		<h1>
			<h:outputText value="JavaBeat JSF 2.2 Examples" />
		</h1>
		<br />
		<h2>
			<h:outputText
				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" />
		</h2>
		<br />
		<h3>
			<h:outputText
				value="Also, it could be used for displaying a text containing a HTML Tags Located at the Managed Beans"
				escape="false" />
		</h3>
		<br />
		<h:outputText
			value="Managed Greeting Message Is : #{userBean.userHelloWords}" />
		<br />
		<h:outputText
			value="Managed Greeting Message Containg HTML Is : #{userBean.userHelloHTMLWords}"
			escape="false" />
	</h:form>
</f:view>
</html>
  • 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

Comments

comments

About Amr Mohammed

Speak Your Mind

*