Using JSON AJAX support in Struts 2

Source: Internet
Author: User
Tags json resource

The JSON plug-in provides a resulttype named JSON that, once a result of type JSON is specified for an action, does not need to be mapped to any view resource. Because the JSON plugin is responsible for serializing the state information in the action into JSON-formatted data and returning that data to JavaScript on the client page.

Simply put, the JSON plugin allows us to invoke the action asynchronously in JavaScript, and the action no longer needs to use the view resource to display state information in the action, but the JSON plugin is responsible for returning state information from the action to the calling page- In this way, Ajax interactions can be accomplished.

STRUTS2 provides a pluggable way to manage plug-ins, and installing STRUTS2 JSON plug-ins is not much different from installing common plug-ins, just by copying the jar files of Struts2 plug-ins to the Web-inf/lib path of the Web application.

Install the JSON plugin as follows:

(1) Login to the http://code.google.com/p/jsonplugin/downloads/list site, download the latest version of the Struts2 JSON plugin, the current version is 0.7, we can download the version of the JSON plugin.

(2) To complete the installation of the JSON plug-in, copy the downloaded Jsonplugin-0.7.jar file to the Web-inf path of the Web application.

Implementing Actio logic

Assuming that the Wo,en input page contains three form fields for three request parameters, you should use the action to encapsulate the three request parameters. The name of the three form fields is field1, Field2, and field3 respectively.

The action class code that handles the request is as follows:

public class Jsonexample
{
//encapsulates the three attributes of the request parameter
private String field1;
private transient String field2;
Private String field3;
Properties of the encapsulation processing result
private int[] INTs = {a};
Private Map map = new HashMap ();
Private String Customname = "Custom";
The setter and getter methods that correspond to three request parameters are public
String getField1 ()
{return
field1;
} public
void SetField1 (String field1)
{
this.field1 = field1;
The
setter and Getter methods
for the field1 and Field2 two fields are omitted ...  The setter and getter method public int[of the property that encapsulates the processing result
getints ()
{return
ints;
} public
void Setints (int[] ints)
{
this.ints = ints;
Public
Map Getmap ()
{return
map;
Public
void Setmap (map map)
{
this.map = map;
///Use annotation syntax to change the property name after serialization of this property
@JSON (name= "NewName") public
String getcustomname ()
{return
t  His.customname;
Public
String Execute ()
{
map.put ("name", "Yeeku");
return action.success;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.