5 Replies Latest reply on Mar 5, 2009 11:38 AM by Aleksandr Dubovsky

    Page reload required for the backbean data fetched with rich

    Andrey Stepanov Newbie

      In my small application I want to fetch (using some external service) and output currency rates for a date selected with rich:calendar.
      Courses are fetched by clicking the a4j:commandButton after the date has been selected.

      This envokes backing beans properties to be updated.
      But strangely enough updated bean properties are transferred to the rich:inplaceInput ONLY AFTER THE .jsp page has been reloaded


      My code looks like this:


      .jsp page

      <rich:calendar id="myCalendar" value="#{calculator.selectedDate}">
      <a4j:support event="onchanged" reRender="euroRate, usdRate" />
      </rich:calendar>

      <a4j:commandButton id="getCourseButton" value="Get Currency Rates" actionListener="#{calculator.fetchCourse}" reRender="euroRate, usdRate" />


      <rich:inplaceInput id="euroRate" value="#{calculator.euroRate}" selectOnEdit="true" editEvent="onclick" layout="block" changedHoverClass="hover" viewHoverClass="hover" viewClass="inplace" changedClass="inplace" validator="#{calculator.checkFieldEuro}" showControls="true">



      Calculator.java

      private String euroRate;
      private String usdRate;
      private Date selectedDate;
      …

      /* this method addresses to some external service to read out the currency rates */

      public void fetchCourse(ActionEvent event) {

      Thread t = new Thread() {

      public void run() {
      CBRFCurrencyCourse cbrfCourse = new CBRFCurrencyCourse();
      DateCurrencyCourse course = null;
      //Date dateLocal = new Date(System.currentTimeMillis());
      try {
      course = cbrfCourse.getDateCurrencyCourse(getSelectedDate());
      setEuroRate(Double.toString(course.getCurrency().get(Currency.EUR)));
      setUsdRate(Double.toString(course.getCurrency().get(Currency.USD)));
      System.out.println("Euro rate: " + getEuroRate());
      System.out.println("USD rate: " + getUsdRate());
      } catch (TravelCostException ex) {
      String message = bundle.getString(ex.getResource());
      } finally {
      //progress bar dissappears
      }
      }
      };
      t.start();
      }


      Instead of inplaceInput components I have used also the following code snippet:

      <h:inputText id="euroRate" value="#{calculator.euroRate}">
      <a4j:support actionListener="#{calculator.checkFieldEuro}" event="onkeyup" />
      </h:inputText>


      And tried the following code for the calendar:

      <rich:calendar value="#{calculator.selectedDate}" currentDateChangeListener="#{calculator.getCourse}">
      <a4j:support event="onchanged" action="#{calculator.getCourse}"/>
      </rich:calendar>


      BUT NEITHER WAY I SEEM TO ABLE TO SEND BACKBEAN PROPERTIES’ VALUES to OUTPUT components in my jsp page.

      Please advise,
      I will be very grateful for you suggestions





        • 1. Re: Page reload required for the backbean data fetched with
          Andrey Stepanov Newbie

          I would be thankful also if some specialist explained me the folllowing moments:

          1. what is the difference between onchange and ondateselected as applied to rich:calendar ?

          2. what is the proper use of ajaxkeys attribute ? doesn't it equivalent to reRender attribute?

          Thank you in advance

          • 2. Re: Page reload required for the backbean data fetched with
            Nick Belaevski Master

            Hello,

            Looks like this happens because rich:inplaceInput is submitting its value back to bean when you press a4j:commandButton. Try enclosing this:

            <rich:calendar id="myCalendar" value="#{calculator.selectedDate}">
            <a4j:support event="onchanged" reRender="euroRate, usdRate" />
            </rich:calendar>
            
            <a4j:commandButton id="getCourseButton" value="Get Currency Rates" actionListener="#{calculator.fetchCourse}" reRender="euroRate, usdRate" />
            into a4j:region.

            Another thing I suspect is the thread invoked from action listener method. Are you sure it completes its task before view rendering when components read values?

            1. I've asked component developer to clarify.

            2. ajaxKeys is an attribute that provides additional information for reRender. Please take a look here: http://java.dzone.com/articles/an-introduction-to-jboss-richf?page=1,5

            • 3. Re: Page reload required for the backbean data fetched with
              Andrey Stepanov Newbie

              Dear Nick,

              Thank you a lot for your suggestions.

              <a4j:region> did not help.

              I was not sure indeed if the thread invoked from action listener completed its task before view rendering.

              { I would like actually to learn how and when exactly rich-components read values from backing bean properties.
              If backing bean properties read their values from some distant service, can I somehow postpone the reading of those values?
              Could you please clarify this for me? }

              Therefore I removed thread.

              The result was that the course is successfully read in the inplaceInput fields for the first date selected.
              But if I change the date it takes now not the total page upload but … moving mouse over those inplaceInput fields for the new values to appear :)

              May be you have met such behaviour and could suggest me a solution?

              PS: thank you for the link to the excellent article by Max Katz

              • 4. Re: Page reload required for the backbean data fetched with
                Nick Belaevski Master

                 

                "andrew_st" wrote:
                I would like actually to learn how and when exactly rich-components read values from backing bean properties.
                If backing bean properties read their values from some distant service, can I somehow postpone the reading of those values?
                Could you please clarify this for me?

                Components are rendered on the 6th phase, just after all action listeners and actions are processed. If the external service is known to be quick you can wait until it returns some value just inside action method. If the service is not quick, you can use a4j:push that will poll the server for response and then automatically update view when new data arrive.

                "andrew_st" wrote:
                The result was that the course is successfully read in the inplaceInput fields for the first date selected.
                But if I change the date it takes now not the total page upload but … moving mouse over those inplaceInput fields for the new values to appear :)

                We'll check this issue.


                • 5. Re: Page reload required for the backbean data fetched with
                  Aleksandr Dubovsky Newbie

                   

                  "andrew_st" wrote:

                  The result was that the course is successfully read in the inplaceInput fields for the first date selected.
                  But if I change the date it takes now not the total page upload but … moving mouse over those inplaceInput fields for the new values to appear :)


                  Hello,

                  Not sure that understand you correctly. Could you please describe your problem more exactly?