首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

strust2中jquery兑现ajax

2012-09-23 
strust2中jquery实现ajax最近在项目中需要使用ajax,根据网上的资料自己写了一个测试小程序,现将代码奉上。

strust2中jquery实现ajax

最近在项目中需要使用ajax,根据网上的资料自己写了一个测试小程序,现将代码奉上。一些需要注意的地方我都加在注释里了。

配置struts的东西我就不说了,网上有很多,自己去看吧。这个是在已经会配置struts的基础上才写的。

Action类

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</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"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript" src="js/jquery-1.8.1.min.js"></script><script type="text/javascript">function loadInfo(){//远程调用check方法,不传参数null决定,定义了回调函数参数data是返回的数据//最后定义了返回的是json数据,jquery会自动将返回的数据进行json解析,此参数还可以是text、xml等具体参见文档$.post("check",null,function(data){//直接使用数据,这里不能取得data.name的值,因为在Action中使用注解使其不被返回alert(data.name+","+data.user.name+","+data.user.sex);//对于List的循环$.each(data.list,function(i,item){alert(item.name+","+item.sex);});},"json");}</script>  </head>    <body>    This is my JSP page. <br>   <input type="button" value="获取" id="btnGet" onclick="loadInfo()" />        <div id="info"></div  </body></html>

读者看的时候如果准备自己运行,必须将对应的jquery文件修改。想要源码的可以直接给我发邮件598660766@qq.com


热点排行