Struts2 + jqury ajax problem summary and Solution

Source: Internet
Author: User

How to Use ajax technology in jquery to struts2:
Ajax involves several concepts:
1. Request url, that is, the background code url to be executed, which can be servlet, jsp, or action in struts
2. Data. When a request is submitted to a url, parameters may need to be transmitted to it. These parameters are organized in json format and passed in as parameter;
3. Submission method: get or post;
4. Callback Function: this refers to how to obtain the returned data and process the returned data after the request is complete. There are several types of errors, success, and complete;

When struts2 is not used, you can use servlet or jsp directly. The output data is usually in json format and printed directly to the client using out. print. The output data is encapsulated into an object on the client and then directly used in the callback function. For examples, see hibernate advanced query in Return of the King of Java Web development;

When using struts2, struts2 re-encapsulates the servlet mechanism, so that the idea of using servlet directly to Implement Asynchronous requests is not applicable here. In the past, I used to return the struts2 action to a jsp page in a function to process the user's uploaded avatar. print data to obtain and process the returned data. However, when struts2 is used, some problems are encountered. Someone suggested that the action directly return null, get response through the context, and then get the Writer object, and then directly write the data into the out stream. This is a method, but to better understand how struts works with jquery, it is necessary to understand how to use action to implement ajax functions without using servlet or jsp.

In actual use, the $. ajax function does not have any problems when calling the url. The problem mainly occurs in how to obtain the returned value and how to handle the returned value.
At first, I tried to return SUCCESS and use Writer to output a string in json format. However, the result is strange. The callback function can be executed, but no data is obtained. In addition, the callback function can only be written in the form of success () and cannot assign parameters to it. If the parameter is assigned, the task is not executed. It is very strange that the returned data is incorrect.

Later, I continued to check the information. Someone mentioned that the type of the action returned result must be set to "json" in the struts configuration file to return data like servlet.
Several problems occurred during the actual attempt:
1. First, use In this way, the returned result requires the package to inherit the json-default package. This package is not included in the struts library by default, you need to go to the next, the package name: struts2-json-plugin-xxxx.jar this. Then you need a package, which is specifically used for struts to convert json objects: ezmorph-xxxx.jar. Other common struts2 packages are required, such as core, convention, json-lib, and common-series packages.
The dependency is resolved, and conflicts between struts-default and json-default must be resolved.
Struts-default can set the result type to type = "stream", which is useful when the verification code is dynamically generated. However, json-default does not support it.
The solution is to set two packages with different names, and specify a package that inherits the json-default as a namespace = "json" and the main package.
After these operations are completed, you can directly access the SUCCESS action returned under json to obtain an output result in json format. The output content contains all the fields of the getter and setter methods in this action.
The output results in json format are obtained before, but problems still occur in the success callback function. I also found that the output json contains all the fields with the setter and getter methods on the page. In fact, only one result is required for configuration using this method. Due to the servlet method, many people think that the returned string should be in json format. In fact, the returned object is a JSONObject object. That is, you need to configure a JSONObject domain for the action with the getter and setter methods. In addition, in the struts configuration file corresponding to the action Method pointed to by the url Add a param label:Field_nameIt is unclear whether the attribute "name" is "root" is used, but "field_name" must be the same as the name of the JSONObject configured in the action. Then, enter the action pointed to by the ajax url in the address bar of the browser, and only return the toString () Result of the JSONObject variable. Then, in the success of $ _ ajax, set the function: function (data) {}, and the data contains the correct json data. Some blog posts wrote that to use an eval method to convert data into a json object, you do not need to do this here. You can directly access the data using the data. variable name. For example, data. username can access the value of the username field in the json object.


I have referenced some articles during problem solving, which are more or less helpful. I respect and thank the original author. The link is as follows:

Struts2 + jquery + json for ajax requests


For help .. Struts2 + jquery + json Always Returns error


Examples of ajax callback functions (native code and jquery Code) are not listed in any other articles.


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.