JSONÀý×Ó£¨struts ajax json£©
À´Ô´£ºhaoxuewu - BlogJava
¡¡¡¡Ç°¶Îʱ¼ä×öÏîÄ¿Óõ½ÁËjson£¬½ñÌìÎÒ³éʱ¼äдÁËÒ»¸östruts+ajax+jsonµÄÀý×Ó.
¡¡¡¡¸öÈ˸оõajax+jsonÔںܴó³Ì¶ÈÉϽµµÍÁËÍøÂçºÍ·þÎñÆ÷µÄ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
*/
//»Øµ÷º¯Êý ¼òµ¥»Øµ÷º¯Êý
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"
Ïà¹ØÎĵµ£º
²ÉÓÃAjax´«µÝ²ÎÊý¼ÓºÅ(+)ºÍÓë·ûºÅ(&)ʱºò£¬·þÎñ¶Ë»ñÈ¡µ½µÄ²ÎÊý²¢²»ÈçÒ⣡
(1) "+"ºÅ£ºJavaScript½âÎöΪ×Ö·û´®Á¬½Ó·û£¬ËùÒÔ·þÎñÆ÷¶Ë½ÓÊÕÊý¾Ýʱ"+"»á¶ªÊ§¡£
(2)"&"£ºJavaScript½âÎöΪ±äÁ¿Á¬½Ó·û£¬ËùÒÔ·þÎñÆ÷¶Ë½ÓÊÕÊý¾Ýʱ&·ûºÅÒÔºóµÄÊý¾Ý¶¼»á¶ªÊ§¡£
½â¾ö°ì·¨£ºÔÚ´«µ½·þÎñ¶Ë֮ǰÏȽ«²ÎÊýÖеÄ"+"ºÍ"&"·ûºÅ¶¼± ......
//´´½¨XMLHttpRequest¶ÔÏó£¬ÐèÒª¸ù¾ÝIEºÍä¯ÀÀÆ÷ÀàÐÍ´´½¨²»Í¬µÄxhr¶ÔÏó
var xhrobj;
function check()
{
//»ñÈ¡Îı¾¿òµÄÖµ
var username = document.getElementById("username").value;
if(window.XMLHttpRequest)
{
//Õë¶Ôfirefox£¬Mozilla£¬ie7£¬ie8µÈ
xhrobj = new XMLHttpRequest();
/ ......
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<mce:style type="text/css"& ......
Ajax¿ÉÒÔʵÏÖÍøÒ³µÄ¾Ö²¿Ë¢ÐÂ,ÊÇͨ¹ýJS»ñÈ¡Êý¾Ý,ÔÙ°ÑÊý¾ÝÌí¼Óµ½ÍøÒ³ÖÐ,È»¶øÓÉÓÚä¯ÀÀÆ÷µÄ²»Í¬,³õѧÕߺÜÄÑ×öµ½¼æÈÝÎÊÌâ.
ÕâÀïÎÒÏò´ó¼Ò½éÉÜÒ»Öֺõķ½·¨.ͨ¹ýJQuery²å¼þ.JQuery.jsÎļþ¿ÉÒÔÔÚ¹ÙÍøÉÏÏÂÔØ,»òÕßgoogleÒ²ÐÐ.
Ê×ÏÈÄãÒªÔÚÍøÒ³ÒýÈëJSÎļþ
<script src="../javascript/jquery.js" type="text/javascript"></ ......