久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Struts2返回JSON數(shù)據(jù)的具體應(yīng)用范例

 南郡修道院 2012-10-14

早在我剛學(xué)Struts2之初的時(shí)候,,就想寫一篇文章來(lái)闡述Struts2如何返回JSON數(shù)據(jù)的原理和具體應(yīng)用了,但苦于一直忙于工作難以抽身,,漸漸的也淡忘了此事,。直到前兩天有同事在工作中遇到這個(gè)問(wèn)題,來(lái)找我詢問(wèn),,我又細(xì)細(xì)地給他講了一遍之后,,才覺(jué)得無(wú)論如何要抽一個(gè)小時(shí)的時(shí)間來(lái)寫這篇文章,從頭到尾將Struts2與JSON的關(guān)系說(shuō)清楚,。

 

其實(shí)網(wǎng)絡(luò)中,,關(guān)于這個(gè)問(wèn)題的答案已是海量,我當(dāng)初也是從這海量的答案中吸收精華,,才將“Struts2返回JSON數(shù)據(jù)”這個(gè)問(wèn)題搞清楚的,。但是這些海量的答案,,有一個(gè)共同的缺陷,就是作者們只關(guān)注問(wèn)題核心,,即“如何在具體的Struts2應(yīng)用中返回JSON數(shù)據(jù)到客戶端”如何實(shí)現(xiàn),,而對(duì)于"為何要這樣實(shí)現(xiàn)"以及實(shí)現(xiàn)的本質(zhì)卻解釋的不甚了了,在筆者看來(lái)這只是“授人以魚”而非筆者所推崇的“授人以魚的同時(shí),,授人以漁”,。在這篇文章中,筆者將總結(jié)前輩們的經(jīng)驗(yàn),,并結(jié)合自己的理解,,來(lái)從理論到實(shí)踐由淺入深的說(shuō)明“Struts2返回JSON數(shù)據(jù)”這一問(wèn)題。

 

JSON(JavaScript Object Notation)

 

首先來(lái)看一下JSON官方對(duì)于“JSON”的解釋:

JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式,。易于人閱讀和編寫,。同時(shí)也易于機(jī)器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個(gè)子集,。 JSON采用完全獨(dú)立于語(yǔ)言的文本格式,,但是也使用了類似于C語(yǔ)言家族的習(xí)慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言,。(更多內(nèi)容請(qǐng)參見(jiàn)JSON官網(wǎng)http:///json-zh.html)


JSON建構(gòu)于兩種結(jié)構(gòu):

“名稱/值”對(duì)的集合(A collection of name/value pairs),。不同的語(yǔ)言中,它被理解為對(duì)象(object),,紀(jì)錄(record),,結(jié)構(gòu)(struct),字典(dictionary),,哈希表(hash table),,有鍵列表(keyed list),或者關(guān)聯(lián)數(shù)組 (associative array),。


值的有序列表(An ordered list of values),。在大部分語(yǔ)言中,它被理解為數(shù)組(array),。

因?yàn)镴SON中的值(value)可以是雙引號(hào)括起來(lái)的字符串(string),、數(shù)值(number)、true,、false,、 null、對(duì)象(object)或者數(shù)組(array),,且這些結(jié)構(gòu)可以嵌套,,這種特性給予JSON表達(dá)數(shù)據(jù)以無(wú)限的可能:它既可以表達(dá)一個(gè)簡(jiǎn)單的key/value,也可以表達(dá)一個(gè)復(fù)雜的Map或List,而且它是易于閱讀和理解的,。

 

 

Struts2中JSON的用武之地


因?yàn)镴SON是脫離語(yǔ)言的理想的數(shù)據(jù)交換格式,所以它被頻繁的應(yīng)用在客戶端與服務(wù)器的通信過(guò)程中,,這一點(diǎn)是毋庸置疑的,。而在客戶端與服務(wù)器的通信過(guò)程中,,JSON數(shù)據(jù)的傳遞又被分為服務(wù)器向客戶端傳送JSON數(shù)據(jù),,和客戶端向服務(wù)器傳送JSON數(shù)據(jù),前者的核心過(guò)程中將對(duì)象轉(zhuǎn)換成JSON,,而后者的核心是將JSON轉(zhuǎn)換成對(duì)象,,這是本質(zhì)的區(qū)別。另外,,值得一提的是,,JSON數(shù)據(jù)在傳遞過(guò)程中,其實(shí)就是傳遞一個(gè)普通的符合JSON語(yǔ)法格式的字符串而已,,所謂的“JSON對(duì)象”是指對(duì)這個(gè)JSON字符串解析和包裝后的結(jié)果,,這一點(diǎn)請(qǐng)牢記,因?yàn)橄旅娴膬?nèi)容會(huì)依賴這一點(diǎn),。

 

Struts2返回JSON數(shù)據(jù)到客戶端


這是最常見(jiàn)的需求,,在AJAX大行其道的今天,向服務(wù)器請(qǐng)求JSON數(shù)據(jù)已成為每一個(gè)WEB應(yīng)用必備的功能,。拋開(kāi)Struts2暫且不提,,在常規(guī)WEB應(yīng)用中由服務(wù)器返回JSON數(shù)據(jù)到客戶端有兩種方式:一是在Servlet中輸出JSON串,二是在JSP頁(yè)面中輸出JSON串,。上文提到,,服務(wù)器像客戶端返回JSON數(shù)據(jù),其實(shí)就是返回一個(gè)符合JSON語(yǔ)法規(guī)范的字符串,,所以在上述兩種 方法中存在一個(gè)共同點(diǎn),,就是將需要返回的數(shù)據(jù)包裝稱符合JSON語(yǔ)法規(guī)范的字符串后在頁(yè)面中顯示。如下所示

 

使用Servlet返回JSON數(shù)據(jù)到客戶端:

 

Java代碼  收藏代碼
  1. package cn.ysh.studio.struts2.json.demo.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import net.sf.json.JSONObject;  
  12.   
  13. import cn.ysh.studio.struts2.json.demo.bean.User;  
  14.   
  15. public class JSON extends HttpServlet {  
  16.   
  17.     /** 
  18.      *  
  19.      */  
  20.     private static final long serialVersionUID = 1L;  
  21.   
  22.     /** 
  23.      * The doGet method of the servlet. <br> 
  24.      * 
  25.      * This method is called when a form has its tag value method equals to get. 
  26.      *  
  27.      * @param request the request send by the client to the server 
  28.      * @param response the response send by the server to the client 
  29.      * @throws ServletException if an error occurred 
  30.      * @throws IOException if an error occurred 
  31.      */  
  32.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  33.             throws ServletException, IOException {  
  34.   
  35.         response.setContentType("text/html");  
  36.         PrintWriter out = response.getWriter();  
  37.         //將要被返回到客戶端的對(duì)象  
  38.         User user=new User();  
  39.         user.setId("123");  
  40.         user.setName("JSONServlet");  
  41.         user.setPassword("JSON");  
  42.         user.setSay("Hello , i am a servlet !");  
  43.         JSONObject json=new JSONObject();  
  44.         json.accumulate("success"true);  
  45.         json.accumulate("user", user);  
  46.         out.println(json.toString());  
  47. //      因?yàn)镴SON數(shù)據(jù)在傳遞過(guò)程中是以普通字符串形式傳遞的,,所以我們也可以手動(dòng)拼接符合JSON語(yǔ)法規(guī)范的字符串輸出到客戶端  
  48. //      以下這兩句的作用與38-46行代碼的作用是一樣的,,將向客戶端返回一個(gè)User對(duì)象,和一個(gè)success字段  
  49. //      String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONServlet\",\"say\":\"Hello , i am a servlet !\",\"password\":\"JSON\"},\"success\":true}";  
  50. //      out.println(jsonString);  
  51.         out.flush();  
  52.         out.close();  
  53.     }  
  54.   
  55.     /** 
  56.      * The doPost method of the servlet. <br> 
  57.      * 
  58.      * This method is called when a form has its tag value method equals to post. 
  59.      *  
  60.      * @param request the request send by the client to the server 
  61.      * @param response the response send by the server to the client 
  62.      * @throws ServletException if an error occurred 
  63.      * @throws IOException if an error occurred 
  64.      */  
  65.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  66.             throws ServletException, IOException {  
  67.         doGet(request, response);  
  68.     }  
  69.   
  70. }  

 

結(jié)果在意料之中,,如下圖所示:

 

 

 

 

使用JSP(或html等)返回JSON數(shù)據(jù)到客戶端:

 

Java代碼  收藏代碼
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. {"user":{"id":"123","name":"JSONJSP","say":"Hello , i am a JSP !","password":"JSON"},"success":true}  

 

這個(gè)就不用去看結(jié)果了吧,。

 

再回到Struts,在Struts的MVC模型中,,Action替代Servlet擔(dān)當(dāng)了Model的角色,,所以對(duì)于Struts而言,返回JSON數(shù)據(jù)到客戶端,跟傳統(tǒng)的WEB應(yīng)用一樣,,存在兩種方式,,即在Action中輸出JSON數(shù)據(jù),和在視圖資源中輸出JSON數(shù)據(jù),。再往下細(xì)分的話,,在Action中輸出JSON數(shù)據(jù)又分為兩種方式,一是使用傳統(tǒng)方式輸出自己包裝后的JSON數(shù)據(jù),,二是使用Struts自帶的JSON數(shù)據(jù)封裝功能來(lái)自動(dòng)包裝并返回JSON數(shù)據(jù),。

 

在視圖資源中輸出JSON數(shù)據(jù)


Action處理完用戶請(qǐng)求后,將數(shù)據(jù)存放在某一位置,,如request中,,并返回視圖,然后Struts將跳轉(zhuǎn)至該視圖資源,,在該視圖中,,我們需要做的是將數(shù)據(jù)從存放位置中取出,然后將其轉(zhuǎn)換為JSON字符串,,輸出在視圖中,。這跟傳統(tǒng)WEB應(yīng)用中在JSP頁(yè)面輸出JSON數(shù)據(jù)的做法如出一轍:

 

Java代碼  收藏代碼
  1. public String testByJSP() {  
  2.         User user = new User();  
  3.         user.setId("123");  
  4.         user.setName("Struts2");  
  5.         user.setPassword("123");  
  6.         user.setSay("Hello world !");  
  7.         JSONObject jsonObject=new JSONObject();  
  8.         jsonObject.accumulate("user", user);  
  9.         //這里在request對(duì)象中放了一個(gè)data,所以struts的result配置中不能有type="redirect"  
  10.         ServletActionContext.getRequest().setAttribute("data", jsonObject.toString());  
  11.         return SUCCESS;  
  12.     };  

 

因?yàn)槭浅R?guī)的Struts流程配置,,所以配置內(nèi)容就不再展示了,。

 

JSP代碼就非常簡(jiǎn)單了,

 

Java代碼  收藏代碼
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. ${data }  

 

結(jié)果如圖所示:

 

 

 

 

在Action中以傳統(tǒng)方式輸出JSON數(shù)據(jù)


這一點(diǎn)跟傳統(tǒng)的Servlet的處理方式基本上一模一樣,代碼如下

 

Java代碼  收藏代碼
  1. public void doAction() throws IOException{  
  2.         HttpServletResponse response=ServletActionContext.getResponse();  
  3.         //以下代碼從JSON.java中拷過(guò)來(lái)的  
  4.         response.setContentType("text/html");  
  5.         PrintWriter out;  
  6.         out = response.getWriter();  
  7.         //將要被返回到客戶端的對(duì)象  
  8.         User user=new User();  
  9.         user.setId("123");  
  10.         user.setName("JSONActionGeneral");  
  11.         user.setPassword("JSON");  
  12.         user.setSay("Hello , i am a action to print a json!");  
  13.         JSONObject json=new JSONObject();  
  14.         json.accumulate("success"true);  
  15.         json.accumulate("user", user);  
  16.         out.println(json.toString());  
  17. //      因?yàn)镴SON數(shù)據(jù)在傳遞過(guò)程中是以普通字符串形式傳遞的,,所以我們也可以手動(dòng)拼接符合JSON語(yǔ)法規(guī)范的字符串輸出到客戶端  
  18. //      以下這兩句的作用與38-46行代碼的作用是一樣的,,將向客戶端返回一個(gè)User對(duì)象,和一個(gè)success字段  
  19. //      String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONActionGeneral\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";  
  20. //      out.println(jsonString);  
  21.         out.flush();  
  22.         out.close();  
  23.     }  

 

struts.xml中的配置:

 

Java代碼  收藏代碼
  1. <package name="default" extends="struts-default" namespace="/">  
  2.     <action name="testJSONFromActionByGeneral" class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="doAction">  
  3.     </action>  
  4. </package>  

 

注意:這個(gè)action沒(méi)有result,,且doAction方法沒(méi)有返回值!

 

就不再貼圖了,,因?yàn)榻Y(jié)果可想而知!

 

 

在Action中以Struts2的方式輸出JSON數(shù)據(jù)

本著“不重復(fù)發(fā)明輪子”的原則,我們將轉(zhuǎn)換JSON數(shù)據(jù)的工作交給Struts2來(lái)做,,那么相對(duì)于在Action中以傳統(tǒng)方式輸出JSON不同的是,,Action是需要將注意力放在業(yè)務(wù)處理上,而無(wú)需關(guān)心處理結(jié)果是如何被轉(zhuǎn)換成JSON被返回客戶端的——這些 工作通過(guò)簡(jiǎn)單的配置,,Struts2會(huì)幫我們做的更好,。

 

Java代碼  收藏代碼
  1. public String testByAction() {  
  2.         // dataMap中的數(shù)據(jù)將會(huì)被Struts2轉(zhuǎn)換成JSON字符串,所以這里要先清空其中的數(shù)據(jù)  
  3.         dataMap.clear();  
  4.         User user = new User();  
  5.         user.setId("123");  
  6.         user.setName("JSONActionStruts2");  
  7.         user.setPassword("123");  
  8.         user.setSay("Hello world !");  
  9.         dataMap.put("user", user);  
  10.         // 放入一個(gè)是否操作成功的標(biāo)識(shí)  
  11.         dataMap.put("success"true);  
  12.         // 返回結(jié)果  
  13.         return SUCCESS;  
  14.     }  

 

 

struts.xml中action的配置:

 

Java代碼  收藏代碼
  1. <package name="json" extends="json-default" namespace="/test">  
  2.         <action name="testByAction"  
  3.             class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="testByAction">  
  4.             <result type="json">  
  5.                 <!-- 這里指定將被Struts2序列化的屬性,,該屬性在action中必須有對(duì)應(yīng)的getter方法 -->  
  6.                 <param name="root">dataMap</param>  
  7.             </result>  
  8.         </action>  
  9. </package>  

 

凡是使用Struts2序列化對(duì)象到JSON的action,,所在的package必須繼承自json-default,注意,,這里唯一的result,,沒(méi)有指定name屬性,。

 

結(jié)果如下圖所示:

 

 

 

上面很詳細(xì)的說(shuō)明了在WEB應(yīng)用中如何返回JSON數(shù)據(jù)到客戶端,講了那么多種方式,,涉及的技術(shù)核心無(wú)非只有兩點(diǎn):


1,、將對(duì)象轉(zhuǎn)換成符合JSON語(yǔ)法格式的字符串;
2,、將符合JSON語(yǔ)法格式的字符串返回客戶端,;


第二點(diǎn)是整個(gè)實(shí)現(xiàn)過(guò)程的本質(zhì),但卻不難做到,;第一點(diǎn)其實(shí)也不難,他甚至有兩種做法,,一是通過(guò)字符串拼接方式,,而是通過(guò)JSONObject以對(duì)象方式轉(zhuǎn)換??聪旅娴囊粋€(gè)例子:

 

Java代碼  收藏代碼
  1. package cn.ysh.studio.struts2.json.demo.test;  
  2.   
  3. import cn.ysh.studio.struts2.json.demo.bean.User;  
  4. import net.sf.json.JSONObject;  
  5.   
  6. public class JSONTest {  
  7.   
  8.     /** 
  9.      * 將普通的pojo轉(zhuǎn)換成JSON字符串 
  10.      * @return 
  11.      */  
  12.     public JSONObject bean2json() {  
  13.         User user = new User();  
  14.         user.setId("JSONTest");  
  15.         user.setName("JSONTest");  
  16.         user.setPassword("JSON");  
  17.         user.setSay("Hello,i am JSONTest.java");  
  18.         JSONObject jsonObject = new JSONObject();  
  19.         jsonObject.accumulate("user", user);  
  20.         System.out.println("User轉(zhuǎn)換后的字符串:"+jsonObject.toString());  
  21.         return jsonObject;  
  22.     }  
  23.   
  24.     /** 
  25.      * 從JSONObject對(duì)象中反向解析出User對(duì)象 
  26.      * @param jsonObject 
  27.      */  
  28.     public void json2bean(JSONObject jsonObject) {  
  29.         User user=(User)JSONObject.toBean((JSONObject)jsonObject.get("user"),User.class);  
  30.         System.out.println("轉(zhuǎn)換得到的User對(duì)象的Name為:"+user.getName());  
  31.     }  
  32.   
  33.     public static void main(String[] s) {  
  34.         JSONTest tester=new JSONTest();  
  35.         tester.json2bean(tester.bean2json());  
  36.     }  
  37. }  

 

 

JSON格式的字符串返回到客戶端后,,客戶端會(huì)將其解析并封裝成真正的JSON對(duì)象,以供JS調(diào)用,。

 


總結(jié)上述,,其實(shí)只要明白了服務(wù)器返回JSON數(shù)據(jù)到客戶端的原理,做起來(lái)就游刃有余了,,他甚至有非常多的可選方案,,但既然是基于Struts2的實(shí)現(xiàn),那么肯定還是要用Struts2的方式來(lái)做啦,,因?yàn)檫@樣確實(shí)可以省很多事,。另外,在文章的最后,,說(shuō)明一下返回JSON數(shù)據(jù)時(shí)在result中配置的參數(shù)的含義及其常見(jiàn)常見(jiàn)配置吧:

 

Java代碼  收藏代碼
  1. <result type="json">  
  2.                 <!-- 這里指定將被Struts2序列化的屬性,,該屬性在action中必須有對(duì)應(yīng)的getter方法 -->  
  3.                 <!-- 默認(rèn)將會(huì)序列所有有返回值的getter方法的值,,而無(wú)論該方法是否有對(duì)應(yīng)屬性 -->  
  4.                 <param name="root">dataMap</param>  
  5.                 <!-- 指定是否序列化空的屬性 -->  
  6.                 <param name="excludeNullProperties">true</param>  
  7.                 <!-- 這里指定將序列化dataMap中的那些屬性 -->  
  8.                 <param name="includeProperties">  
  9.                     userList.*  
  10.                 </param>  
  11.                 <!-- 這里指定將要從dataMap中排除那些屬性,,這些排除的屬性將不被序列化,一半不與上邊的參數(shù)配置同時(shí)出現(xiàn) -->  
  12.                 <param name="excludeProperties">  
  13.                     SUCCESS  
  14.                 </param>  
  15. </result>  

 

值得一提的是通過(guò)Struts2來(lái)返回JSON數(shù)據(jù),,在IE中會(huì)提示下載,,這個(gè)不用關(guān)心,換個(gè)瀏覽器就能正常展示JSON數(shù)據(jù),而在JS調(diào)用中,更是毫無(wú)影響。

 

下面是整個(gè)Action的完整代碼:

 

Java代碼  收藏代碼
  1. package cn.ysh.studio.struts2.json.demo.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import net.sf.json.JSONObject;  
  13.   
  14. import cn.ysh.studio.struts2.json.demo.bean.User;  
  15.   
  16. import com.opensymphony.xwork2.ActionSupport;  
  17.   
  18. public class UserAction extends ActionSupport {  
  19.   
  20.     /** 
  21.      *  
  22.      */  
  23.     private static final long serialVersionUID = 1L;  
  24.   
  25.     //將會(huì)被Struts2序列化為JSON字符串的對(duì)象  
  26.     private Map<String, Object> dataMap;  
  27.   
  28.     /** 
  29.      * 構(gòu)造方法 
  30.      */  
  31.     public UserAction() {  
  32.         //初始化Map對(duì)象  
  33.         dataMap = new HashMap<String, Object>();  
  34.     }  
  35.   
  36.     /** 
  37.      * 測(cè)試通過(guò)action以視圖方式返回JSON數(shù)據(jù) 
  38.      * @return 
  39.      */  
  40.     public String testByJSP() {  
  41.         User user = new User();  
  42.         user.setId("123");  
  43.         user.setName("JSONActionJSP");  
  44.         user.setPassword("123");  
  45.         user.setSay("Hello world !");  
  46.         JSONObject jsonObject=new JSONObject();  
  47.         jsonObject.accumulate("user", user);  
  48.         jsonObject.accumulate("success"true);  
  49.         //這里在request對(duì)象中放了一個(gè)data,,所以struts的result配置中不能有type="redirect"  
  50.         ServletActionContext.getRequest().setAttribute("data", jsonObject.toString());  
  51.         return SUCCESS;  
  52.     };  
  53.   
  54.     /** 
  55.      * 測(cè)試通過(guò)action以Struts2默認(rèn)方式返回JSON數(shù)據(jù) 
  56.      * @return 
  57.      */  
  58.     public String testByAction() {  
  59.         // dataMap中的數(shù)據(jù)將會(huì)被Struts2轉(zhuǎn)換成JSON字符串,,所以這里要先清空其中的數(shù)據(jù)  
  60.         dataMap.clear();  
  61.         User user = new User();  
  62.         user.setId("123");  
  63.         user.setName("JSONActionStruts2");  
  64.         user.setPassword("123");  
  65.         user.setSay("Hello world !");  
  66.         dataMap.put("user", user);  
  67.         // 放入一個(gè)是否操作成功的標(biāo)識(shí)  
  68.         dataMap.put("success"true);  
  69.         // 返回結(jié)果  
  70.         return SUCCESS;  
  71.     }  
  72.   
  73.     /** 
  74.      * 通過(guò)action是以傳統(tǒng)方式返回JSON數(shù)據(jù) 
  75.      * @throws IOException 
  76.      */  
  77.     public void doAction() throws IOException{  
  78.         HttpServletResponse response=ServletActionContext.getResponse();  
  79.         //以下代碼從JSON.java中拷過(guò)來(lái)的  
  80.         response.setContentType("text/html");  
  81.         PrintWriter out;  
  82.         out = response.getWriter();  
  83.         //將要被返回到客戶端的對(duì)象  
  84.         User user=new User();  
  85.         user.setId("123");  
  86.         user.setName("JSONActionGeneral");  
  87.         user.setPassword("JSON");  
  88.         user.setSay("Hello , i am a action to print a json!");  
  89.         JSONObject json=new JSONObject();  
  90.         json.accumulate("success"true);  
  91.         json.accumulate("user", user);  
  92.         out.println(json.toString());  
  93. //      因?yàn)镴SON數(shù)據(jù)在傳遞過(guò)程中是以普通字符串形式傳遞的,,所以我們也可以手動(dòng)拼接符合JSON語(yǔ)法規(guī)范的字符串輸出到客戶端  
  94. //      以下這兩句的作用與38-46行代碼的作用是一樣的,將向客戶端返回一個(gè)User對(duì)象,,和一個(gè)success字段  
  95. //      String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONActionGeneral\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";  
  96. //      out.println(jsonString);  
  97.         out.flush();  
  98.         out.close();  
  99.     }  
  100.       
  101.     /** 
  102.      * Struts2序列化指定屬性時(shí),,必須有該屬性的getter方法,實(shí)際上,,如果沒(méi)有屬性,,而只有g(shù)etter方法也是可以的 
  103.      * @return 
  104.      */  
  105.     public Map<String, Object> getDataMap() {  
  106.         return dataMap;  
  107.     }  
  108.   
  109. }  

 

完整的struts.xml配置文件如下:

 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  3.     "http://struts./dtds/struts-2.0.dtd">  
  4. <struts>  
  5.     <package name="json" extends="json-default" namespace="/test">  
  6.         <action name="testByAction"  
  7.             class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="testByAction">  
  8.             <result type="json">  
  9.                 <!-- 這里指定將被Struts2序列化的屬性,,該屬性在action中必須有對(duì)應(yīng)的getter方法 -->  
  10.                 <!-- 默認(rèn)將會(huì)序列所有有返回值的getter方法的值,,而無(wú)論該方法是否有對(duì)應(yīng)屬性 -->  
  11.                 <param name="root">dataMap</param>  
  12.                 <!-- 指定是否序列化空的屬性 -->  
  13.                 <!-- 
  14.                 <param name="excludeNullProperties">true</param> 
  15.                 -->  
  16.                 <!-- 這里指定將序列化dataMap中的那些屬性 -->  
  17.                 <!--   
  18.                 <param name="includeProperties">  
  19.                     userList.*  
  20.                 </param>  
  21.                  -->  
  22.                 <!-- 這里指定將要從dataMap中排除那些屬性,這些排除的屬性將不被序列化,,一半不與上邊的參數(shù)配置同時(shí)出現(xiàn) -->  
  23.                 <!--   
  24.                 <param name="excludeProperties">  
  25.                     SUCCESS  
  26.                 </param>  
  27.                 -->  
  28.             </result>  
  29.         </action>  
  30.     </package>  
  31.     <package name="default" extends="struts-default" namespace="/">  
  32.         <action name="testJSONFromActionByGeneral"  
  33.             class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="doAction">  
  34.         </action>  
  35.         <action name="testByJSP"  
  36.             class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="testByJSP">  
  37.             <result name="success">/actionJSP.jsp</result>  
  38.         </action>  
  39.     </package>  
  40. </struts>  

 

 

最后,,附上整個(gè)范例工程(一個(gè)MyEclipse工程)源碼。

 

 

 

 

 

 原創(chuàng)文章,,轉(zhuǎn)載請(qǐng)注明出處:http://yshjava./blog/1333104

 

 

 

 

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多