简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM