Use Struts2 and jQuery EasyUI to implement a simple CRUD System (iii) -- ajax. Use json format for struts2 interaction.
Following the previous article: Using Struts2 and jQuery EasyUI to implement a simple CRUD System (II) -- interaction between ajax and struts2, next we need to transmit data in json format.
Here are the three methods:
1. As mentioned in the last article, it is not difficult to convert the specific content to the json format before sending it to the page.
2. Use ServletActionContext. getResponse (). getWriter (). println (content). The content can also be in json format, and then return Action. NONE.
3. Use the json plug-in of struts.
Prepare the struts2-json-plugin-2.3.16.3.jar package.
There are also json-format conversion class JSONObject packages and the required jar packages:
You must:
However, this part and the struts package can be used to remove the duplicate part.
In struts. xml
It is best to remove it during development. After a commons-lang Package is imported, an exception is reported and the error page jumps to the error page, so that I do not know where the error is or report an exception.
In fact, the official documentation provides a simple example:
Https://struts.apache.org/docs/json-plugin.html
First, AjaxAction. java
package action;import java.util.HashMap;import java.util.Map;import net.sf.json.JSONObject;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class AjaxAction implements Action{private String jsonresult;public String getJsonresult() {return jsonresult;}public void setJsonresult(String jsonresult) {this.jsonresult = jsonresult;}public String json() throws Exception {System.out.println(action execute);Map
map = new HashMap
();map.put(b,b);System.out.println(map);JSONObject json = null;json = JSONObject.fromObject(map);System.out.println(json);jsonresult = json.toString();System.out.println(jsonresult);return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}}
Modify something in struts. xml. The original struts-default is changed to json-default.
Result needs to declare the json type.
/hello.jsp
Output result
What is the principle? Change the AjaxAction.
package action;import java.util.HashMap;import java.util.Map;import net.sf.json.JSONObject;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class AjaxAction implements Action{private String jsonresult;private String jsonsecond;private String jsonthird;public String json() throws Exception {System.out.println(action execute);Map
map = new HashMap
();map.put(b,b);System.out.println(map);JSONObject json = null;json = JSONObject.fromObject(map);System.out.println(json);jsonresult = json.toString();System.out.println(jsonresult);return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}public String getJsonsecond() {return jsonsecond;}public void setJsonsecond(String jsonsecond) {this.jsonsecond = jsonsecond;}public String getJsonresult() {return jsonresult;}public void setJsonresult(String jsonresult) {this.jsonresult = jsonresult;}}
First view the output result:
Jsonsecond is null, and jsonresult is in json format. Both of them have jsonthird. Because there is no corresponding get or set method result, jsonsecond is null because no value is assigned.
Struts will help you output the declared attributes with get and set methods.
JSONObject helps you convert a map object into a json string.
Why is it converted into a json string, because some front-ends require a specific data format for interaction, such as EasyUI written later.
With the basis of the previous two articles, use the plug-in provided by struts and the JSONObject class to solve the problem.
The problem of json Data Interaction Between ajax and struts is solved.