Sometimes when you develop application to the customer he want to see data in specific format although it stored in the database in other format for example:
Assume you have a table store mobile number in this format 3538xxxxxxxx but the user want to see this mobile number in this format 08xxxxxxxx.
It means when you read the mobile from database (in this format 3538xxxxxxxx) you should convert it (to 08xxxxxxxx) before displayed in the screen.
and vise versa when the user enter mobile number in this format 08xxxxxxxx it should store in the database in this format 3538xxxxxxxx
To make this follow this steps:
1- Create java class implements javax.faces.convert.Converter (e.g MobileNumberConverter.java)
2- Open faces-config.xml and go to Converters and create new converter
3- Go to inputText component and insert inside it f:converter then select your custom converter (MobileConverter)
You can make your own converter depending on your business need like making convert for dates sometimes customer need to enter date in specific format and store in database in other format.
Assume you have a table store mobile number in this format 3538xxxxxxxx but the user want to see this mobile number in this format 08xxxxxxxx.
It means when you read the mobile from database (in this format 3538xxxxxxxx) you should convert it (to 08xxxxxxxx) before displayed in the screen.
and vise versa when the user enter mobile number in this format 08xxxxxxxx it should store in the database in this format 3538xxxxxxxx
To make this follow this steps:
1- Create java class implements javax.faces.convert.Converter (e.g MobileNumberConverter.java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.faces.application.FacesMessage; | |
import javax.faces.component.UIComponent; | |
import javax.faces.context.FacesContext; | |
import javax.faces.convert.Converter; | |
import javax.faces.convert.ConverterException; | |
public class MobileNumberConverter implements Converter | |
{ | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) // set | |
{ | |
if ( string == null || string.trim().equals("") ) | |
{ | |
return null; | |
} | |
if(string.length() != 10) | |
{ | |
FacesMessage message = new FacesMessage("You have to enter mobile number in this format [089xxxxxxx]."); | |
message.setSeverity(FacesMessage.SEVERITY_ERROR); | |
throw new ConverterException(message); | |
} | |
String number = string.substring(1, 10); | |
number = "353" + number; | |
return number; | |
} | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) // get | |
{ | |
if ( object == null ) | |
{ | |
return ""; | |
} | |
String obj = object.toString(); | |
String number = obj.substring(3, 12); | |
number = "0" + number; | |
return number; | |
} | |
} |
2- Open faces-config.xml and go to Converters and create new converter
3- Go to inputText component and insert inside it f:converter then select your custom converter (MobileConverter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<af:inputText value="#{row.bindings.CustomerMobile.inputValue}" | |
label="#{bindings.CustomersView11.hints.CustomerMobile.label}" | |
required="#{bindings.CustomersView11.hints.CustomerMobile.mandatory}" | |
columns="#{bindings.CustomersView11.hints.CustomerMobile.displayWidth}" | |
maximumLength="10" | |
shortDesc="Enter mobile in this format [089xxxxxxx]" | |
id="it3"> | |
<f:validator binding="#{row.bindings.CustomerMobile.validator}"/> | |
<f:converter converterId="MobileConverter"/> | |
</af:inputText> |
You can make your own converter depending on your business need like making convert for dates sometimes customer need to enter date in specific format and store in database in other format.
very helpfull post, its intresting....!!!!
ReplyDeleteMicrosoft Office 2016 Free Download Full Version With Product Key