Magazine
 
JSF Application
 
return sObject;
}
catch (Exception exception) {
throw new
ConverterException(exception);
}
}
public String getAsString(FacesContext
facesContext,
UIComponent
uiComponent,
Object obj) {
try {
int total_seconds =
(int)((Integer)obj).intValue();
int hours=(total_seconds)/(60*60);
int rem=(total_seconds)%(60*60);
int minutes=rem/60;
int seconds=rem%60;
String str_hours=””+hours;
String str_minutes=””+minutes;
String str_seconds=””+seconds;
if(hours<10){
str_hours=”0"+hours;
}
if(minutes<10){
str_minutes=”0"+minutes;
}
if(seconds<10){
str_seconds=”0"+seconds;
}
return str_hours + “:” + str_minutes
+ “:” + str_seconds;
}
catch (Exception exception) {
throw new
ConverterException(exception);
}
}
}

In this class “param” represents the string provided by the user in the component. This string is passed to the getAsObject() method. Now we can use this according to our requirement of manipulation and return the appropriate object. “obj” parameter passed in getAsString() method represents the

 

converted object in the previous method. This method is called while displaying in the page. So return the appropriate String by manipulating this object. If there is any problem in this process we can handle it by try and catch block. An error message is shown to the current page if conversion is not successful.

Step2: Configure the configuration file (faces-config.xml). Open this file and add the following code.

<?xml version=”1.0"?>
<faces-config>
<converter>
<converter-id>TimeConverter</converterid>
<converter-class>TimeConverter</
converter-class>
</converter>
</faces-config>

Here <converter-id> gives ID to the converter that will be used in our page and <converter-class> specifies the
implementing class.

Step3: Now, we can code for the page “converter.jsp” where <f:converter> tag is used to associate the converter to the component using converter Id attribute.
Value of this attribute is matched with the ID of the converter specified in the configuration file.

converter.jsp:

<%@ taglib uri=”http://java.sun.com/jsf/
core” prefix=”f” %>
<%@ taglib uri=”http://java.sun.com/jsf/
html” prefix=”h” %>
<f:view>
<html>
<body>
<h:form>
<b>Please enter the time below :</b><br>
</br>
<h:inputText id=”time”>

 

Dec 2007 | Java Jazz Up |63
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

Download PDF