3 Replies Latest reply on Nov 20, 2011 3:33 AM by Christian Peter

    calendar value not save

    Xiaofeng Feng Newbie

      hello everybody,My English is very poor,Sorry.

       

      1.page

       

      <a4j:keepAlive beanName="admp_admin_urlMaintainBB" />

      <h:form>

          

          <h:panelGrid binding="#{admp_admin_urlMaintainBB.panelGrid}"/>  

           <a4j:commandButton action="#{admp_admin_urlMaintainBB.save}" value="save"/>

      </h:form>

       

      2.backingbean

       

      private HtmlPanelGrid panelGrid;

       

      public HtmlPanelGrid getPanelGrid() {

             if(panelGrid == null) {

                   panelGrid = new HtmlPanelGrid();

                   HtmlCalendar calendar = new HtmlCalendar();

                   calendar = new HtmlCalendar();

                   panelGrid.getChildren().add(calendar);

             }

            

      }

       

      public String save() {

          

           HtmlCalendar calendar =  (HtmlCalendar)panelGrid.getChildren().get(0);

           System.out.println(calendar.getValue());

           return "";

      }

       

      The printed value is empty,what is it

        • 1. Re: calendar value not save
          Christian Peter Expert

          """

                       panelGrid = new HtmlPanelGrid();

                       HtmlCalendar calendar = new HtmlCalendar();

                       calendar = new HtmlCalendar();"""

           

          No!

           

          You have to use the FacesContext and Application to create UIComponents programmatically, like for example:

           

          FacesContext facesContext = FacesContext.getCurrentInstance();

           

          UIComponent component = facesContext.getApplication().createComponent(

                  HtmlPanelGroup.COMPONENT_TYPE);

           

           

          You should use valuebinding in your calendar component instead of accessing it in the component tree directly.

          • 2. Re: calendar value not save
            Xiaofeng Feng Newbie

            calendar save, value is emtpy

             

            <a4j:keepAlive beanName="bean"></a4j:keepAlive>
              <h:form >
               <rich:calendar binding="#{bean.richCalendar }"/>
               <h:commandButton action="#{bean.save }"/>
              </h:form>

             

            private HtmlCalendar richCalendar;
             
            public HtmlCalendar getRichCalendar() {
              if(richCalendar == null) {
               FacesContext facesContext = FacesContext.getCurrentInstance();
               UIComponent component = facesContext.getApplication().createComponent(HtmlCalendar.COMPONENT_TYPE);
              
               richCalendar =(HtmlCalendar) component;
              }
              return richCalendar;
            }

             

            public void save() {
                         System.out.println(richCalendar.getValue());
                }

            • 3. Re: calendar value not save
              Christian Peter Expert

              What scope does your bean have?

               

               

               

              Just use valuebinding!

               

              <h:form>

               

              <rich:calendar value="#{myBean.date}" />

               

              <a4j:commandButton value="save" action="#{myBean.save}" />

               

              </h:form>

               

               

              Bean:

              ...

              private Date date;

               

              public getDate(){

              return date;

              }

               

              public setDate(Date date){

              this.date = date;

              }

               

              public String save(){

              System.out.println("date is: " + date);

              return "";

              }