位置:首頁 > Java技術 > Struts2教學 > Struts2和JSON實例

Struts2和JSON實例

在這個Struts2例子,將學習如何通過“struts2-json-plugin.jar”庫將對象轉換為JSON格式的數據。

1. Action (JSON)

這是一個將被轉換成JSON格式的 Action 類。
package com.gitbook.netmon.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;

public class JSONDataAction{

	private String string1 = "A";
	private String[] stringarray1 = {"A1","B1"};
	private int number1 = 123456789;
	private int[] numberarray1 = {1,2,3,4,5,6,7,8,9};
	private List<String> lists = new ArrayList<String>();
	private Map<String, String> maps = new HashMap<String, String>();
	
	//no getter method, will not include in the JSON
	private String string2 = "B";
	
	public JSONDataAction(){
		lists.add("list1");
		lists.add("list2");
		lists.add("list3");
		lists.add("list4");
		lists.add("list5");
		
		maps.put("key1", "value1");
		maps.put("key2", "value2");
		maps.put("key3", "value3");
		maps.put("key4", "value4");
		maps.put("key5", "value5");
	}

	public String execute() {
               return Action.SUCCESS;
        }
	
	public String getString1() {
		return string1;
	}

	public void setString1(String string1) {
		this.string1 = string1;
	}

	public String[] getStringarray1() {
		return stringarray1;
	}

	public void setStringarray1(String[] stringarray1) {
		this.stringarray1 = stringarray1;
	}

	public int getNumber1() {
		return number1;
	}

	public void setNumber1(int number1) {
		this.number1 = number1;
	}

	public int[] getNumberarray1() {
		return numberarray1;
	}

	public void setNumberarray1(int[] numberarray1) {
		this.numberarray1 = numberarray1;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}
	
}

3. struts.xml

要輸出JSON數據,需要聲明一個包,它擴展“json-default”,會將結果類型轉為“json”。
<?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>

   <constant name="struts.devMode" value="true" />
	
   <package name="default" namespace="/" extends="json-default">
      <action name="getJSONResult" 
           class="com.gitbook.netmon.action.JSONDataAction">
       	   <result type="json" />
      </action>
    </package>
	
</struts>

4. 實例

訪問動作URL時,JSONDataAction屬性將被轉換成JSON格式。

http://localhost:8080/struts2json/getJSONResult.action

JSON 格式 …

{
   "lists":["list-1","list-2","list-3","list-4","list-5"],
   "maps":
   {
     "key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1"
   },
   "number1":123456789,
   "numberarray1":[1,2,3,4,5,6,7,8,9],
   "string1":"A",
   "stringarray1":["A1","B1"]
} 
希望這個簡單的例子可以了解JSON插件在Struts2是如何工作的有一個總體的思路。不過,還是有很多有用的參數不包括在這裡,請務必閱讀 Struts2 JSON插件文檔 以獲取更多詳細信息。

下載源代碼(struts2json) – http://pan.baidu.com/s/1bnv8l9X