AjaxAnyWhere 实现页面局部刷新,局部分页

这个比较jquery、单纯ajax异步简单多了,不多说了直接上代码。


需要引入:ajaxanywhere-1.2.1.jar (最新)和 /ajaxAnyWhereDemo/WebRoot/js/aa.js  ,

免费下载地址http://download.csdn.net/detail/xuke6677/8064977

目录结构如下:




1、ListBean.java 实体类   

package org.ydd.test;

public class ListBean {
	private String id;
	private String name;
	private String sex;
	private String work;
	private String address;
        //get(),set()…… 省略
}


2、   AjaxAnyWhereTest.java  后台servlet


package org.ydd.test;

import java.io.IOException;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AjaxAnyWhereTest extends HttpServlet {
	private static final long serialVersionUID = -5750167075517567170L;
	List<ListBean> rs = new ArrayList<ListBean>();
	
	@Override
	public void init() throws ServletException {
		ListBean userBean = new ListBean();
		userBean.setId("1");
		userBean.setName("张三");
		userBean.setSex("123");
		userBean.setWork("prom");
		userBean.setAddress("fdsfsd");
		rs.add(userBean);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ListBean userBean = new ListBean();
		String id= request.getParameter("id");
		if(!"".equals(id) && id!=null){
			userBean.setId(id);
			userBean.setName(request.getParameter("name"));
			userBean.setAddress(request.getParameter("address"));
			userBean.setSex(request.getParameter("sex"));
			userBean.setWork(request.getParameter("work"));
			rs.add(userBean);
		}
		
		if(rs.size()>0){
			request.setAttribute("queryList", rs);
		}
		
		request.getRequestDispatcher("list.jsp").forward(request, response);
	}

}

3、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!-- Ajax配置开始,带编码转换(包括ajax提交的编码) --> 
    <filter>
        <filter-name>AjaxAnywhere</filter-name>
        <filter-class>org.ajaxanywhere.AAFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name><!-- 普通提交方式编码 -->
            <param-value>GB2312</param-value>
        </init-param>
        <init-param>
            <param-name>ajaxencoding</param-name><!-- AJAX提交方式编码 -->
            <param-value>GB2312</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>AjaxAnywhere</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
    
    
  <servlet>
    <servlet-name>AjaxAnyWhereTest</servlet-name>
    <servlet-class>org.ydd.test.AjaxAnyWhereTest</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AjaxAnyWhereTest</servlet-name>
    <url-pattern>/AjaxAnyWhereTest</url-pattern>
  </servlet-mapping>

    <!-- Ajax配置结束 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


4、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <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">
  </head>
  <body>
  	 <jsp:forward page="AjaxAnyWhereTest"/>
  </body>
</html>



5、list.jsp


注:需要引入<script language="javascript" src="js/aa.js"></script>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://ajaxanywhere.sourceforge.net/" prefix="aa" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>ajaxAnywhere局部刷新</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 language="javascript" src="js/aa.js"></script>	
	<script type="text/javascript">
	 <!-- 所要提交的表单 -->
		ajaxAnywhere.formName="listForm";
	 <!-- 所要刷新的区域 -->
		ajaxAnywhere.getZonesToReload = function(){
			return "formlist";
		} 
		//提交
		function doCheck(){
			var id =  document.getElementById("id").value;
			if(id==""){
				alert("请填写ID编号!");
				return false;
			}
			var f=document.forms[0];
			f.action="AjaxAnyWhereTest";
			ajaxAnywhere.submitAJAX();
		}
	</script>
	
  </head>
  
  <body>
  
<center>
  <div>列表</div>
  <aa:zone name="formlist">
	  <table border="1">
	   		<tr>
	   			<td>ID编号   </td>
	   			<td>姓名</td>
	   			<td>性别</td>
	   			<td>工作行业</td>
	   			<td>地址</td>
	   		</tr>
	  			<c:forEach var="listbean" items="${queryList}">
			  <tr>
			  	<td>${listbean.id}</td>
			  	<td>${listbean.name}</td>
			  	<td>${listbean.sex}</td>
			  	<td>${listbean.work}</td>
			  	<td>${listbean.address}</td>
			  </tr>
	  		    </c:forEach>
	   </table>
  </aa:zone>
</center> 
<br>
<br>
<hr>
 <center>
    		<div> <h3>录入</h3></div>
    		<form name="listForm" action="AjaxAnyWhereTest"  method="post">
    		<table>
    			 <tr>
    			 	<td>ID编号 :</td><td><input type="text" name="id" id="id"/></td>
    			 </tr>
    			 <tr>
    			 	<td>姓名 :</td><td><input type="text" name="name" /></td>
    			 </tr>
    			 <tr>
    			 	<td>性别 :</td><td><input type="text" name="sex" /></td>
    			 </tr>
    			 <tr>
    			 	<td>工作行业:</td><td><input type="text" name="work" /></td>
    			 </tr>
    			 <tr>
    			 	<td>地址:</td><td><input type="text" name="address" /></td>
    			</tr>
    			<tr>
    				<td><input type="button" value="提交" οnclick="return doCheck()" /></td ><td><input type="reset" value="取消" /></td >
    			</tr> 
    			
    		</table>
    		</form>
    </center>  
  </body>
</html>


可以封装一下

定义空的form表单只做跳转用<form name="listForm" action="/newActivity/hotSummerDays.html"  method="post"></form> 

var url = "/newActivity/hotSummerDays.html?tradeId=lightenCountAndUser";
 publicSch("formlist","listForm",url);<span style="white-space:pre">																			
 function publicSch(listName,formName,url){
		ajaxAnywhere.formName=formName;
		ajaxAnywhere.getZonesToReload = function(){
			return listName;
		} 
		var f=document.listForm;
		f.action=url;
		ajaxAnywhere.submitAJAX();
	}




6、注意问题:



点击确定会显示具体的错误信息,如下

<body>
		<h1>
			HTTP Status 404 - /newActivity/undefined
		</h1>
		<HR size="1" noshade="noshade">
		<p>
			<b>type</b> Status report
		</p>
		<p>
			<b>message</b>
			<u>/newActivity/undefined</u>
		</p>
		<p>
			<b>description</b>
			<u>The requested resource is not available.</u>
		</p>
		<HR size="1" noshade="noshade">
		<h3>
			Apache Tomcat/6.0.41
		</h3>
	</body>



1、如果有使用jquery,一定要注意js的加载顺序。


<title></title>
<script type="text/javascript" charset="utf-8" src="/js/aa.js"/>
<script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>

</head>


2、


 

7、可以用在局部分页,只刷新分页的地方


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>动漫搜索</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"> var xmlhttp; function loadXMLDoc(url,cfun) { // alert("hadhfaf"); xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=cfun; xmlhttp.open("GET",url,true); xmlhttp.send(); } function myfunction(id,times) { //alert("123"); loadXMLDoc("open.jsp?id="+id+"&times="+times+"&t="+Math.random(),function() { //alert(xmlhttp.status); if(xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById(id).innerHTML=xmlhttp.responseText; } }); } </script> <% //currentPage当前显示页数 if (request.getParameter("currentPage") == null||request.getParameter("currentPage").equals("")) { pageContext.setAttribute("currentPage", new Integer(0)); } else { pageContext.setAttribute("currentPage", request .getParameter("currentPage")); } %> <sql:query var="rs" dataSource="jdbc/mysql"> select* from anime </sql:query> <!-- 得到所有记录数 --> <c:set var="resultNum"> <jsp:getProperty name="rs" property="rowCount" /> </c:set> <!-- 每页显示记录数 --> <c:set var="PerPageNum" value="3" /> </head> <style type="text/css"> body{background-color:} </style> <body> <% int perPageNum = Integer.parseInt((String) pageContext .getAttribute("PerPageNum")); int resultNum = Integer.parseInt((String) pageContext .getAttribute("resultNum")); int pageNum; //如果所有记录数除每页显示记录数没有余数-1 if((int)resultNum%perPageNum==0) pageNum = (int)(resultNum / perPageNum-1); else pageNum = (int)(resultNum / perPageNum); pageContext.setAttribute("pageNum", pageNum); %> <div style="border:1px solid #cccccc; width:1100px;margin:100px 0 0 90px"> <table style=" width:1100px;"> <tr> <td>序号</td> <td>热搜动漫</td> <td>动漫简介</td> <td>更新集数</td> <td>搜索指数</td> <td>动漫详情</td> </tr> <c:if test="${currentPage lt 0 }"><!-- 当前显示页小于0 --> <c:set var="currentPage" value="0"></c:set> </c:if> <c:if test="${currentPage gt pageNum}"><!-- 当前显示页大于总页数 --> <c:set var="currentPage" value="${pageNum }"></c:set> </c:if> <c:set var="currentPage" value="${currentPage}" scope="session" /> <!-- 循环查找记录 --> <c:forEach var="row" items="${rs.rows}" begin="${currentPage*PerPageNum}" end="${PerPageNum+currentPage*PerPageNum-1}" > <tr> <td> ${row.id }</td> <td> <img src="image/${row.image}" width="50" height="50"/></td> <td style="width:600"> ${row.introduce}</td> <td> ${row.blues} </td> <td> ${row.times }</td> <c:set var="times" value="${row.times }" scope="session" /> <td> <input type="button" value="展开" onclick="myfunction(${row.id },${row.times })"/> </td> </tr> <tr> <td colspan=6 id="${row.id}"></td> </tr> </c:forEach> </table> <div style="margin:0px 0px 0px 800px"> 总共有${pageNum+1}页-第${currentPage+1}页-<a href="index.jsp?currentPage=${currentPage-1}">上一页</a> - <a href="index.jsp?currentPage=${currentPage+1}">下一页</a> </div> </div> </body> </html>
AjaxAnywhere的类库及其用法 AjaxAnywhere使用一个名为aa.js的Javascript文件来处理客户端的全部Ajax操作,包括初始化XMLHttpRequest、获取表单内容、发送Ajax请求、执行回调函数等。aa.js也是使用AjaxAnywhere之前必须了解的,至少应该知道其经常用到的API。Ajax Anywhere的官方网站提供了相应的Javascript Document,方便快速查找和了解这些API。 1.AjaxAnywhere的初始化 aa.js中定义了一个AjaxAnywhere对象,针对Ajax的各种操作被抽象成AjaxAnywhere对象的方法,通过这些对象方法完成所需的操作。必要的时候,可以重载这些方法,以便满足个性化的需求。在aa.js文件的末端,AjaxAnywhere对象使用默认的构造方法完成对象实例化。 ajaxAnywhere = new AjaxAnywhere(); ajaxAnywhere.bindById(); 所以,所有引用aa.js的页面都可以在Javascript代码段中使用AjaxAnywhere对象的实例ajaxAnywhere。 当AjaxAnywhere初始化的时候,它在默认的构造函数中完成XMLHttpRequest对象的创建,并保存在AjaxAnywhere对象属性req中。AjaxAnywhere对象默认的构造方法如例程11-23所示。 例程11-23 AjaxAnywhere对象的默认构造方法 function AjaxAnywhere() { this.id = AjaxAnywhere.defaultInstanceName;//id,用于生成更新区域的编号等用途 this.formName = null;//页面表单名称 this.notSupported = false;//是否支持Ajax this.delayBeforeContentUpdate = true;//在更新页面内容之前是否延迟 this.delayInMillis = 100;//延迟时间 //初始化XMLHttpRequest对象--req if (window.XMLHttpRequest) { this.req = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { this.req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e1) { this.notSupported = true; /* XMLHTTPRequest not supported */ } } } //确定浏览器是否支持Ajax if (this.req == null || typeof this.req == "undefined") this.notSupported = true; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值