構(gòu)建Json插件的相關(guān)技巧說明介紹
作者:佚名
前段時間做項目用到了JSON,今天我抽時間寫了一個struts+ajax+json的例子,個人感覺ajax+json在很大程度上降低了網(wǎng)絡(luò)和服務(wù)器的IO,是一個很不錯的組合
昨天到今天搞了一整天的電腦,我都快崩潰了! ,以后再也不能隨便的關(guān)機了,因為這次的不小心,幾乎所有的Net安裝,調(diào)試問題都被我碰到了個人感覺ajax+json在很大程度上降低了網(wǎng)絡(luò)和服務(wù)器的IO,是一個很不錯的組合!
1:json的lib我用的是json-lib-2.1-jdk15.jar,它可以在
2:struts用的是1.2#t#
3:用到了js第三方prototype.js,主要用它包裝的ajax對象,大家也沒必要用這個,可以直接在js里用XMLHttpRequest。
以下是例子中所用到的相關(guān)文件:
- /////////////////////////////////////// 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
- {
責任編輯:chenqingxiang
來源:
計世網(wǎng)