6 Replies Latest reply on Jun 11, 2014 12:16 PM by Ricardo Bento

    How to pass return value of Java call out from <aj4:jsFunction>

    Ping Liu Newbie

      I am new to RichFaces and have difficulty in understanding the online docs.  Any response is highly appreciated.

       

      The scenario I am facing is as follows.

       

           <a4j:jsFunction name="myJsFunction" action="#{myManagedBean.myJavaMethod()"/>

       

           <script type="text/javascript">

                var greeting = myJsFunction();

           </script>

       

      Suppose myJavaMethod() in Java code returns a String -- "Merry Christmas!", the same String should be passed to JavaScript to variable greeting.  But I got undefined value.

       

      In short, native JavaScript variable to access jsFunction by calling it which in turn calls Java method.  Java method returns a value.  jsFunction is expected to pass the same value and assign it to the native JavaScript variable but returns undefined.

       

      I think it should be just a matter of how to configure those attribute for the jsFunction.  Any help is greatly appreciated!

        • 1. Re: How to pass return value of Java call out from <aj4:jsFunction>
          Christian Peter Expert

          It's actually quite easy. Just replace "action" with "data":

           

          <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="alert(data);" />

           

          bean:

           

          public String myMethod{

          return "hello world";

          }

           

          You'll have to keep in mind that ajax is asynchron. Therefore you'll have to use a callback handler like "oncomplete=alert(data);" in my example.

           

          Even more complex objects are possible. JSON for example.

          • 2. Re: How to pass return value of Java call out from <aj4:jsFunction>
            Lu Han Newbie

            I don't think it's the right approach to use this tag.

            My sample:

            http://community.jboss.org/wiki/UseA4jjsFunctionToProxyTheJavascript

             

            For your scenario, the common way is use action to invoke the beakingbean's method, and in this method, some attributes of this bean will be updated, and then render the other component whose value is the updated attribute.

            i.e.:

             

            <a4j:commandButtton ... action=#{a.m} render="c"/>

            <h:outputText id="c" value=#{a.x}/>

             

            A{

            x;

            public .. getX(){

                 return x;

            }

            public void m(){

                 x=...;

            }

            }

            • 3. Re: How to pass return value of Java call out from <aj4:jsFunction>
              Ping Liu Newbie

              Thanks for you guys' reply.

               

              Actually, I need use JavaScript.  I thought <a4j:jsFunction> is the best RichFaces component suitable for such case.

               

              However, I couldn't make <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="alert(data);" /> work for my scenario.  The code looks like this

               

              <script type="text/javascript">

                   ...

                   var greeting;

               

                   function setGreeting(data) {

                        greeting = data;

                   }

               

                   demoFunction();

               

                   // calling demoFunction() is supposed to

                   //     1. invoke bean.myMethod() on server side and return value back to JavaScript

                   //     2. and then call setGreeting() and thus set variable greeting indirectly

                   //

                   // now  this variable

                   //

               

                   if (greeting != undefined) {

                        doSomething();

                   }

                   else {

                       doSomethingElse();

                   }

               

                   ...

               

              </script>

              <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(data);" />

               

              bean:

               

              public String myMethod{

                   return "hello world";

              }

               

              This jsFunction call looks odd to me too.  It is not direct invocation and value passing like

               

              var greeting = demoFunction(); or var greeting = myJsFunction(); in my first post.

               

              JavaScript itself is horrible to debug and test and there seems no clue what this <a4j:jsFunction> plays with its attributies and how <a4j:jsFunction> works with JavaScript.

               

              I gave up by just using the following syntax

               

              <script type="text/javascript">

                   ...

               

                   if ("#{bean.myMethod()}" != "") {

                        doSomething();

                   }

                   else {

                       doSomethingElse();

                   }

               

                   ...

               

              </script>

               

              But I really wish <a4j:jsFunction> can pass value back easily.

               

              Thanks again for the reply.

              • 4. Re: How to pass return value of Java call out from <aj4:jsFunction>
                Christian Peter Expert

                As I've already mentioned, jsFunctions are ajax and therefore asynchronous. If you call one of them, the ajax post is made but the control goes staight to the next line of code. You'll have to resume your methods after the result comes back from the backing bean. This can be done with callbackhandlers implemented by  "oncomplete". The "data" object contains some serializable data, that can be parsed on the client. The callback handle has now the result and can continue your algorithms / program / procedure whatsoever.

                 

                 

                <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(data);" />

                 

                 

                 

                var greeting;

                 

                     function setGreeting(data) {

                          greeting = data;

                          if (greeting != undefined) {

                               doSomething();

                          }

                          else {

                              doSomethingElse();

                          }

                     ...}

                • 5. Re: How to pass return value of Java call out from <aj4:jsFunction>
                  Ping Liu Newbie

                  I gave it another try but the callback handler seems not fired.  The page contains a lot of other components.  Even worse, the change made the page stops working.

                   

                  I believe <aj4:jsFunction> works as you claimed.  I saw some other forum, questions/answers on other website mentioning it as well.  But I guess I have to isolate it to a separate small example and try it again from there.  I just need wait until I have extra time.

                   

                  Thanks a lot again!  Happy New Year!

                  • 6. Re: How to pass return value of Java call out from <aj4:jsFunction>
                    Ricardo Bento Newbie

                    I know it is old but for possibile references (event.data):

                    try with <a4j:jsFunction name="demoFunction" data="#{bean.myMethod()}" oncomplete="setGreeting(event.data);" />