5 Replies Latest reply on Jan 31, 2012 5:22 AM by john Sheppard

    Rich:Calendar - String backing bean version 4.1.0-FINAL

    john Sheppard Newbie

      Hello,

              I really love to work with richfaces, and I usually handle all the problems by myself, but this time I can't figure out why when i set a String backing bean value for the calendar component, he sets the value of the string to something like :

                                                   Wed Jan 04 12:00:00 CET 2012
      therefore, when I refresh the page, the calendar component cannot understand his own writing, he cannot parse that String that he have set himself.

       

      is there any possible way to set the formatting , or the pattern to the value ?

      I want it to be like

       

      "dd-MM-yyyy HH:mm"

      here is some code :

      the calendar is inside an xhtml, and I include this file like this :

       

      // triggerVAValue is a string
      <ui:include src="./Item_selector.xhtml">                    
      <ui:param name="items" value="#{NotificationFacadeBean.triggerTypes}" />
      <ui:param name="valueForType" value="#{NotificationFacadeBean.currentNotification.triggerVAValue}"/>
      </ui:include>
      
      // this is Item_selector.xhtml
        <ui:repeat value="#{items}" var="item" >
           <rich:calendar  datePattern="dd-MM-yyyy HH:mm" rendered="#{item.type.equals('Date')}" value="#{valueForType}"  locale="#{local.calendarLocal}"
                                inputSize="14">
                              </rich:calendar>
      </ui:repeat>
      
        • 1. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
          john Sheppard Newbie

          Any kind of help, or tips could be appriciated thx.

          • 2. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
            Tom Giberius Newbie

            Indeed strange and I run into this kind of issue but different:

             

            Perphaps a custom date converter might solve your problem. It is an atribute of the date field:

            converter        ="LaDateConverter"

             

            Then implement a Java Class:

             

            @FacesConverter("LaDateConverter")

            public class LaDateConverter implements Converter {

             

            You have to override:

            public Object getAsObject(FacesContext context, UIComponent component, String value) {

            public String getAsString(FacesContext context, UIComponent component, Object value) {

             

            As format mask I used: sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

             

            It it no use to give you my converter while it is specific to solve my use case where I switch between query mode, where all fields are strings and normal mode. Only with the date field I got stuck while no trigger fires. I will raise an enhencement request later but the date converter solved the issue with the date format mask.

            • 3. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
              john Sheppard Newbie

              hello Tom,

                             Thanks alot for your answer. It helped me alot,when I implemented your solution, I made the mistake to return a Date on the function getAsObject(FacesContext context, UIComponent component, String value); getAsObject and getAsString should both return a String, because my backing value is a String.

               

              Is there a way to retrieve the userPattern from the calendar itself?, instead of using a hardcoded pattern, how can I use the one set in the calendar tag???

               

               

              <rich:calendar converter="#{CalendarDateStringConverter}" datePattern="dd/MM/yyyy HH:mm" rendered="#{item.type.equals('Date')}" value="#{valueForType}"  locale="#{access.calendarLocal}"
                                        inputSize="14">
                                      </rich:calendar>
              
              I put my code here (for people who get cought in the same trap)
              @FacesConverter("CalendarDateStringConverter")
              @Named("CalendarDateStringConverter")
              public class CalendarDateStringConverter implements Converter  {
                  private String pattern = "dd/MM/yyyy HH:mm";
                  @Override
                  public Object getAsObject(FacesContext context, UIComponent component, String value) {
                      String result = "";
                      if (value != null && value.length() > 0) {
                          try {
                              //SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
                              SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                              Date date = sdf.parse(value);
                              result = sdf.format(date);
                          } catch (ParseException ex) {
                              Logger.getLogger(CalendarDateStringConverter.class.getName()).log(Level.SEVERE, null, ex);
                          }
                      }
                      return result;
                  }
                  @Override   
                  public String getAsString(FacesContext context, UIComponent component, Object value) {
                      String result = "";
                      String valueStr = (String) value;
                      if (valueStr!= null && valueStr.length() > 0) {
                          try {
                              SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                              Date date = sdf.parse(valueStr);
                              result = sdf.format(date);
                          } catch (ParseException ex) {
                              Logger.getLogger(CalendarDateStringConverter.class.getName()).log(Level.SEVERE, null, ex);
                          }
                      } 
                      return result;
                  }
              }
              
              • 4. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
                Tom Giberius Newbie

                Hi John,

                 

                Good to hear it solved your problem. I have no idea how to get the date pattern from the calendar object. However I saw that you set the datePattern attribute in rich:calendar and that you could use this datepatternr in the converter (I did not know that). If this is the case you might try:

                1) Set the datePattern in your bean, e.g. datePattern={myBean.datePattern}

                2) Inject you bean in the converter with CDI, e.g

                           

                   @Inject

                    protected MyBean myBean;

                 

                You can use CDI if you have defined your beans with @Named(value = "myBean")

                 

                If you can inject your bean you can have the datepattern in your converter: myBean.getDatePattern

                1 of 1 people found this helpful
                • 5. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
                  john Sheppard Newbie

                  Hi Tom,

                              Thanks again for your answer, I wanted to give the freedom to anyone using a calendar object to set his pattern on the JSF page, then the converter will take care of everything by retrieving the pattern set by the user into the calendar object.

                   

                  isn't it possible to retrieve that string pattern from the component ? could be very useful.