下面是整個Action的完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
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; //將會被Struts2序列化為JSON字符串的對象 private Map<String, Object> dataMap; /** * 構造方法 */ public UserAction() { //初始化Map對象 dataMap = new HashMap<String, Object>(); } /** * 測試通過action以視圖方式返回JSON數據 * @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 ); //這里在request對象中放了一個data,所以struts的result配置中不能有type="redirect" ServletActionContext.getRequest().setAttribute( "data" , jsonObject.toString()); return SUCCESS; }; /** * 測試通過action以Struts2默認方式返回JSON數據 * @return */ public String testByAction() { // dataMap中的數據將會被Struts2轉換成JSON字符串,所以這里要先清空其中的數據 dataMap.clear(); User user = new User(); user.setId( "123" ); user.setName( "JSONActionStruts2" ); user.setPassword( "123" ); user.setSay( "Hello world !" ); dataMap.put( "user" , user); // 放入一個是否操作成功的標識 dataMap.put( "success" , true ); // 返回結果 return SUCCESS; } /** * 通過action是以傳統方式返回JSON數據 * @throws IOException */ public void doAction() throws IOException{ HttpServletResponse response=ServletActionContext.getResponse(); //以下代碼從JSON.java中拷過來的 response.setContentType( "text/html" ); PrintWriter out; out = response.getWriter(); //將要被返回到客戶端的對象 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()); // 因為JSON數據在傳遞過程中是以普通字符串形式傳遞的,所以我們也可以手動拼接符合JSON語法規范的字符串輸出到客戶端 // 以下這兩句的作用與38-46行代碼的作用是一樣的,將向客戶端返回一個User對象,和一個success字段 // 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(); } /** * Struts2序列化指定屬性時,必須有該屬性的getter方法,實際上,如果沒有屬性,而只有getter方法也是可以的 * @return */ public Map<String, Object> getDataMap() { return dataMap; } } |
完整的struts.xml配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<? 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" class = "cn.ysh.studio.struts2.json.demo.action.UserAction" method = "testByAction" > < result type = "json" > <!-- 這里指定將被Struts2序列化的屬性,該屬性在action中必須有對應的getter方法 --> <!-- 默認將會序列所有有返回值的getter方法的值,而無論該方法是否有對應屬性 --> < param name = "root" >dataMap</ param > <!-- 指定是否序列化空的屬性 --> <!-- <param name="excludeNullProperties">true</param> --> <!-- 這里指定將序列化dataMap中的那些屬性 --> <!-- <param name="includeProperties"> userList.* </param> --> <!-- 這里指定將要從dataMap中排除那些屬性,這些排除的屬性將不被序列化,一半不與上邊的參數配置同時出現 --> <!-- <param name="excludeProperties"> SUCCESS </param> --> </ result > </ action > </ package > < package name = "default" extends = "struts-default" namespace = "/" > < action name = "testJSONFromActionByGeneral" class = "cn.ysh.studio.struts2.json.demo.action.UserAction" method = "doAction" > </ action > < action name = "testByJSP" class = "cn.ysh.studio.struts2.json.demo.action.UserAction" method = "testByJSP" > < result name = "success" >/actionJSP.jsp</ result > </ action > </ package > </ struts > |
以上這篇在Action中以Struts2的方式輸出JSON數據的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。