2 Replies Latest reply on May 7, 2008 10:59 AM by Rosario Nicolazzo

    rich:calendar and XMLGregorianCalendar converter

    Rosario Nicolazzo Newbie

      I'm trying to use a Converter to convert a XMLGregorianCalendar,
      when open page the conversion is ok and method getAsString is invoked
      but when post the request method getAsObject is not invoked and in jsf page view this message and not find errors in the logs and system out:

      insert date: An error occurred when processing your submitted information.

      this is my jsf code:

      <rich:calendar value="#{HomeBB.home.insertDate}" popup="true" label="insert date" datePattern="dd/MM/yyyy" showApplyButton="false"/>

      this is my faces-config:

      this is converter:
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.GregorianCalendar;
      import java.util.Locale;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.convert.Converter;
      import javax.faces.convert.ConverterException;
      import javax.xml.datatype.DatatypeFactory;
      import javax.xml.datatype.XMLGregorianCalendar;
      import org.richfaces.component.UICalendar;
      public class XMLGregorianCalendarConverter implements Converter {
       public Object getAsObject(FacesContext context, UIComponent component, String value) {
       System.out.println("invoke getAsObject");
       GregorianCalendar cal = null;
       XMLGregorianCalendar calendar = null;
       if (value != null && value.length() > 0) {
       Locale locale = (Locale) ((UICalendar) component).getLocale();
       String pattern = ((UICalendar) component).getDatePattern();
       DateFormat df = new SimpleDateFormat(pattern, locale);
       try {
       Date date = df.parse(value);
       cal = new GregorianCalendar();
       calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
       } catch (Exception e) {
       throw new ConverterException(e);
       return calendar;
       public String getAsString(FacesContext context, UIComponent component, Object value) {
       System.out.println("invoke getASString");
       String result = null;
       if (value != null) {
       if (value instanceof String) {
       result = (String) value;
       } else {
       DateFormat df;
       if (component instanceof UICalendar) {
       Locale locale = (Locale) ((UICalendar) component).getLocale();
       String pattern = ((UICalendar) component).getDatePattern();
       df = new SimpleDateFormat(pattern, locale);
       } else {
       Locale locale = context.getViewRoot().getLocale();
       df = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
       Date date = ((XMLGregorianCalendar) value).toGregorianCalendar().getTime();
       result = df.format(date);
       return result;