日期:2014-05-16  浏览次数:20409 次

Struts2整合JSON

首先在http://code.google.com/p/jsonplugin/downloads/list下载jsonplugin-0.34.jar

之后将jsonplugin-0.34.jar放入WEB-INF/lib

?

之后使用prototype.js

?

Action代码

?

package com.platform.Action.SDCategory;

import java.util.HashMap;
import java.util.Map;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class JsonExampleAction extends ActionSupport
{
	private String field1;
	private transient String field2;
	private String field3;
	
	private int[] ints = {10,20};
	private Map map = new HashMap();
	private String customName = "fini";
	public String getField1()
	{
		return field1;
	}
	public void setField1(String field1)
	{
		this.field1 = field1;
	}
	public String getField2()
	{
		return field2;
	}
	public void setField2(String field2)
	{
		this.field2 = field2;
	}
	public String getField3()
	{
		return field3;
	}
	public void setField3(String field3)
	{
		this.field3 = field3;
	}
	public int[] getInts()
	{
		return ints;
	}
	public void setInts(int[] ints)
	{
		this.ints = ints;
	}
	public Map getMap()
	{
		return map;
	}
	public void setMap(Map map)
	{
		this.map = map;
	}
	@JSON(name="newName")
	public String getCustomName()
	{
		return customName;
	}
	public void setCustomName(String customName)
	{
		this.customName = customName;
	}
	
	
	public String execute() throws Exception
	{
		map.put("name", "fuck");
		return SUCCESS;
	}
}

?

struts.xml

?

<package name="json" extends="json-default">
		<action name="QueryChildCategory" class="QueryChildCategoryAction">
			<result type="json"></result>
		</action>
	</package>

?

?

jsp页面

<script src="prototype.js" type="text/javascript"></script>
		<script type="text/javascript">
			function gotClick()
			{
				var url = 'JsonExample.action';
				var params = Form.serialize('form1');
				var myAjax = new Ajax.Request(url,{method:'post',parameters:params,onComplete:processResponse,asynchronous:true});
			}
			function processResponse(request)
			{
				var json = request.responseText
????var jsonData = eval("("+json+")");
????document.getElementById("show").innerHTML=jsonData["field1"];			}
		</script>
<form id="form1" name="form1" method="post">
				<input type="text" name="field1" id="field1"/><br>
				<input type="text" name="field2" id="field2"/><br>
				<input type="text" name="field3" id="field3"/><br>
				<input type="button" value="提交" onClick="gotClick();"/>
			</form>
			<div id="show"></div>

?

?

?或链接调用

script src="prototype.js" type="text/javascript"></script>
		<script type="text/javascript">
			function gotClick()
			{
				var url = 'JsonExample.action?no-cache='+Math.random();
				var params = "";
				var myAjax = new Ajax.Request(url,{method:'get',parameters:params,onComplete:processResponse});
			}
			function processResponse(request)
			{
				var json = request.responseText
				var jsonData = eval("("+json+")");
				//document.getElementById("show").innerHTML=jsonData["fuck"];
				document.getElementById("show").innerHTML=request.responseText;
			}
		</script>
<a href="javascript:void(0)" onclick="gotClick();">afafa</a>
			<div id="show"></div>

?var url = 'JsonExample.action?no-cache='+Math.random();无缓存

?

由于json-plugin会把action中的所有getter方法的属性序列化成json串,所以在整合