📄 基于ajax和json从javascript中调用后台java方法的jsongateway_西风吹雨的blog_新浪博客.mht
字号:
<DIV class=3D"jifen end"><A =
class=3Dgray>=E5=8D=9A=E5=AE=A2=E8=AE=BF=E9=97=AE</A>=EF=BC=9A<SPAN =
class=3Dscore=20
id=3DblogInfoCount>0 =E6=AC=A1</SPAN></DIV></DIV></DIV>
<DIV class=3DcomponentBottom></DIV></DIV>
<DIV class=3Dcomponent id=3Dblog_903>
<DIV class=3DcomponentBar><SPAN =
class=3Dtitle>=E7=9B=B8=E5=85=B3=E5=8D=9A=E6=96=87</SPAN></DIV>
<DIV class=3DcomponentContent>
<CENTER>=E5=86=85=E5=AE=B9=E8=AF=BB=E5=8F=96=E4=B8=AD=E2=80=A6</CENTER></=
DIV>
<DIV class=3DcomponentBottom></DIV></DIV>
<DIV class=3Dcomponent id=3Dblog_904>
<DIV class=3DcomponentBar><SPAN =
class=3Dtitle>=E6=8E=A8=E8=8D=90=E5=8D=9A=E6=96=87</SPAN></DIV>
<DIV class=3DcomponentContent>
<CENTER>=E5=86=85=E5=AE=B9=E8=AF=BB=E5=8F=96=E4=B8=AD=E2=80=A6</CENTER></=
DIV>
<DIV class=3DcomponentBottom></DIV></DIV>
<DIV class=3Dcomponent id=3Dblog_12>
<DIV class=3DcomponentBar><SPAN =
class=3Dtitle>=E8=B0=81=E7=9C=8B=E8=BF=87=E8=BF=99=E7=AF=87=E5=8D=9A=E6=96=
=87</SPAN></DIV>
<DIV class=3DcomponentContent>
<CENTER>=E5=86=85=E5=AE=B9=E8=AF=BB=E5=8F=96=E4=B8=AD=E2=80=A6</CENTER></=
DIV>
<DIV class=3DcomponentBottom></DIV></DIV></DIV></DIV>
<DIV class=3D"gap floatLeft"></DIV>
<DIV class=3D"column_2 floatLeft">
<DIV class=3D"components730 SinaAd_hide" id=3Dadvertise_top></DIV>
<DIV class=3Dcomponents730>
<DIV class=3Dcomponent>
<DIV class=3DcomponentBar><SPAN class=3D"floatRight articleFontSize"=20
id=3DarticleFontManage>=E5=AD=97=E4=BD=93=E5=A4=A7=E5=B0=8F=EF=BC=9A<A =
onclick=3D"changeFontSize(2);return false;"=20
href=3D"http://blog.sina.com.cn/s/">=E5=A4=A7</A><B>=E4=B8=AD</B><A=20
onclick=3D"changeFontSize(0);return false;"=20
href=3D"http://blog.sina.com.cn/s/">=E5=B0=8F</A></SPAN> <SPAN =
class=3Dtitle>=E6=AD=A3=E6=96=87</SPAN> <SPAN=20
class=3DARTICLE_HOT></SPAN></DIV>
<DIV class=3D"componentContent article">
<DIV class=3DarticleTitle>
<DIV style=3D"DISPLAY: inline"><B=20
id=3Dt_3fd7a127010009fr>=E5=9F=BA=E4=BA=8EAjax=E5=92=8CJSON=E4=BB=8Ejavas=
cript=E4=B8=AD=E8=B0=83=E7=94=A8=E5=90=8E=E5=8F=B0java=E6=96=B9=E6=B3=95=E7=
=9A=84JsonGateway</B> <SPAN=20
class=3Dtime>(2007-06-17 12:25:11)</SPAN> </DIV></DIV>
<DIV class=3DarticleTag>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
<TBODY>
<TR>
<TD>
<SCRIPT>=0A=
var $tag=3D'';=0A=
var $tag_code=3D'2cdb3ea19b008620a19310ff191a83b2';=0A=
</SCRIPT>
</TD>
<TD vAlign=3Dtop align=3Dright =
width=3D225></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DarticleContent id=3DarticleBody>
<DIV>
<P> <WBR> <WBR> <WBR> <WBR>JsonGateway=E6=98=AF=E4=B8=
=80=E4=B8=AAjava=E8=BF=9C=E7=A8=8B=E6=9C=8D=E5=8A=A1=E8=B0=83=E7=94=A8=E7=
=9A=84ajax=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=AE=83=E4=BD=BF=E7=94=A8=
Json=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E5=9C=A8=E9=A1=B5=E9=9D=A2=E4=B8=
=8A=E7=9A=84javascript=E5=92=8C=E5=90=8E=E5=8F=B0=E7=9A=84java=E6=9C=8D=E5=
=8A=A1=E4=B9=8B=E9=97=B4=E8=BF=9B=E8=A1=8C=E6=95=B0=E6=8D=AE=E4=BA=A4=E6=8D=
=A2=E3=80=82=E7=9B=AE=E5=89=8D=E5=AE=83=E5=8F=AF=E4=BB=A5=E8=B0=83=E7=94=A8=
spring=E7=9A=84service=EF=BC=8C=E5=BD=93=E7=84=B6=E4=B9=9F=E5=8F=AF=E4=BB=
=A5=E8=B0=83=E7=94=A8=E6=99=AE=E9=80=9A=E7=9A=84java=E7=B1=BB=E6=96=B9=E6=
=B3=95=E3=80=82jsonGateway=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=8A=A8=E7=94=9F=E6=
=88=90=E6=89=80=E9=85=8D=E7=BD=AE=E7=9A=84java=E7=B1=BB=E7=9A=84javascrip=
t=E5=8C=85=E8=A3=85=E7=B1=BB=EF=BC=8C=E8=BF=99=E5=B0=86=E4=BD=BF=E4=BD=A0=
=E8=83=BD=E5=A4=9F=E8=BD=BB=E6=9D=BE=E8=BF=9B=E8=A1=8Cajax=E8=BF=9C=E7=A8=
=8B=E8=B0=83=E7=94=A8=E3=80=82jsonGateway=E4=BD=BF=E7=94=A8=E4=BA=86JQuer=
y=E6=9D=A5=E8=BF=9B=E8=A1=8C=E8=BF=9C=E7=A8=8B=E8=B0=83=E7=94=A8=EF=BC=8C=
=E5=90=8C=E6=97=B6=E5=9C=A8=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=BD=BF=E7=94=A8=E4=
=BA=86jsontools=E8=BF=9B=E8=A1=8Cjson=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=
=E7=9A=84=E8=BD=AC=E6=8D=A2=E3=80=82</P>
<P><STRONG><A=20
href=3D"http://sourceforge.net/project/showfiles.php?group_id=3D188794"=20
target=3D_blank>=E4=BD=A0=E5=8F=AF=E4=BB=A5=E5=9C=A8Sorceforge=E4=B8=8A=E4=
=B8=8B=E8=BD=BD</A></STRONG></P>
<P>=E4=BD=BF=E7=94=A8=E6=96=B9=E6=B3=95=EF=BC=9A</P>
<P>=E5=A6=82=E6=9E=9C=E5=90=8E=E5=8F=B0=E6=88=91=E4=BB=AC=E4=BD=BF=E7=94=A8=
=E4=BA=86Spring,=E9=82=A3=E4=B9=88=E8=B0=83=E7=94=A8Spring=E4=B8=AD=E9=85=
=8D=E7=BD=AE=E7=9A=84Service =
Bean=E7=9A=84=E6=96=B9=E6=B3=95=E7=9A=84=E6=96=B9=E5=BC=8F=E4=B8=BA</P>
<P>=EF=BC=9A<BR> <WBR> <FONT=20
color=3D#0000ff>=E9=85=8D=E7=BD=AEJsonGateway=E7=9A=84Servlet</FONT>:&nbs=
p;<WBR> <WBR>=E5=9C=A8web.xml=E4=B8=AD, <WBR>=E5=A2=9E=E5=8A=A0=
<WBR><BR> <WBR> <WBR> <WBR>=20
<servlet><BR> <WBR> <WBR> <WBR> <WBR> <WBR=
> <WBR> <WBR>=20
<description>json=20
gateway</description><BR> <WBR> <WBR> <WBR> <WB=
R> <WBR> <WBR> <WBR>=20
<display-name>json gateway=20
servlet</display-name><BR> <WBR> <WBR> <WBR> <W=
BR> <WBR> <WBR> <WBR>=20
<servlet-name>json-gateway</servlet-name><BR> <WBR> =
;<WBR> <WBR> <WBR> <WBR> <WBR> <WBR>=20
<servlet-class>org.svilo.spring.json.JsonGatewayServlet</servlet=
-class><BR> <WBR> <WBR> <WBR>=20
</servlet></P>
<P> <WBR> <WBR> <WBR>=20
<servlet-mapping><BR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR> <WBR> <WBR>=20
<servlet-name>json-gateway</servlet-name><BR> <WBR> =
;<WBR> <WBR> <WBR> <WBR> <WBR> <WBR>=20
<url-pattern>/json/*</url-pattern><BR> <WBR> <WBR>&=
nbsp;<WBR>=20
</servlet-mapping></P>
<P><FONT =
color=3D#0000bf>=E5=9C=A8html=E3=80=81jsp=E9=A1=B5=E9=9D=A2=E4=B8=AD=EF=BC=
=8C=E5=BC=95=E5=85=A5=E5=A6=82=E4=B8=8B=E7=9A=84javascript</FONT>:<BR>&nb=
sp;<WBR>=20
<script type=3D"text/javascript"=20
src=3D"/js/jquery.js"></script><BR> <WBR> <script=20
type=3D"text/javascript" =
src=3D"/js/json.js"></script><BR> <WBR>=20
<script type=3D"text/javascript"=20
src=3D"/js/moo.js"></script><BR> <WBR> <script=20
type=3D"text/javascript" =
src=3D"/js/jsonGateway.js"></script></P>
<P><FONT =
color=3D#0000bf>=E5=90=8C=E6=97=B6=E4=B9=9F=E9=9C=80=E8=A6=81=E5=BC=95=E5=
=85=A5=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E7=9A=84=E6=9C=8D=E5=8A=A1=E4=BB=
=A3=E7=90=86Javascript=EF=BC=9A</FONT></P>
<P> <WBR> <WBR>=20
=E5=A6=82=E6=9E=9C=E4=BD=A0=E6=9C=89=E4=B8=80=E4=B8=AASpring=E7=9A=84=E6=9C=
=8D=E5=8A=A1bean=E5=8F=AB=E5=81=9A'CustomerMgrService',=E9=82=A3=E4=B9=88=
=E5=9C=A8=E4=BD=A0=E7=9A=84html=E4=B8=AD=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=A6=
=82=E4=B8=8B=E5=87=A0=E8=A1=8C=E4=BB=A3=E7=A0=81=EF=BC=9A</P>
<P> <WBR> <WBR> <script=20
type=3D"text/javascript"src=3D"/json/CustomerMgrService.js"></scrip=
t> <WBR> <WBR>=20
//the CustomerMgrService.js is generated by the JsonGateway</P>
<P><FONT =
color=3D#0000bf>=E5=85=B7=E4=BD=93=E5=9C=A8javascript=E4=B8=AD=E7=9A=84=E4=
=BD=BF=E7=94=A8=E6=96=B9=E6=B3=95=E5=A6=82=E4=B8=8B=EF=BC=9A</FONT></P>
<P> <WBR>function=20
testService(){<BR> <WBR><BR> <WBR> <WBR> <WBR> <=
WBR>=20
var callerResponder =3D new=20
CallerResponder(); <WBR>//=E8=BF=99=E6=98=AFajax=E7=9A=84=E5=93=8D=E5=
=BA=94=E5=9B=9E=E8=B0=83=E7=9A=84=E5=B0=81=E8=A3=85=E7=B1=BB<BR> <WB=
R> <WBR> <WBR> <WBR>=20
callerResponder.success =3D function(jsonObj) { <WBR>=20
//=E5=BD=93=E8=B0=83=E7=94=A8=E5=90=8E=E5=8F=B0=E6=96=B9=E6=B3=95=E6=88=90=
=E5=8A=9F=E6=97=B6=E7=9A=84=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0 <WBR=
><BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR> <WBR>=20
var customer =3D =
jsonObj; <WBR> <WBR> <WBR> <WBR> <WBR>=20
<WBR><BR> <WBR> <WBR> <WBR> <WBR> <WBR>&nb=
sp;<WBR> <WBR> <WBR>=20
alert(customer.brandName); <WBR><BR> <WBR> <WBR> <WBR=
> <WBR> <WBR> <WBR> <WBR> <WBR><BR> <WBR>&n=
bsp;<WBR> <WBR> <WBR> <WBR> <WBR> <WBR> <WB=
R>=20
alert(this.context.name); <WBR> <WBR> <WBR> <WBR> =
//this=20
will display=20
'wuyu' <WBR> <WBR>//=E6=88=91=E4=BB=AC=E5=9C=A8=E8=B0=83=E7=94=A8=
=E6=9F=90=E4=B8=AA=E5=90=8E=E5=8F=B0=E6=9C=8D=E5=8A=A1=E6=96=B9=E6=B3=95=E5=
=89=8D=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=88=B0CallerResponder=E4=B8=8A=E4=B8=8B=
=E6=96=87=E4=B8=AD=E7=9A=84=E4=B8=80=E4=BA=9B=E5=8F=98=E9=87=8F=EF=BC=8C=E5=
=9C=A8=E6=88=90=E5=8A=9F=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0=E4=B8=AD=EF=BC=
=8C=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87=E4=B8=8A=E4=B8=8B=E6=96=87=E5=8F=96=
=E5=87=BA=E6=9D=A5=E4=BD=BF=E7=94=A8<BR> <WBR> <WBR> <WBR>=
<WBR> <WBR> <WBR> <WBR> <WBR>=20
alert(this.context.age); <WBR> <WBR> <WBR> <WBR> =
//this will=20
display=20
'30'<BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR=
> <WBR> <WBR>=20
var str =3D=20
jQuery.toJSON(customer); <WBR> <WBR><BR> <WBR> <WBR>&=
nbsp;<WBR> <WBR> <WBR> <WBR> <WBR> <WBR>=20
var msg =3D "ajax success: " +=20
str; <WBR> <WBR> <WBR> <WBR> <WBR> <WBR><BR=
> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR> =
<WBR> <WBR>=20
alert(msg); <WBR> <WBR> <WBR> <WBR> <WBR> <=
WBR><BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR=
> <WBR>=20
}<BR> <WBR><BR> <WBR> <WBR> <WBR> <WBR>=20
callerResponder.error =3D function(request,settings,e) { <WBR> =
//failure=20
callback function, you may not implement it</P>
<P>//=E8=B0=83=E7=94=A8=E5=90=8E=E5=8F=B0=E6=96=B9=E6=B3=95=E5=87=BA=E9=94=
=99=E7=9A=84=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0<BR> <WBR> <WBR=
> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>=20
if (request.status =3D=3D 500)=20
{<BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR> <WBR> <WBR> <WBR> <WBR>=20
var jsonError =3D=20
jQuery.parseJSON(request.responseText);<BR> <WBR> <WBR> <W=
BR> <WBR> <WBR> <WBR> <WBR> <WBR>=20
}<BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR>=20
if (this.debug )=20
{<BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR> <WBR> <WBR> <WBR> <WBR>=20
alert("ajax error: " +=20
request.responseText);<BR> <WBR> <WBR> <WBR> <WBR>&nb=
sp;<WBR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR=
>=20
alert(e);<BR> <WBR> <WBR> <WBR> <WBR> <WBR> =
;<WBR> <WBR>=20
}<BR> <WBR> <WBR> <WBR> <WBR>=20
}<BR> <WBR><BR> <WBR> <WBR> <WBR>=20
callerResponder.context.name =3D "wuyu"; <WBR> //set the data which =
you will=20
access in callback=20
function <WBR>=E5=90=91callerResponder=E7=9A=84=E4=B8=8A=E4=B8=8B=E6=
=96=87=E4=B8=AD=E8=AE=BE=E7=BD=AE=E4=B8=80=E4=BA=9B=E9=9C=80=E8=A6=81=E5=9C=
=A8=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0=E4=B8=AD=E8=AE=BF=E9=97=AE=E7=9A=84=
=E6=95=B0=E6=8D=AE<BR> <WBR><BR> <WBR> <WBR> <WBR>=20
callerResponder.context.age =3D=20
"30"; <WBR><BR> <WBR><BR> <WBR><BR> <WBR> <WBR>&=
nbsp;<WBR> <WBR>=20
var customerMgrService =3D new=20
CustomerMgrService(); <WBR>//=E6=96=B0=E5=88=9B=E5=BB=BA=E4=B8=80=E4=
=B8=AA=E8=BF=9C=E7=A8=8B=E6=9C=8D=E5=8A=A1=E5=8C=85=E8=A3=85=E7=9A=84java=
script=E5=AF=B9=E8=B1=A1<BR> <WBR> <WBR> <WBR> <WBR> =
var tagMgrService =3D new=20
TagMgrService();<BR> <WBR> <WBR> <WBR> <WBR> var=20
strCustomerId =3D=20
jQuery("#customerId").val();<BR> <WBR> <WBR> <WBR> <W=
BR> <WBR>//=E8=B0=83=E7=94=A8=E5=90=8E=E5=8F=B0=E6=9C=8D=E5=8A=A1=E6=
=96=B9=E6=B3=95<BR> <WBR> <WBR> <WBR> <WBR>=20
customerMgrService.findByCustomerId(strCustomerId,callerResponder); =
<WBR>=20
// =
=E5=90=8E=E5=8F=B0=E7=9A=84spring=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=90=8D=E5=AD=
=97=E5=8F=AB =
CustomerMgrService=E7=9A=84Bean=E6=9C=89=E4=B8=80=E4=B8=AA<FONT =
color=3D#00007f>'public=20
Customer findByCustomerId(String=20
customerId)'=E6=96=B9=E6=B3=95=EF=BC=8C=E6=89=80=E4=BB=A5=E6=88=91=E4=BB=AC=
=E5=9C=A8=E8=BF=99=E9=87=8C=E5=8F=AF=E4=BB=A5=E8=B0=83=E7=94=A8=E5=AE=83=EF=
=BC=8C=E5=AE=83=E5=9C=A8=E5=90=8E=E5=8F=B0=E6=96=B9=E6=B3=95=E4=B8=AD=E5=8F=
=AA=E6=9C=89customerId=E8=BF=99=E4=B8=AA=E5=8F=82=E6=95=B0=EF=BC=8C=E5=9C=
=A8=E8=BF=99=E9=87=8C=EF=BC=8C=E6=88=91=E4=BB=AC=E5=9C=A8=E8=B0=83=E7=94=A8=
=E6=97=B6=E6=96=B9=E6=B3=95=E5=8F=82=E6=95=B0=E7=9A=84=E6=9C=80=E5=90=8E=E9=
=9D=A2=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80=E4=B8=AAcallerResponder=E7=9A=84=
=E5=8F=82=E6=95=B0=E3=80=82</FONT><BR> <WBR>}</P>
<P> <WBR></P>
<P>=E4=B8=8B=E8=BD=BD=E5=8C=85=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E7=BC=BA=
=E7=9C=81=E5=8F=AA=E8=83=BD=E6=98=AF=E8=B0=83=E7=94=A8Spring=E4=B8=AD=E9=85=
=8D=E7=BD=AE=E7=9A=84=E6=9C=8D=E5=8A=A1bean=E7=9A=84=E6=96=B9=E6=B3=95=EF=
=BC=8C=E4=BD=86=E6=98=AF=E5=9C=A8=E6=B2=A1=E6=9C=89=E4=BD=BF=E7=94=A8spri=
ng=E7=9A=84=E7=A8=8B=E5=BA=8F=E4=B8=AD=EF=BC=8C=E6=88=91=E4=BB=AC=E4=B9=9F=
=E8=A6=81=E4=BD=BF=E7=94=A8=E7=9A=84=E8=AF=9D=EF=BC=8C=E5=8F=AF=E4=BB=A5=E4=
=BF=AE=E6=94=B9JsonGatewayServlet.java</P>
<P><FONT face=3D=E5=AE=8B=E4=BD=93>public class JsonGatewayServlet =
extends HttpServlet=20
{</FONT></P>
<P><FONT face=3D=E5=AE=8B=E4=BD=93> <WBR> <WBR> <WBR> =
private static Properties=20
services;</FONT></P>
<P><FONT face=3D=E5=AE=8B=E4=BD=93> <WBR> <WBR> <WBR> =
public void init() throws=20
ServletException=20
{<BR> <WBR> <WBR> <WBR> <WBR> <WBR> <WBR>&n=
bsp;<WBR> <WBR>=20
super.init();<BR> <WBR> <WBR> <WBR> <WBR> <WBR>&=
nbsp;<WBR> <WBR> <WBR>=20
services =3D new=20
Properties();<BR> <WBR> <WBR> <WBR> <WBR> <WBR>&=
nbsp;<WBR> <WBR> <WBR>=20
InputStream in=20
=3DJsonGatewayServlet.class.getClassLoader().getResourceAsStream("service=
s.properties");<BR> <WBR> <WBR> <WBR> <WBR> <WBR=
> <WBR> <WBR> <WBR>=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -