3 Replies Latest reply on Apr 23, 2010 4:53 PM by Aldo Bonzi

    pass return value from javascript to seam

    Marcel Vielsack Newbie

      Hi!


      I want to store the return value of a javascript in a seam component, but i just don't know how to do this.


      Things like this don't work:


      <h:commandButton value="submit" action="#{mySeamComponent.store(javascript:getValue())}"/>


      Has anybody an idea?


      Thanks.


      Cheers Marcel

        • 1. Re: pass return value from javascript to seam
          Tauseef Ahmed Newbie

          Hi Marcel,


          You can go with reverse process.
          First call a javascript method and perform operation whatever is there in getValue(), and pass the result of getValue to Seam Component using seam remoting method.


          Check Seam Remoting...



          Regards,
          Tauseef

          • 2. Re: pass return value from javascript to seam
            Marcel Vielsack Newbie

            Hey


            Thanks for the reply.
            I try to do it the way you suggested.


            Now I'm facing a new problem...
            I added the @WebRemote annotation to the Java method and added


            <s:remote include="mySeamComponent" />


            to the xhtml file.
            When I try to call the method from my javascript function, i get this error (reported by firebug)
            "Error: mySeamComponent.myJavaMethod is not a function"


            I also copied the jboss-seam-remoting.jar into the EAR-Content\lib folder.


            Does anybody know a solution?


            Thanks.

            • 3. Re: pass return value from javascript to seam
              Aldo Bonzi Novice

              Another thing you can do is to use hidden fields where you can assign the javascript value.
              Take a look at this:




              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml"
                     xmlns:h="http://java.sun.com/jsf/html">
                   <body>
                        <script>
                             function assignValue(){
                                  var d = new Date();
                                  document.getElementById("form1:hidden1").setAttribute("value",d);
                             }
                        </script>
                        <h:form id="form1">
                             <h:inputHidden id="hidden1" value="#{mySeamComponent.localTime}" />
                             <h:commandButton action="#{mySeamComponent.showLocalTime}" value="Submit" onclick="javascript:assignValue()"/>
                        </h:form>
                   </body>
              </html>



              MySeamComponent.java



              @Name("mySeamComponent")
              public class MySeamComponent {
                   private String localTime;
                   
                   public void showLocalTime(){
                        System.out.println(localTime);
                   }
              
                   public String getLocalTime() {
                        return localTime;
                   }
              
                   public void setLocalTime(String localTime) {
                        this.localTime = localTime;
                   }
              }