畢設系列—服務端:Java-SSH框架(2)之服務器端發送JSON格式數據

1、序

由於業務(畢設)需要……客戶端和服務端需要進行數據通信,而數據通信的話,一般常用的就是JSON格式。基本原理就是,服務端將後臺數據庫中的數據讀取,並通過相關的工具類,將其轉換爲JSON格式。客戶端再將JSON格式的數據解析出來,從而實現了服務器(SSH)和客戶端(Android)之間的通信。

2、IDE

開發環境 jdk+mysql+myeclipse+tomcat

3、過程

步驟一 、新建一個User library,包含JSON所需要的jar
(1) 打開myeclipse,點擊上方菜單欄處【Window】,在彈出的子菜單中點擊【Preferences】
(2) 進入preferences界面, 點擊左側列表中java
(3) 在彈出的菜單欄中點擊【Bulid path】,然後打開【User library】
(4) 進入User library界面,可以看到當前我們已經創建的jar包,點擊右上方【new】
(5) 在彈出的對話框輸入 當前jar包名稱 “JSON“,建議直接以jar命名方便查看
(6) 創建user library完成,點擊選中該jar目錄,然後點擊右側【Add external jar】將我上傳的json包裏的jar導入即可。
JSON所需jar
PS:
詳情請見myeclipse2014如何創建user library
下載所需jar,鏈接JSON打包下載
注意其中的gson除了加入Build path以外,還要複製到WEB-INF/lib/,否則會出錯。

步驟二、新建一個ResUtil.java文件,作爲工具類,使用GSON將數據轉換成JSON字符串:
ResUtil.java

package com.toeat.util;

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

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class ResUtil {

    public static void toJson(HttpServletResponse response, Object data) 
        throws IOException {

        Gson gson = new Gson();
        String result = gson.toJson(data);
        response.setContentType("text/json; charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();

    }

}

步驟三、新建QueryUsersAction.java文件,用於封裝並返回所有用戶信息,訪問http://localhost:8080/ToEatServer/QueryUsers.action 即可看到及經過封裝過的JSON格式的數據

JSON格式的User數據

https://img-blog.csdn.net/20160517155144617

/**
 * author : sagewang
 * date : 下午4:42:48
 */
package com.toeat.action.user;

/**   
 *    
 * 項目名稱:ToEatServer   
 * 類名稱:QueryUsersAction   
 * 類描述:   
 * 創建人:wsqali   
 * 創建時間:2016年5月12日 下午4:42:48   
 * 修改人:wsqali   
 * 修改時間:2016年5月12日 下午4:42:48   
 * 修改備註:   
 * @version    
 *    
 */
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.toeat.bean.User;
import com.toeat.service.BaseService;
import com.toeat.util.ResUtil;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class QueryUsersAction {
    private BaseService baseService;
    public BaseService getBaseService() {
        return baseService;
    }
    public void setBaseService(BaseService baseService) {
        this.baseService = baseService;
    }

    private User user;
    // 封裝並返回所有用戶信息
    public String execute(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<User> userlist = baseService.ReadAll("User");
        try{        
            map.put("Users", userlist);
            ResUtil.toJson(ServletActionContext.getResponse(), map);
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
}

相關配置:

struts-user.xml

<package name="login" namespace="/" extends="struts-default">
    <!-- 查詢所有用戶信息 -->
    <action name="QueryUsers" class="QueryUsersAction"></action>
</package>

appliacation-user.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop     
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context     
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 查詢所有用戶信息 -->
    <bean id="QueryUsersAction" class="com.toeat.action.user.QueryUsersAction">
        <property name="baseService" ref="BaseService"></property>
    </bean>
</beans>

未完待續……

原創:畢設系列—服務端:服務器端發送JSON格式數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章