JSF 2 h outputFormat

JSF 2 h outputFormat


Posted in : Java Posted on : July 31, 2012 at 11:03 AM Comments : [ 0 ]

In this tutorial you will learn about the JSF 2 html outputFormat tag.

JSF 2 h outputFormat

In this tutorial you will learn about the JSF 2 html outputFormat tag.

<h:outputFormat> tag generally renders the parameterised text. This tag is rendered also to the HTML <span> element if the style or styleClass attribute is used with this tag. If parameter of this tag is defined as a child UIParameter, it accrue a list of these parameter values and converted them ( if one or more accumulated parameter values ) to an Object array. Values of these parameters are passed as the first argument and the array of parameter values as the second argument.

Attributes of JSF h outputFormat

  • converter : This is an optional attribute. This attribute is used when you want to use any Converter class with this component.
  • id : This is an optional attribute. This attribute is used to make the component uniquely identifiable.
  • rendered : This is an optional attribute. This attribute evaluates a boolean expression. Presence of this attribute specifies that whether the current component should be rendered in the Rendered Response phase or not or worked on any later form submission. The default value of this attribute is defined to true.
  • value : This is an optional attribute. This attribute specifies the current value of the component.
  • dir : This is an optional attribute. This attribute evaluates to the java.lang.String value that suggests the direction for the text which doesn't inherit the property of maintaining a direction. The allowable values are "LTR (Left-to-Right)" and "RTL (Right-to-Left)".
  • escape : This is an optional attribute. This attribute evaluates a boolean expression which specifies whether the HTML, XML markup characters used in this tag will be escaped ( rendered to the HTML, XML markup characters) or not. The default value is defined to the 'true' i.e. the HTML, XML markup characters will must be escaped i.e. they will not be executed.
  • lang : This is an optional attribute. This attribute is used to specify the language code for the component.
  • style : This is an optional attribute. This attribute evaluates to a java.lang.String value, which specifies that the CSS style will be applied to this component when rendered.
  • styleClass : This is an optional attribute. This attribute evaluates to a java.lang.String value specifies the applicable CSS style space-separated list to applied at the time of rendering. "class" attribute must be used to pass this value.
  • title : This is an optional attribute. This attribute is used to specify the title of the component which will be displayed as tooltip when the mouse pointer is moved over the component.
  • binding : This is an optional attribute. This attribute evaluates to a javax.faces.component.UIComponent specified for linking the component with the property of backing bean.

Example :

Here I am giving a simple JSF example which will demonstrate you about how to use the JSF 2 h outputFormat tag in JSF applications. In this example I have created a very simple JSF view page where I have used the <h:outputFormat> tag with the 'escape' attribute and used the <f:param> tag as a child component to provide the value as parameter. And a JavaBeans named 'TextBean' to set and get the value.

Directory Structure

TextBean.java

package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="text")
@RequestScoped
public class TextBean {
public String text = "<b>Welcome to {0} {1}</b>";
public String value= "JSF Tutorial";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
} 
}

jsfOutputFormat.xhtml

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

<head>
<title>JSF 2 h outputFormat Example</title>
</head>
<h:body>
<f:view>
<h:outputFormat value="#{text.text}" escape="false">
<f:param value="Devmanuals" />
<f:param value="#{text.value}" />
</h:outputFormat>
</f:view>
</h:body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfHOutputFormat</display-name>
<welcome-file-list>
<welcome-file>jsfOutputFormat.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How To Run

After copying the above code paste them to the respective files or by downloading the code from Download Source Code start your web server and then use the following link http://localhost:8181/jsfHOutputFormat/jsfOutputFormat.xhtml in the address bar of the web browser.

Output

When you will execute the above code you will get the output as follows :

But, if you do not use the 'escape' attribute or escape='true' then the output will be as follows :

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics