昨天到今天搞了一整天的電腦,我都快崩潰了! ,以後再也不能隨便的關機了,因為這次的不小心,幾乎所有的Net安裝,調試問題都被我碰到了個人感覺AJax+JSon在很大程度上降低了網絡和服務器的IO,是一個很不錯的組合!
1:json的lib我用的是JSon-lib-2.1-jdk15.jar,它可以在
2:struts用的是1.2
以下是例子中所用到的相關文件:
- /////////////////////////////////////// toolhxw.JS
- /**
- @hxw 20080602
- */
- //回調函數 簡單回調函數
- 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;
- }
- //回調函數 復雜回調函數
- 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數據
- function getesay(){
- var url = 'test.do';
- var pars = 'method=getEasy';
- var AJax = new AJax.Request(
- url,
- {method:'post',parameters:pars,onComplete:showesay}
- );
- }
- //獲取對象級復雜數據
- 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 {
- /**
- * 獲取簡單組合數據
- */
- public ActionForward getEasy(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- response.setContentType("text/Html; charset=GBK");
- try
- {