Thursday, April 18, 2013

Send JSON data to struts2

Send JSON data to struts2


I found a nice article which points out how the struts2 receives json data from UI and returns the json data as response.


We need to add a dependency in pom.xml

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts2-json-plugin.version}</version>
        </dependency>


In action class just add the interceptor which maps the json data to the actions class.
    <action name="jsonTest" class="com.blah.action.JsonTestAction">          <interceptor-ref name="json">              <param name="contentType">application/json</param>          </interceptor-ref>          <result type="json"/>      </action>

Json data can be returned from strust2 action to View in the form of

        <action name="jsonTest" method="jsonTest" class="com.blah.action.JsonTestAction">
            <result type="json"/>
        </action>


Happy Coding.

--
Thank you.


Regards,
Kaleeswaran.S

No comments:

Post a Comment