Search code examples
javascriptjsfrichfacesejbajax4jsf

JSF. How to set a value of JavaScript function to a field of Bean?


Good morning! How to set a value of JavaScript function to a field of Enterprice Java Bean?
I have the js function:

<script type="text/javascript">
    function getTimezone() {
      var d = new Date()
      var gmtMinutes = -d.getTimezoneOffset();
      return gmtMinutes;
    }
</script>

I'm trying to use:

<a4j:jsFunction name="timezone" assignTo="MyBean.gmtMinutes">
       <a4j:actionparam name="timezone" value="getUserId()"/>
</a4j:jsFunction>

But I did not get. I think that I incorrectly used the tag a4j:jsFunction. Give me advice please how to use the tag correctly!


Solution

  • You can register a jsFunction which sends parameter's value to server:

            <a4j:jsFunction name="updateTimeZone">
                <a4j:param name="timezone" assignTo="#{MyBean.gmtMinutes}"/>
            </a4j:jsFunction>
    

    And then invoke that jsFunction from JavaScript:

    <script type="text/javascript">
        function sendTimezoneToServer() {
          var d = new Date()
          var gmtMinutes = -d.getTimezoneOffset();
          updateTimeZone(gmtMinutes);
        }
    </script>
    

    There is also an example of the same on RichFaces Showcase: http://richfaces-showcase.appspot.com/richfaces/component-sample.jsf?demo=jsFunction&skin=blueSky