JSF: Lesson – 5: Converting Data #Java Short Notes

  • Sample application for JSF Data Type Converter
  • Video Tutorial for JSF Data Type Converter
  • Pre-requisite: JSF Lesson 1, 2, 3, 4
  • JSF provides two different types of built-in data type converters such as: String and Date Conversion, Number and String Conversion.
  • Related tags: f:convertDateTime, f:convertNumber, f:converter
  • In our previous examples, we could accept departing and returning dates to be date/time types rather than string types [as expected]
  • How to achieve:
      
  • For other types of conversions, you have to use custom converters.
  • To make use of the strategy, you have to write a class that will do the conversion work.
  • Your class should implement: javax.faces.convert.Converter interface that has two methods that you must implement. One method takes input as String and returns an Object. Another method, takes an Object as the input and returns String.
  • Methods
    Object getAsObject(javax.faces.context.FacesContext context,   javax.faces.component.UIComponent component, java.lang.String value)String getAsString(javax.faces.context.FacesContext context,   javax.faces.component.UIComponent component, java.lang.Object value)
  • getAsObject: Returns an object
  • getAsString: Returns String
  • As you have access to the JSF context, and the corresponding user interface component in these methods- you will be able to greatly customize your converter
  • How to use the custome converter:
    • Define a converter in the configuration file (faces-config.xml) as follows where TerminalConverter is your custom converter class:
      terminal.converter  net.justetc.jsf.TerminalConverter
    • From JSPs refer to a converter-id as defined in the configuration file. See example below:
    • 		  	or	
    • You can also, define/use the converter for all properties of a class such as Terminal as follows:
        net.justetc.jsf.Terminal  net.justetc.jsf.TerminalConverter
    • In this last scenario, whenever JSF identifies a Terminal type data it converts the data using the TerminalConverter class.
         

      In this example, as it encounters bus.origin to be of Terminal type, it will convert the origin to be a Terminal object though the user will just enter a String.

From: http://sitestree.com/?p=4996
Categories:Java Short Notes
Tags:
Post Data:2011-05-14 09:49:51

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada