前段時間做項目用到了json,今天我抽時間寫了一個struts+ajax+json的例子.
個人感覺ajax+json在很大程度上降低了網(wǎng)絡和服務器的IO,是一個很不錯的組合!
1:json的lib我用的是json-lib-2.1-jdk15.jar,它可以在
2:struts用的是1.2
3:用到了js第三方prototype.js,主要用它包裝的ajax對象,大家也沒必要用這個,可以直接在js里用XMLHttpRequest。
以下是例子中所用到的相關文件:
/////////////////////////////////////// toolhxw.js
/**
@hxw 20080602
*/
//回調(diào)函數(shù) 簡單回調(diào)函數(shù)
function showesay(dataResponse)
{
var data = eval('(' + dataResponse.responseText + ')');
var str='';
str+='<ul>';
str+='<li>'+data.param1;+'</li>';
str+='<li>'+data.param2;+'</li>';
str+='</ul>';
document.getElementById("content").innerHTML=str;
}
//回調(diào)函數(shù) 復雜回調(diào)函數(shù)
function showcomplex(dataResponse)
{
var data = eval('(' + dataResponse.responseText + ')');
var str='';
for(var i=0;i<data.js.length;i++)
{
str+='<ul>';
str+='<li>'+data.js[i].id+'</li>';
str+='<li>'+data.js[i].age+'</li>';
str+='<li>'+data.js[i].name+'</li>';
str+='<li>'+data.js[i].address+'</li>';
str+='</ul>';
}
document.getElementById("content").innerHTML=str;
}
//獲取簡單的json數(shù)據(jù)
function getesay(){
var url = 'test.do';
var pars = 'method=getEasy';
var ajax = new Ajax.Request(
url,
{method:'post',parameters:pars,onComplete:showesay}
);
}
//獲取對象級復雜數(shù)據(jù)
function getcomplex(){
var url = 'test.do';
var pars = 'method=getComplex';
var ajax = new Ajax.Request(
url,
{method:'post',parameters:pars,onComplete:showcomplex}
);
}
///////////////////////////////////////struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/test" parameter="method" type="com.json.struts.action.TestAction">
</action>
</action-mappings>
<message-resources parameter="com.json.struts.ApplicationResources" />
</struts-config>
////////////////////////////////TestAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.json.struts.action;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import net.sf.json.*;
/**
* @author hxw
*
*/
public class TestAction extends DispatchAction {
/**
* 獲取簡單組合數(shù)據(jù)
*/
public ActionForward getEasy(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html; charset=GBK");
try
{
PrintWriter out = response.getWriter();
//這里的數(shù)據(jù)拼裝一般是從數(shù)據(jù)庫查詢來的
JSONObject jsonObject = new JSONObject();
jsonObject.put("param1", "變量一");
jsonObject.put("param2", "變量二");
out.print(jsonObject.toString());
out.flush();
out.close();
return null;
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
/**
* 獲取復雜組合數(shù)據(jù)
*/
public ActionForward getComplex(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html; charset=GBK");
try
{
PrintWriter out = response.getWriter();
JSONObject obj = new JSONObject();
JSONArray js = new JSONArray();
//這里的數(shù)據(jù)拼裝一般是從數(shù)據(jù)庫查詢來的
for(int i=0;i<3;i++)
{
JSONObject objtemp = new JSONObject();
objtemp.put("id", i);
objtemp.put("age", "23");
objtemp.put("name", "test"+i);
objtemp.put("address", "test");
js.add(objtemp);
}
obj.put("js",js);
out.print(obj.toString());
}catch(Exception e)
{
e.printStackTrace();
System.out.print("消費明細json存儲異常");
}
return null;
}
}