1 Reply Latest reply on May 23, 2016 4:19 PM by amostech

    transfer a value from javascript to a backingbean java

    voicul

      Hello guys,

      I have a js code that finds out the timezone and then I am trying to "send" a value to a java backingbean.


      This code must be execute during the load of the xhtml page because in the process I use the "localTimeZone" bean.

       

                  <h:inputHidden value="#{localTimeZone.timeZoneID}" id="clientSelectedTimeZoneID"/>
         <h:inputHidden value="#{localTimeZone.offset}" id="clientSelectedOffset"/>

       

         <script type="text/javascript">
         var userTimeZone = userTimeZone();
        
         var hiddenElementTimeZoneID = document.getElementById("requestWizardForm:clientSelectedTimeZoneID");
         hiddenElementTimeZoneID.value = userTimeZone.timeZoneID;
        
         var hiddenElementOffset = document.getElementById("requestWizardForm:clientSelectedOffset");
         hiddenElementOffset.value = userTimeZone.offset;
           </script>

       

      Can you please tell me how is the correct way to do this in order to have the setters of backingbean called ?

      Thanks

      PS:

      JSF ver: Implementation-Version: 1.2_12-b01-FCS
      RICHFACES: Implementation-Version: 3.3.3.Final

        • 1. Re: transfer a value from javascript to a backingbean java
          amostech

          I have no specific information regarding richfaces itself but I am guessing this should be similar regardless of which template library you use.

           

          For example in primefaces you would have a component called <p:remoteCommand which allowed you to bind a javascript function to a backing bean method.

           

          Anyway.

           

          Let's say you are doing this in a non-ajax manner. Meaning that your parameters are going to be sent through a POST or GET request to your backing bean.

           

          Then I would just create a button in the screen:

          <h:commandButton id="hdnBtn" actionListener="#{personBean.method}" style="display: none;" />


          Note the display: none; css. So when your page loads you change the value of this field and trigger it's click manually. Like this:


          onclick="jQuery('#form:hdnBtn').click();"


          If you don't like this approach, please refer to this stackoverflow entry where you will find other suggestions.

          Hope this helps.


          Best,

          Arthur.