Custom Converters in JSF

Custom Converters in JSF
JSF has Standart Converters for numbers and dates. In addition to that you can also create custom converts in JSF.(you can find Standard Converters here)

Creating Custom Converters:
1.
Create a java class implementing Converter Interface and provide implementation of these 2 methods

  • Object getAsObject(FacesContext context, UIComponent component, String value)
  • String getAsString(FacesContext context, UIComponent component, Object value)

2. Register the Custom Converter class
3. Associate Custom Converter with the UI

Implementing 2 Methods of Converter Interface:
Object getAsObject(FacesContext context, UIComponent component, String value)

  • The faces context represents the context of UI component which conversion is to be applied
  • UI component refers to the component to which converter to be added
  • String refers to the User input

String getAsString(FacesContext context, UI Component component, Object value)

Accepts the instance of FacesContext, UI Component and Object returned from getAsObject method.
This method converts the object to string to be displayed on a view

Registering Custom Converter class:
There are 2 ways
1. Register in faces.config.xml
<converter>
<converter-id>CConverter</converter-id>
<converter-class>CustomConverter</converter-class>
</converter>

2 Use Annotations:
@FacesConverter(value=”CConverter”)

Lets us consider, a customer enters his credit-card number.
Lets convert the credit card number in the format of 1111-2222-3333-4444.
Suppose if he enters the number less than 16 digits a message to be displayed.

JSF Managed Bean – creditnumber.java

CustomConverter.java

 

Custom.xhtml

show.xhtml

Output:
out1
If entered Correctly,
out2

 

 

 

 

 

 

 

 

Read More

Standard Converters in JSF (convertNumber and convertDateTime)

Standard Converters in JSF (convertNumber and convertDateTime)

The standard converters are built-in converters in JSF Framework. These converters are usually associated with the User Interface components with the help of tags that are available at http://java.sun.com/jsf/core.

There are 2 types of Standard Converters in JSF:

  • convertNumber
  • convertDateTime

convertNumber:

Attribute
Example
Currency Code < c:convertNumber type=”currency” currencyCode=”NZD” >
Currency Symbol < c:convertNumber type=”currency” currencySymbol=”$” >
maxFractionDigits < c:convertNumber maxFractionDigits=”3″ >
minFractionDigits < c:convertNumber minFractionDigits=”1″ >
maxIntegerDigits < c:convertNumber maxIntegerDigits=”3″ >
minIntegerDigits < c:convertNumber minIntegerDigits=”3″ >
integerOnly < c:convertNumber integerOnly=”true” >
pattern < c:convertNumber pattern=”#000.00″ >
type < c:convertNumber type=”percent” > (types- type=number,percent,currency)

convertDateTime

Attribute Example
type < c:convertDateTime type=”time” >
dateStyle < c:convertDateTime dateStyle=”long” > (types-short,medium,long,full)
timeStyle < c:convertDateTime type=”time” timeStyle=”long” >
pattern < c:convertDateTime pattern=”dd-MM-yy” > (types-short,medium,long,full)

Example:
converter.xhtml

Output:
output1

When date entered wrong,
output2

 

Read More

JSF Example (Simple JSF Application)

JSF Example (Simple JSF Application)

Here is a code for Simple JSF Application, Getting input from the User and displaying it to User. This application is tested in Netbeans 8.0.2.

Files to be Created:
input.xhtml (JSF page)
show.xhtml (JSF page)
Student.java (JSF Managed Bean)

Student.java

input.xhtml

show.xhtml (Displaying to the user)

 

Read More