3 Replies Latest reply on Mar 25, 2011 5:27 AM by Nick Belaevski

    Send JSON as param using a4j:jsFunction

    Satya K Newbie

      Hi All,

       

      Is it possible to send JSON object as a pramaeter using a4j:jsFunction?

      Ex: 

      <a4j:form>

          <a4j:jsFunction name="callMe" reRender="updateSomeView">

             <a4j:actionparam name="jsonObject" assignTo="#{userBean.jsonObject}" />

          </a4j:jsFunction>

      </a4j:form>

       

      Also I am trying to implement a JS interface for third party JQuery library. So that library should call my JS function.

       

      For this to work is it enough for me to define the function using <a4j:jsFunction name="callMe"> or should I actually have to decalre JS function

       

      like : function callMe() {...}

       

      Thanks in advance for all the help.

       

      Regards,

      Satya

        • 1. Send JSON as param using a4j:jsFunction
          Nick Belaevski Master

          Hi Satya,

           

          No it's not possible to send JSON object out of the box.

           

          It is enough to define <a4j:jsFunction> tag, nothing else required.

          • 2. Send JSON as param using a4j:jsFunction
            Satya K Newbie

            Hi Nick,

             

            I tried few things yesterday and it is possible to send the JSON object. Also you can reconstruct the object at bean.

             

            Though not straightforward but still possible.

             

            Below code calls a javascript function which internally calls a4j:jsFunction for sending JSON...

             

                    <a4j:form>

                        <a4j:commandButton value="Click Me..." onclick='callMe();'/>

                        <a4j:jsFunction name="updateCart">

                            <a4j:actionparam name="json" assignTo="#{partManagementBean.jsonObject}"/>                 

                        </a4j:jsFunction>

                    </a4j:form>

             

            JS function converts JSON to text

             

            function callMe() {

                var myJSON = {"firstName" : "Satya", "lastName" : "K"};

                var jsonText = JSON.stringify(myJSON);

                updateCart(jsonText);

            }

             

            Reconstructing the JSON string to Object...

             

               public void setJsonObject(Object jsonObject) throws JSONException {

                    JSONObject richObj = new JSONObject(jsonObject.toString());

                    LOGGER.info("richfaces jsonObject : " + richObj.getString("firstName"));

                   

                    this.jsonObject = jsonObject;

               }

             

            Regards,

            Satya

            • 3. Send JSON as param using a4j:jsFunction
              Nick Belaevski Master

              Yes, possible in this way.