国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
struts+ajax+json
 

前段時間做項目用到了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;
}
}

////////////////////////////test.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json練習</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">
<script type="text/javascript"  src="js/prototype.js"></script>
<script type="text/javascript"  src="js/toolhxw.js"></script>
</head>
 
<body>
<div id="func" >
<a href='javascript:getesay()'>獲取簡單組合數(shù)據(jù)</a>
<a href='javascript:getcomplex()'>獲取復雜組合數(shù)據(jù)</a>
</div>
<div id="content">
正在獲取內(nèi)容...
</div>
</body>
</html>
大家將這幾個文件拷貝到你的myeclipse的web項目中,發(fā)布運行即可。
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
js與struts如何通過aja以json數(shù)據(jù)形式進行數(shù)據(jù)傳輸
JS中的JSON對象
采用Jquery+Ajax+PHP+MySQL實現(xiàn)分類列表管理 上
Ajax三種實現(xiàn)方法與AJAX解析JSON
通過Jquery中Ajax獲取json文件數(shù)據(jù)
Struts2+Ajax+JQuery+Json
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服