- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>test</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <script type="text/javascript" src="js/common/prototype.js"></script>
- <script language="javascript" type="text/javascript">
- function getJson()
- {
- var url = 'JSONExample.action';
- var pars = '';
- var myAjax = new Ajax.Request(url,{
- method:'get',
- encoding:'UTF-8',
- parameters:pars,
- onComplete:function(json){
- var JSONobj=eval('('+ json.responseText +')');
- var html="" ;
- var menuid="";
- html += "<li>" + JSONobj.newName +"</li>";
- var ints = JSONobj.ints;
- for (var i=0; i< ints.length; i++){
- html += "<li>" + ints[i] +"</li>";
- }
- var map = JSONobj.map;
- for(array in map){
- html += "<li>" + array + ":" + map[array] +"</li>";
- }
- $("item").innerHTML=html;
- }
- });
- }
- </script>
- <body>
- <input type="button" name="button" id="button" onclick="getJson()"value="點擊測試">
- <div id="item"></div>
- </body>
- </html>
struts.xml配置文件
- <package name="example" extends="json-default">
- <action name="JSONExample" class="jSONExample" method="executeJson">
- <result type="json"/>
- </action>
- </package>
action
- package action;
- import java.util.HashMap;
- import java.util.Map;
- import com.googlecode.jsonplugin.annotations.JSON;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * @author 王盛武 <a href="[email protected]"/>
- * @date Aug 1, 200812:10:35 PM
- */
- public class JSONExample extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 4588482034890372410L;
- // 封裝請求參數的三個屬性
- //format= 例如"yyyy-MM-dd'T'HH:mm:ss"。
- private String field1;
- private transient String field2;
- private String field3;
- // 封裝處理結果的屬性
- private int[] ints = { 10, 20 };
- @SuppressWarnings("unchecked")
- private Map map = new HashMap();
- private String customName = "custom";
- @SuppressWarnings("unchecked")
- public String executeJson() {
- map.put("name1", "sinlff1");
- map.put("name2", "sinlff2");
- map.put("name3", "sinlff3");
- return SUCCESS;
- }
- // 三個請求參數對應的setter和getter方法
- @JSON(serialize = false) //format
- public String getField1() {
- return field1;
- }
- public void setField1(String field1) {
- this.field1 = field1;
- }
- @JSON(serialize = false)
- public String getField2() {
- return field2;
- }
- public void setField2(String field2) {
- this.field2 = field2;
- }
- @JSON(serialize = false) //format
- public String getField3() {
- return field3;
- }
- public void setField3(String field3) {
- this.field3 = field3;
- }
- public void setCustomName(String customName) {
- this.customName = customName;
- }
- // 封裝處理結果的屬性的setter和getter方法
- public int[] getInts() {
- return ints;
- }
- public void setInts(int[] ints) {
- this.ints = ints;
- }
- @SuppressWarnings("unchecked")
- public Map getMap() {
- return map;
- }
- @SuppressWarnings("unchecked")
- public void setMap(Map map) {
- this.map = map;
- }
- // 使用註釋語法來改變該屬性序列化後的屬性名
- @JSON(name = "newName")
- public String getCustomName() {
- return this.customName;
- }
- }