JSF Tutorial - JSF OutputFormat Example








The h:outputFormat tag renders an HTML text but can accept parameterised inputs.

The following JSF code

<h:outputFormat value="parameter 1 : {0}, parameter 2 : {1}" >
   <f:param value="Item 1" />
   <f:param value="Item 2" />
</h:outputFormat>

is rendered into the following HTML code.

parameter 1 : Item 1, parameter 2 : Item 2

Tag Attributes

AttributeDescription
idid for the tag
bindingReference to the component used in a backing bean
renderedA boolean value; false would suppress rendering
styleClassCascading stylesheet (CSS) class name
valuevalue binding
converterConverter class name
styleInline style information
titleA title used for accessibility. Browsers typically create tooltips for the title's value




Example

The following code is from UserBean.java.

package com.java2s.common;

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

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

  public String text = "Hello {0}";
  public String htmlInput = "<a href='http://java2s.com'>{0}</a>";
  
  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;
  }
  
}

The following code is from demo.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>
        <h:outputFormat value="this is param 0 : {0}, param 1 : {1}" >
         <f:param value="Number 1" />
         <f:param value="Number 2" />
       </h:outputFormat>
     <br/>
       <h:outputFormat value="#{user.text}" >
         <f:param value="java2s.com" />
       </h:outputFormat>
    <br/>
      <h:outputFormat value="#{user.htmlInput}" >
         <f:param value="text" />
         <f:param value="size='30'" />
       </h:outputFormat>
    <br/>
      <h:outputFormat value="#{user.htmlInput}" escape="false" >
         <f:param value="text" />
         <f:param value="size='30'" />
       </h:outputFormat>
    <br/>
      <h:outputFormat value="#{user.htmlInput}" escape="false" >
         <f:param value="button" />
         <f:param value="value='Click Me'" />
       </h:outputFormat>
    </h:body>
</html>


Download OutputFormat.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml