Advertisement

07.16.2007 at 04:40AM PDT, ID: 22698345
[x]
Attachment Details

JSF custom converter - Calendar to String

Asked by paulmaker in Java Server Faces (JSF), IBM Websphere Application Server

Tags: jsf, calendar, converter, string, custom

I am using Java Server Faces to develop a dynamic application form. (developing on RAD/Websphere).
I have a 'Customer' backing bean with a dateOfBirth field of type java.util.Calendar
I have a problem where I have written a custom converter to convert a String input to Calendar object and this seems to work ok. The problem arises when the form fails validation. The field displays back the full Calendar object instead of displaying as a String. Do I need to change my converter or the way I call the value in the backing bean?

Converter code:-
public class DateConverter implements Converter {
      
      public Object getAsObject(FacesContext context, UIComponent component, String value) {
          if (StringUtility.isStringEmpty(value)){ return null;}

          String sDate = value.replaceAll("[a-z,A-Z, ,-,,/]+","");
          String day = sDate.substring(0,2);
          String month = sDate.substring(2,4);
          String year = sDate.substring(4);
          Calendar dateOfBirth = Calendar.getInstance();
          dateOfBirth.set(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
          
          return dateOfBirth;
      }
      
      public String getAsString(FacesContext context, UIComponent component, Object value) {
            return value.toString();
      }
}Start Free Trial
 
Loading Advertisement...
 
[+][-]07.16.2007 at 06:16AM PDT, ID: 19495163

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.16.2007 at 12:14PM PDT, ID: 19711424

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08.21.2007 at 02:43PM PDT, ID: 19741858

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Server Faces (JSF), IBM Websphere Application Server
Tags: jsf, calendar, converter, string, custom
Sign Up Now!
Solution Provided By: Computer101
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32