JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







JSF Convertors
  • Convertors are used to make sure the component value is of right type.
  • When the user inputs value to the component, it is simple string value. Now you may be in the need of using this value as a different object like Boolean, Date etc.
  •  Converters can help in this conversion.
  • JSF framework has provided many converters like Boolean Converter, Byte Converter, Number Converter etc. 
  • These converters convert values into appropriate type of object and returns it also to the page in the appropriate format.

Standard Converters

  • JSF provides standard converters for all basic types like: Byte, Short, Integer,
    Long, Float, Double, BigDecimal, BigInteger, Boolean and Character.
  •  If you don't specify the converter for the component then JSF itself will apply the
    appropriate converter for the component. For example, if the component is
    associated with a property of type double, JSF will choose the Double converter.

DateTimeConverter

  • If you want to convert a component value to a Date type then <f:convertDateTime/>
    tag can be used inside the component tag.
  • It has many attribute like dateStyle, locale, pattern, timeStyle, timeZone etc which can be used to specify the format and type of the data.
Example:In this example, <f:convertDateTime> ensures the date is convertible into a Date object
of the format dd/MM/yyyy (day/month/year).
<h:inputText id="date" value="#{ManagedBean.date}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:inputText>
<h:message for="date" style="color:RED"/>

NumberConverter
  • If you want to convert a component value to a Number type then <f:convertNumber/> tag can be used inside the component tag. 
  • It has many attribute like integerOnly, maxIntegerDigits, currencyCode,
    currencySymbol, pattern etc.
Example:The following example displays only the integer part and only 2 digits of the value.
<h:outputText value="#{ManagedBean.number}">
<f:convertNumber type="number" integerOnly="true" maxIntegerDigits="2"/>
</h:outputText>

Example 2:
The following example takes the input value with $ symbol and displays again the value with $ symbol.
<h:inputText id="salary" value="#{ManagedBean.salary}">
<f:convertNumber currencySymbol="$" type="currency"/>
</h:inputText><br>
<h:message for="salary" style="color:RED"/>
<h:outputText value="#{ManagedBean.salary}">
<f:convertNumber currencySymbol="$" type="currency"/>
</h:outputText>
Example:-

index.jsp
<html>
   <%taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
   <f:view>
      <head>
         <link href="styles.css" rel="stylesheet" type="text/css"/>
         <title><h:outputText value="#{msgs.title}"/></title>
      </head>
      <body>
         <h:form>
            <h1><h:outputText value="#{msgs.enterPayment}"/></h1>
            <h:panelGrid columns="3">
               <h:outputText value="#{msgs.amount}"/>
               <h:inputText id="amount" label="#{msgs.amount}" 
                     value="#{payment.amount}"
                  <f:convertNumber minFractionDigits="2"/> 
               </h:inputText>
               <h:message for="amount" styleClass="errorMessage"/>

               <h:outputText value="#{msgs.creditCard}"/>
               <h:inputText id="card" label="#{msgs.creditCard}"
                     value="#{payment.card}"/>
               <h:panelGroup/>

               <h:outputText value="#{msgs.expirationDate}"/>
               <h:inputText id="date" label="#{msgs.expirationDate}"
                     value="#{payment.date}">                
                  <f:convertDateTime pattern="MM/yyyy"/>
               </h:inputText>
               <h:message for="date" styleClass="errorMessage"/>
            </h:panelGrid>
            <h:commandButton value="#{msgs.process}" action="process"/>
         </h:form>
      </body>
   </f:view>
</html>


PaymentBean.java
package myjsf;

import java.util.Date;

public class PaymentBean {
   private double amount;
   private String card = "";
   private Date date = new Date();


   public void setAmount(double newValue) { amount = newValue; }
   public double getAmount() { return amount; }


   public void setCard(String newValue) { card = newValue; }
   public String getCard() { return card; }

   
   public void setDate(Date newValue) { date = newValue; }
   public Date getDate() { return date; }
}


messages.properties
title=An Application to Test Data Conversion
enterPayment=Please enter the payment information
amount=Amount
creditCard=Credit Card
expirationDate=Expiration date (Month/Year)
process=Process
paymentInformation=Payment information

result.jsp
<html>
   <%taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
   <f:view>
      <head>
         <link href="styles.css" rel="stylesheet" type="text/css"/>
         <title><h:outputText value="#{msgs.title}"/></title>
      </head>
      <body>
         <h:form>
            <h1><h:outputText value="#{msgs.paymentInformation}"/></h1>
            <h:panelGrid columns="2">
               <h:outputText value="#{msgs.amount}"/>
               <h:outputText value="#{payment.amount}"
                  <f:convertNumber type="currency"/>
               </h:outputText>

               <h:outputText value="#{msgs.creditCard}"/>
               <h:outputText value="#{payment.card}"/>

               <h:outputText value="#{msgs.expirationDate}"/>
               <h:outputText value="#{payment.date}"
                  <f:convertDateTime pattern="MM/yyyy"/>
               </h:outputText>
            </h:panelGrid>
            <h:commandButton value="Back" action="back"/>
         </h:form>
      </body>
   </f:view>
</html>

Output:-









Download Source With lib
Download Source Without lib

Custom Converter

  • JSF flexible architecture provides you freedom to create your own
    converters. These can be used to check the value in the correct format.
Binding Component

Binding Component value to a Bean Property
  • JavaBeans are used to associate the UI components.
  • This backing bean (JavaBean) contains properties that can be bound to
    component value.
  • This binding requires use of Expression Language (EL) syntax.
  • A component value can be bound to the bean property by referencing
    the property in the “value" attribute of the tag associated with the component.
  • The property of the bean must accept and return the same component
    type as the value of the bound component.
Exam
  • Input text component uname’s value is bound to username property of the UserBean.java
  • Input text component pwd’s value is bound to the password property of the UserBean.java
<h:inputText id="uname" required="true" value="#{UserBean.username}"/>
<h:inputSecret id="pwd" required="true" value="#{UserBean.password}"/>


UserBean.java
package myjsf;

public class UserBean {
public UserBean(){
}
String username;
String password;
public String getUsername()
 {
return username;
 
}
public void setUsername(String username)
{

this
.username = username;
}

public String getPassword()
 { 
return password;
 
}
public void setPassword(String password)
 {

this
.password = password; 

Binding Component Value to an Implicit Object
  • JSF provides list of implicit objects. These objects can be referred in value attribute of
    component in the JSP page.
applicationScope :-Represents a Map of application scope attribute values with attribute
names as keys
cookie :- Represents a Map of cookie values for the current request with cookie
names as keys
facesContext:- Represents the FacesContext instance for the current request
header:-  Represents a Map of HTTP header values for the current request with
header names as keys
headerValues:-  Represents a Map of String arrays containing all the header values
for HTTP headers in the current request with header names as keys
initParam:-  Represents a Map of the context initialization parameters
param:-  Represents a Map of the request parameters for this request with
parameter names as keys
paramValues:-  Represents a Map of String arrays containing all the parameter values
for request parameters in the current request with parameter names as
keys
requestScope:-  Represents a Map of the request attributes for this request with
attribute names as keys
sessionScope:- Represents a Map of the session attributes for this request with
attribute names as keys
view:- Represents The root UIComponent in the current component tree
stored in the FacesRequest for this request