Here is the complete code for the entire Action:
package cn.ysh.studio.struts2.json.demo.action; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import net.sf.json.JSONObject; import cn.ysh.studio.struts2.json.demo.bean.User; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; // Object that will be serialized by Struts2 into a JSON string private Map<String, Object> dataMap; /** * Constructor*/ public UserAction() { // Initialize the Map object dataMap = new HashMap<String, Object>(); } /** * Test returns JSON data in a view through action* @return */ public String testByJSP() { User user = new User(); user.setId("123"); user.setName("JSONActionJSP"); user.setPassword("123"); user.setSay("Hello world !"); JSONObject jsonObject=new JSONObject(); jsonObject.accumulate("user", user); jsonObject.accumulate("success", true); // Here is a data in the request object, so there cannot be type="redirect" in the result configuration of struts ServletActionContext.getRequest().setAttribute("data", jsonObject.toString()); return SUCCESS; }; /** * Test returns JSON data in Struts2 by default* @return */ public String testByAction() { // The data in the dataMap will be converted to a JSON string by Struts2, so you must first clear the data in it dataMap.clear(); User user = new User(); user.setId("123"); user.setName("JSONActionStruts2"); user.setPassword("123"); user.setSay("Hello world !"); dataMap.put("user", user); // Put in an identity whether the operation is successful dataMap.put("success", true); // Return the result return SUCCESS; } /** * Return JSON data in the traditional way through action* @throws IOException */ public void doAction() throws IOException{ HttpServletResponse response=ServletActionContext.getResponse(); //The following code copied from JSON.java response.setContentType("text/html"); PrintWriter out; out = response.getWriter(); //The object to be returned to the client User user=new User(); user.setId("123"); user.setName("JSONActionGeneral"); user.setPassword("JSON"); user.setSay("Hello , i am a action to print a json!"); JSONObject json=new JSONObject(); json.accumulate("success", true); json.accumulate("user", user); out.println(json.toString()); // Because JSON data is passed in the form of a normal string during the delivery process, we can also manually splice strings that comply with the JSON syntax specifications and output them to the client// The function of the following two sentences is the same as the function of lines 38-46, which will return a User object and a success field to the client// String jsonString="{/"user/":{/"id/":/"123/",/"name/":/"JSONActionGeneral/",/"say/":/"Hello, i am a action to print a json!/",/"password/":/"JSON/"},/"success/":true}"; // out.println(jsonString); out.flush(); out.close(); } /** * When Struts2 serializes a specified attribute, there must be a getter method for that attribute. In fact, if there is no attribute, only getter method is OK * @return */ public Map<String, Object> getDataMap() { return dataMap; } }The complete struts.xml configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="json" extends="json-default" namespace="/test"> <action name="testByAction" method="testByAction"> <result type="json"> <!-- Here you specify the attributes that will be serialized by Struts2. This attribute must have a corresponding getter method in the action --> <!-- By default, the values of all getter methods with return values will be sequenced, regardless of whether the method has corresponding properties --> <param name="root">dataMap</param> <!-- Specify whether to serialize empty properties --> <!-- <param name="excludeNullProperties">true</param> --> <!-- Specify which properties in the dataMap will be serialized --> <!-- Specify which properties in the dataMap will be excluded from the dataMap. These excluded properties will not be serialized, and half will not appear at the same time as the above parameter configuration --> <!-- <param name="excludeProperties"> SUCCESS </param> --> </result> </action> </package> <package name="default" extends="structs-default" namespace="/"> <action name="testJSONFromActionByGeneral" method="doAction"> </action> <action name="testByJSP" method="testByJSP"> <result name="success">/actionJSP.jsp</result> </action> </package> </struts>
The above example of outputting JSON data in Action in Struts2 is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.