jsf

OutputFormat Example with JSF 2.0

Hi there, have a nice and proactive week! Today we ‘ll extend a little bit our last example, just to make sure that we do make clear the difference between <h:outputText /> and <h:outputFormat />.

Generally, the fore-mentioned JSF tags are similar, but the one that we ‘re going to talk today about, provides some extra functionality that enables a rendering of a parameterized message.
 
 
 
 
 
 

 
For example,

<h:outputFormat value="param0 : {0}, param1 : {1}" >
 	<f:param value="Java Core" />
 	<f:param value="Java Enterprise Edition" />
</h:outputFormat>

will output something like param0 : Java Core, param1 : Java Enterprise Edition.

1. Managed Bean

Now that we ‘re done with a general intro, let’s get into a more specific example. Here is the structure of our classic managed bean, which will provide some demonstration text:

UserBean.java

package com.javacodegeeks.enterprise.jsf.outputformat;

import java.io.Serializable;
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 {0}" ;
	public String htmlInput = "" ;
	
	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. View 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 OutputFormat Example</h1>
    	<ol>
    		<li>
    			<h:outputFormat value="Hello from param 0 : {0}, param 1 : {1}">
    				<f:param value="1st sample parameter" />
    				<f:param value="2nd sample parameter" />
    			</h:outputFormat>
    		</li>
    		<li>
				<h:outputFormat value="#{user.text}">
					<f:param value="Thodoris Bais" />
				</h:outputFormat>
			</li>
			<li>
				<h:outputFormat value="#{user.htmlInput}">
					<f:param value="text" />
					<f:param value="size='30' " /> 
				</h:outputFormat>
			</li>
			<li>
				<h:outputFormat value="#{user.htmlInput}" escape="false">
					<f:param value="text" />
					<f:param value="size='30' " /> 
				</h:outputFormat>
			</li>
			<li>
				<h:outputFormat value="#{user.htmlInput}" escape="false">
					<f:param value="button" />
					<f:param value="value='Click me' " />
				</h:outputFormat>
			</li>
		</ol>	
    </h:body>
</html>

This will generate the following HTML format:

<?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 OutputFormat Example</h1>
    	<ol>
    		<li>
	            this is param0 : 1st sample parameter, param1 : 2nd sample parameter
 	        </li> 
 	        <li>
	            Hello Thodoris Bais
 	        </li> 
	        <li>
	            <input type="text" size='30' />
	        </li> 
	        <li>
	            <input type="text" size='30' /> 
	        </li> 
	        <li>
	            <input type="button" value='Click Me' /> 
	        </li> 
        </ol>	
    </h:body>
</html>

3. Demo

Let’s have a quick look at our final product:

img

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

Thodoris Bais

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button