Struts2 Send Ajax several questions (without using Struts2-json-plugin case)

Source: Internet
Author: User

The original way to send Ajax to action, you will encounter get,post different, because of the ContentType problem, ContentType must be text/html,struts get the InputStream to have value! And the default jquery to send Ajax using the contenttype is application/x-www-form-urlencoded; Charset=utf-8, if manually set to JSON format to send, then STRUTS2 will not get the data, so in the case of post submission without specifying ContentType, with the default on the line, this is not the same as SPRINGMVC, SPRINGMVC send JSON to specify ContentType as ' Application/json;charset=utf-8 ', and JSON is a fully formatted string, but struts2 not only contentType support text /html, and send data does not need to be converted to JSON string, directly send JSON object can, a bit of a pit AH

When you output JSON in the original way, you can use Fastjson, which is relatively efficient, but the following points are important to note:

1. Fields that do not require JSON conversion, add @jsonfield (Serialize=false) to the Set method
2, mutual Reference object, must have an object property set method plus @jsonfield (serialize=false), in case of circular reference
3, Fastjson conversion data cannot use Hibernate's Load method to obtain data, report stack overflow error
4, the list in the same object for the JSON conversion, the default next object will be displayed as a reference to the previous ref, need to set Fastjson to the same object JSON conversion
String jsonstr = json.tojsonstring (list, serializerfeature.disablecircularreferencedetect);//After closing reference detection

Struts2 Send Ajax several questions (without using Struts2-json-plugin case)

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.