深刻了解Struts2返回JSON數據的原理

首先來看一下JSON官方對於「JSON」的解釋:html

JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。它基於JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集。 JSON採用徹底獨立於語言的文本格式,可是也使用了相似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。這些特性使JSON成爲理想的數據交換語言。(更多內容請參見JSON官網http://json.org/json-zh.html)java


JSON建構於兩種結構:     apache

「名稱/值」對的集合(A collection of name/value pairs)。     json

        { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }數組

不一樣的語言中,它被理解爲對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。瀏覽器

 


值的有序列表(An ordered list of values)。在大部分語言中,它被理解爲數組(array)。服務器

        { "people": [jsp

        { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },post

        { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},測試

        { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

        ]}

由於JSON中的值(value)能夠是雙引號括起來的字符串(string)、數值(number)、true、false、 null、對象(object)或者數組(array),且這些結構能夠嵌套,這種特性給予JSON表達數據以無限的可能:它既能夠表達一個簡單的 key/value,也能夠表達一個複雜的Map或List,並且它是易於閱讀和理解的。

 

Struts2中JSON的用武之地


由於JSON是脫離語言的理想的數據交換格式,因此它被頻繁的應用在客戶端與服務器的通訊過程當中,這一點是毋庸置疑的。而在客戶端與服務器的通訊過程當中,JSON數據的傳遞又被分爲服務器向客戶端傳送JSON數據,和客戶端向服務器傳送JSON數據,前者的核心過程當中將對象轉換成JSON,然後者的核心是將JSON轉換成對象,這是本質的區別。另外,值得一提的是,JSON數據在傳遞過程當中,其實就是傳遞一個普通的符合JSON語法格式的字符串而已,所謂的「JSON對象」是指對這個JSON字符串解析和包裝後的結果,這一點請牢記,由於下面的內容會依賴這一點。

 

Struts2返回JSON數據到客戶端


這是最多見的需求,在AJAX大行其道的今天,向服務器請求JSON數據已成爲每個WEB應用必備的功能。拋開Struts2暫且不提,在常規WEB應用中由服務器返回JSON數據到客戶端有兩種方式:一是在Servlet中輸出JSON串,二是在JSP頁面中輸出JSON串。上文提到,服務器像客戶端返回JSON數據,其實就是返回一個符合JSON語法規範的字符串,因此在上述兩種 方法中存在一個共同點,就是將須要返回的數據包裝稱符合JSON語法規範的字符串後在頁面中顯示。以下所示

 

使用Servlet返回JSON數據到客戶端:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import cn.ysh.studio.struts2.json.demo.bean.User;

public class JSON extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //將要被返回到客戶端的對象
        User user=new User();
        user.setId("123");
        user.setName("JSONServlet");
        user.setPassword("JSON");
        user.setSay("Hello , i am a servlet !");
        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\":\"JSONServlet\",\"say\":\"Hello , i am a servlet !\",\"password\":\"JSON\"},\"success\":true}";
//        out.println(jsonString);
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

再回到Struts,在Struts的MVC模型中,Action替代Servlet擔當了Model的角色,因此對於Struts而言,返回 JSON數據到客戶端,跟傳統的WEB應用同樣,存在兩種方式,即在Action中輸出JSON數據,和在視圖資源中輸出JSON數據。再往下細分的話,在Action中輸出JSON數據又分爲兩種方式,一是使用傳統方式輸出本身包裝後的JSON數據,二是使用Struts自帶的JSON數據封裝功能來自動包裝並返回JSON數據。

 

在視圖資源中輸出JSON數據


Action處理完用戶請求後,將數據存放在某一位置,如request中,並返回視圖,而後Struts將跳轉至該視圖資源,在該視圖中,咱們須要作的是將數據從存放位置中取出,而後將其轉換爲JSON字符串,輸出在視圖中。這跟傳統WEB應用中在JSP頁面輸出JSON數據的作法一模一樣:

public String testByJSP() {
        User user = new User();
        user.setId("123");
        user.setName("Struts2");
        user.setPassword("123");
        user.setSay("Hello world !");
        JSONObject jsonObject=new JSONObject();
        jsonObject.accumulate("user", user);
        //這裏在request對象中放了一個data,因此struts的result配置中不能有type="redirect"
        ServletActionContext.getRequest().setAttribute("data", jsonObject.toString());
        return SUCCESS;
    };

 

在Action中以傳統方式輸出JSON數據


這一點跟傳統的Servlet的處理方式基本上如出一轍,代碼以下

 

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();
    }

struts.xml中的配置:

<package name="default" extends="struts-default" namespace="/">
    <action name="testJSONFromActionByGeneral" class="cn.ysh.studio.struts2.json.demo.action.UserAction" method="doAction">
    </action>
</package>

注意:這個action沒有result,且doAction方法沒有返回值!

 

在Action中以Struts2的方式輸出JSON數據

本着「不重複發明輪子」的原則,咱們將轉換JSON數據的工做交給Struts2來作,那麼相對於在Action中以傳統方式輸出JSON不一樣的是,Action是須要將注意力放在業務處理上,而無需關心處理結果是如何被轉換成JSON被返回客戶端的——這些 工做經過簡單的配置,Struts2會幫咱們作的更好。

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;
    }

struts.xml中action的配置:

<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方法 -->
                <param name="root">dataMap</param>
            </result>
        </action>
</package>

凡是使用Struts2序列化對象到JSON的action,所在的package必須繼承自json-default,注意,這裏惟一的result,沒有指定name屬性。

 

上面很詳細的說明了在WEB應用中如何返回JSON數據到客戶端,講了那麼多種方式,涉及的技術核心無非只有兩點:

一、將對象轉換成符合JSON語法格式的字符串;
二、將符合JSON語法格式的字符串返回客戶端;


第二點是整個實現過程的本質,但卻不難作到;第一點其實也不難,他甚至有兩種作法,一是經過字符串拼接方式,而是經過JSONObject以對象方式轉換。看下面的一個例子:

import cn.ysh.studio.struts2.json.demo.bean.User;
import net.sf.json.JSONObject;

public class JSONTest {

    /**
     * 將普通的pojo轉換成JSON字符串
     * @return
     */
    public JSONObject bean2json() {
        User user = new User();
        user.setId("JSONTest");
        user.setName("JSONTest");
        user.setPassword("JSON");
        user.setSay("Hello,i am JSONTest.java");
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("user", user);
        System.out.println("User轉換後的字符串:"+jsonObject.toString());
        return jsonObject;
    }

    /**
     * 從JSONObject對象中反向解析出User對象
     * @param jsonObject
     */
    public void json2bean(JSONObject jsonObject) {
        User user=(User)JSONObject.toBean((JSONObject)jsonObject.get("user"),User.class);
        System.out.println("轉換獲得的User對象的Name爲:"+user.getName());
    }

    public static void main(String[] s) {
        JSONTest tester=new JSONTest();
        tester.json2bean(tester.bean2json());
    }
}

JSON格式的字符串返回到客戶端後,客戶端會將其解析並封裝成真正的JSON對象,以供JS調用。

 


總結上述,其實只要明白了服務器返回JSON數據到客戶端的原理,作起來就遊刃有餘了,他甚至有很是多的可選方案,但既然是基於 Struts2的實現,那麼確定仍是要用Struts2的方式來作啦,由於這樣確實能夠省不少事。另外,在文章的最後,說明一下返回JSON數據時在 result中配置的參數的含義及其常見常見配置吧:

<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>

值得一提的是經過Struts2來返回JSON數據,在IE中會提示下載,這個不用關心,換個瀏覽器就能正常展現JSON數據,而在JS調用中,更是毫無影響。

 

下面是整個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配置文件以下:

<?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>

轉載自   http://yshjava.iteye.com/blog/1333104

相關文章
相關標籤/搜索