Use JSON Ajax support in struts 2

Source: Internet
Author: User

Use JSON Ajax support in struts 2

The JSON plug-in provides a type of resulttype called JSON. Once a result of the JSON type is specified for an action, the result does not need to be mapped to any view resource. Copy the downloaded jsonplugin-0.7.jar file to the Web application's WEB-INF path to complete the installation of the JSON plug-in.

 

The JSON plug-in provides a type of resulttype called JSON. Once a result of the JSON type is specified for an action, the result does not need to be mapped to any view resource. The JSON plug-in will serialize the status information in the action into JSON data and return the data to the JavaScript on the client page.

Simply put, the JSON plug-in allows us to call an action asynchronously in Javascript, and action no longer needs to use view resources to display the status information in the action, the JSON plug-in is responsible for returning the status information in the action to the calling page. In this way, Ajax interaction can be completed.

Struts2 provides a pluggable way to manage plug-ins. Installing the JSON plug-in of struts2 is not much different from installing a common plug-in, just copy the JAR file of the struts2 plug-in to the WEB-INF/lib path of the Web application.

To install the JSON plugin, follow these steps:

(1) log on to the terminal.

(2) copy the downloaded jsonplugin-0.7.jar file to the WEB-INF path of the Web application to complete the installation of the JSON plug-in.

  Implement actio Logic

Assume that the "wo" and "en" input pages contain three form fields. These three form fields encapsulate these three Request Parameters Using action. The names of the three form fields are field1, field2, and field3.

The action code for processing the request is as follows:

Public class jsonexample
{
// Encapsulate the three attributes of Request Parameters
Private string field1;
Private transient string field2;
Private string field3;
// Encapsulate the processing result attributes
Private int [] ints = {10, 20 };
Private map = new hashmap ();
Private string customname = "Custom ";
// The setter and getter methods corresponding to the three Request Parameters
Public String getfield1 ()
{
Return field1;
}
Public void setfield1 (string field1)
{
This. field1 = field1;
}
// The setter and getter methods for field1 and field2 fields are omitted here.
...
// The setter and getter methods that encapsulate the attributes of the processing result
Public int [] 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 the annotation syntax to change the serialized attribute name.
@ JSON (name = "newname ")
Public String getcustomname ()
{
Return this. customname;
}
Public String execute ()
{
Map. Put ("name", "yeeku ");
Return Action. success;
}
}

In the code above, the JSON annotation is used, and the name field is specified during the annotation. The action attribute specified by the name field is serialized into the attribute name of the JSON object. In addition, JSON annotations support the following fields:

 

 

 

Serialize: sets whether to serialize this attribute.

Deserialize: sets whether to deserialize this attribute.

Format: format used to format the output and parse the date form fields. For example, "yyyy-mm-dd't'hh: mm: SS ".

There is a small difference between configuring this action and configuring a common action. You should configure a result of the JSON type for this action. This result does not need to be configured with any view resources.

The code for configuring the Struts. xml file for this action is as follows:

<? XML version = "1.0" encoding = "GBK"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Constant name = "struts. i18n. encoding" value = "UTF-8"/>
<Package name = "example" extends = "JSON-Default">
<Action name = "jsonexample" class = "Lee. jsonexample">
<Result type = "JSON"/>
</Action>
</Package>
</Struts>

There are two notable points in the above configuration file:

The first place is to configure the Struts. i18n. Encoding constant, no longer use GBK encoding, but UTF-8 encoding, this is because Ajax POST requests are encoded in the form of UTF-8.

In the second place, when you configure a package, your package inherits the JSON-default package, instead of the default package. This is because only the package has a JSON-type result.

There are two notable points in the above configuration file:

The first place is to configure the Struts. i18n. Encoding constant, no longer use GBK encoding, but UTF-8 encoding, this is because Ajax POST requests are encoded in the form of UTF-8.

In the second place, when you configure a package, your package inherits the JSON-default package, instead of the default package. This is because only the package has a JSON-type result.

From: http://webservices.ctocio.com.cn/tips/424/7670924.shtml

 

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.