一 视图
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用JSON插件</title>
<script src="${pageContext.request.contextPath}/jquery-1.11.1.js"
type="text/javascript">
</script>
<script type="text/javascript">
function gotClick()
{
$("#show").hide();
// 指定向JSONExample发送请求,将id为form1的表单所包含的表单控件转换为请求参数
$.post("JSONExample" , $("#form1").serializeArray() ,
// 指定回调函数
function(data , statusText)
{
$("#show").height(80)
.width(240)
.css("border" , "1px solid black")
.css("border-radius" , "15px")
.css("background-color" , "#efef99")
.css("color" , "#ff0000")
.css("padding" , "20px")
.empty();
// 遍历JavaScript对象的各属性
for(var propName in data)
{
$("#show").append(propName + "-->"
+ data[propName] + "<br />");
}
$("#show").show(600);
},
// 指定服务器响应为JSON数据
"json");
}
</script>
</head>
<body>
<s:form id="form1">
<s:textfield name="field1" label="Field 1"/>
<s:textfield name="field2" label="Field 2"/>
<s:textfield name="field3" label="Field 3"/>
<tr><td colspan="2">
<input type="button" value="提交" onclick="gotClick();"/>
</td></tr>
</s:form>
<div id="show">
</div>
</body>
</html>
二 配置
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="example" extends="json-default">
<action name="JSONExample" class="org.crazyit.app.action.JSONExample">
<!-- 配置类型的json的Result -->
<result type="json">
<!-- 为该Result指定参数 -->
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<!-- 设置只序列Action的map属性 -->
<!-- param name="root">map</param -->
</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
三 action
package org.crazyit.app.action;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.json.annotations.JSON;
public class JSONExample
{
// 模拟处理结果的成员变量
private int[] ints = {10, 20};
private Map<String , String> map
= new HashMap<String , String>();
private String customName = "顾客";
// 封装请求参数的三个成员变量
private String field1;
// 'transient'修饰的成员变量不会被序列化
private transient String field2;
// 没有setter和getter方法的成员变量不会被序列化
private String field3;
public String execute()
{
map.put("name", "疯狂Java讲义");
return Action.SUCCESS;
}
// 使用注解来改变该成员变量序列化后的名字
@JSON(name="newName")
public Map getMap()
{
return this.map;
}
// customName的setter和getter方法
public void setCustomName(String customName)
{
this.customName = customName;
}
public String getCustomName()
{
return this.customName;
}
// field1的setter和getter方法
public void setField1(String field1)
{
this.field1 = field1;
}
public String getField1()
{
return this.field1;
}
// field2的setter和getter方法
public void setField2(String field2)
{
this.field2 = field2;
}
public String getField2()
{
return this.field2;
}
// field3的setter和getter方法
public void setField3(String field3)
{
this.field3 = field3;
}
public String getField3()
{
return this.field3;
}
}
四 测试