JSF Converter Tags

Jsf is provided with in built converter to convert its UI Component Data to Object created in Managed Bean.

Convertes in JSF:

f:convertNumber: Converts a String into a Number of desired format.

Example to convert a String to a number in a specific format:

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>
      <h3>Jsf ConvertNumber Example</h3>
      <table border="1" cellspacing="1" cellpadding="1">
         <tr><th>Attribute</th><th>Value </th><th>Output</th></tr>
         <tr><td>minFractionDigits="2"</td><td>10.1</td>
         <td>
            <h:outputText value="10.1" >
               <f:convertNumber minFractionDigits="2" />
            </h:outputText>
         </td></tr>
         <tr><td>maxFractionDigits="2"</td><td>10.1678</td>
         <td>
            <h:outputText value="10.1678" >
               <f:convertNumber maxFractionDigits="2" />
            </h:outputText>
         </td></tr>
         <tr><td>pattern="#000.000"</td><td>10.1</td>
         <td>
            <h:outputText value="10.1" >
               <f:convertNumber pattern="#000.000" />
            </h:outputText>
         </td></tr>
         <tr><td>currencySymbol="$"</td><td>$10</td>
         <td>
            <h:outputText value="$10">
               <f:convertNumber currencySymbol="$" type="currency" />
            </h:outputText>
         </td></tr>
         <tr><td>type="percent"</td><td>10.1%</td>
         <td>
            <h:outputText value="10.1%" >
               <f:convertNumber type="percent" />
            </h:outputText>
         </td></tr>
      </table>
   </h:body>
</html>


Output:

jsf-converter-tags

f:convertDateTime:Converts a String into a Date of desired format

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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:body>

<h3>JSF convertDateTime example</h3>

<h:form>
    Enter Date : 
<h:inputText id="date" value="#{user.date}" size="20"
required="true" label="Date">
<f:convertDateTime pattern="dd-mm-yyyy" />
</h:inputText>

<h:message for="date" style="color:red" />

<h:commandButton value="Submit" action="output" />
<br></br>
<br></br>
Required Format:
<h:outputText value="09-12-2014" >
         <f:convertDateTime pattern="dd-mm-yyyy" />
      </h:outputText>

</h:form>

</h:body>
</html>

Bean:

import java.io.Serializable;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class User implements Serializable{
 
/**
* 
*/
private static final long serialVersionUID = 1L;
Date date;
 
public Date getDate() {
  return date;
}
 
public void setDate(Date date) {
  this.date = date;
}
}

Output page:

<?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"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      >
    <h:body>
 
    <h3>JSF convertDate Example</h3>
 
Date :  
<h:outputText value="#{user.date}" >
<f:convertDateTime pattern="dd-mm-yyyy" />
</h:outputText>
 
    </h:body>
</html>

Output:

jsf-converter-tags

Enter ankit and submit

jsf-converter-tags