Friday, October 16, 2009

JSON USAGE

JSON Usage :

 

Plug-in :

jsonplugin-0[1].30.jar

This jar file should be palced in lib folder of the Project / Wherever jars placed

 

Configuring in Struts.xml: in struts 2 :

 

Needs to be configure as like below.

Here Test is the package name which we are using for the module , and this package should be extends json-default along with other extends like tiles-default.

 

AJAXSMDAction is the action name of json call for the action class TestAction.java , and overriding method of JSON for eg here smd,

Referring interceptor has "JSON" . Whenever

 

 

 

<package name="Test" extends="tiles-default,json-default">

<action name="AJAXSMDAction" class="com.test.action.TestAction" method="smd">

<interceptor-ref name="json">

<param name="enableSMD">true</param>

<param name="ignoreSMDMethodInterfaces">false</param>

</interceptor-ref>

<result type="json">

<param name="enableSMD">true</param>

<param name="ignoreInterfaces">false</param>

</result>

 

</action>

 

</package>

 

 

IN javascript;

 

function showJsonUsage(username) {

    // load dojo RPC

    dojo.require("dojo.rpc.*");    

 

    // create service object(proxy) using SMD (generated by the json result)

 

//This service is nothing but action to call

    var service = new dojo.rpc.JsonService("AJAXSMDAction.action");

 

 

 

    var callback = function(userDetails) {

                

        document.getElementById("userName").innerHTML = (userDetails. userName!=null)? userDetails. userName:"";

        document.getElementById("firstName").innerHTML = (userDetails. firstName!=null)? userDetails. firstName:"";

          

          

        

        if(document.getElementById("lastName")!=null){

            document.getElementById("lastName").innerHTML = userDetails.lastName;

        }        

 

    };

 

    /* parameter */

    var userDetails = {};

 

    /* execute remote method or the method which we suppose to call in Action calss */

    var returnedUserDetails = service.retrieveUserDetails (userDetails,userName);

    /* attach callback to defered object */

    returnedUserDetails.addCallback(callback);

 

}

 

 

 

In JSP,

 

username , firstName , lastName are input fields /textfields with the ids respectively.

 

In Action :

 

TestAction.java

import com.googlecode.jsonplugin.annotations.SMDMethod;

/**

     * Ajax Method to Retrieve User Details

     * */

    @SMDMethod

    public UserDetails retrieveUserDetails(UserDetails userDetails,

            String userName) {

//Calling Servic e from action, it will return the USerDetaisl model object

 

 

        userDetails = getUserDetailsService().getUserDetails(

                new String (userNAme));

        

            return userDetails;

    }

//this action class for eg. Extends ParentTestAction.java

 

This class should be have this method

@SMDMethod

    public String smd()

    {        

        return SUCCESS;

    }

 

This method is overrides the TestAction , retrieveUserDetails method by providing @SMDmethod using Annotation .

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment