dwr无刷新分页

来源:互联网 发布:python sin函数图像 编辑:程序博客网 时间:2021/12/08 07:41
<%@ page language="java" contentType="text/html; charset=GBK"%><html><head> <link rel="stylesheet" type="text/css" href="main.css"/> <script type='text/javascript' src='/pageDemo/dwr/interface/test.js'></script>   <script type='text/javascript' src='/pageDemo/dwr/engine.js'></script> <script type='text/javascript' src='/pageDemo/dwr/util.js'></script> <script type="text/javascript" src="main.js"></script> <script type="text/javascript">var nowPage;var mmmmmmm;function getInfo(){test.defaultAll(hallnpsot);}function goSelect(selectPageSize){var dxsize = mmmmmmm.infoCount;if(selectPageSize>dxsize){selectPageSize = mmmmmmm.infoCount;}test.caonimamPageSize(selectPageSize,hallnpsot);}function goupPage(){nowPage = mmm.nowPage;nowPage--;if(nowPage<=0){document.getElementById("message").innerHTML = "对不起,已经是第一页";}else{test.goup(nowPage,mmmmmmm.pageCount,mmmmmmm.pageSize,hallnpsot);document.getElementById("message").innerHTML = "";}}function nextPage(){nowPage = mmm.nowPage;nowPage++;if(nowPage>mmm.pageCount){document.getElementById("message").innerHTML = "对不起,已经是最后一页";}else{test.next(nowPage,mmmmmmm.pageCount,mmmmmmm.pageSize,hallnpsot);document.getElementById("message").innerHTML = "";}}function goPage(){var nowPage = document.getElementById("initValue").value;if(nowPage==""){document.getElementById("message").innerHTML = "对不起,页面编号必须填写";}else{test.gotoPage(nowPage,mmmmmmm.pageCount,mmmmmmm.pageSize,hallnpsot);document.getElementById("message").innerHTML = "";}}function hallnpsot(infoList){//alert(infoList);var deptInfo = infoList[0];mmmmmmm = infoList[1];mmm = infoList[1];$(selectElement).options[0].value =mmmmmmm.infoCount;document.getElementById("msg").innerHTML = "<font color='red'>共"+mmm.infoCount+"条/每页"+mmm.pageSize+"条 共"+mmm.pageCount+"页 当前第"+mmm.nowPage+"页</font>";//获取一个表格var table = document.getElementById("tableInfo");//清楚表中的数据while(table.rows.length>0)        {          table.deleteRow(0);        }        //给表格添加头部var newTh = table.insertRow();newTh.style.backgroundColor="#C8ECEC";newTh.align="center";//表头TDvar newTh1 = newTh.insertCell();var newTh2 = newTh.insertCell();var newTh3 = newTh.insertCell();var newTh4 = newTh.insertCell();var newTh5 = newTh.insertCell();var newTh6 = newTh.insertCell();var newTh7 = newTh.insertCell();newTh1.style.width="100";newTh2.style.width="130";newTh3.style.width="190";newTh4.style.width="190";newTh5.style.width="190";newTh6.style.width="60";newTh7.style.width="60";newTh.className = "style2";//添加表头内容newTh1.innerHTML = "部门编号";newTh2.innerHTML = "部门名称";newTh3.innerHTML = "部门邮箱";newTh4.innerHTML = "部门电话";newTh5.innerHTML = "部门地址";newTh6.innerHTML = "编辑";newTh7.innerHTML = "删除";for(var i=0;i<deptInfo.length;i++){//获取传递过来的数据var id = deptInfo[i].id;var name = deptInfo[i].name;var email = deptInfo[i].email;var tel = deptInfo[i].tel;var address = deptInfo[i].address;//添加一行var newTr = table.insertRow();newTr.style.backgroundColor="#E6E6E6";newTr.align="center";newTr.className = "style3";//添加5列var newTd1 = newTr.insertCell();var newTd2 = newTr.insertCell();var newTd3 = newTr.insertCell();var newTd4 = newTr.insertCell();var newTd5 = newTr.insertCell();var newTd6 = newTr.insertCell();var newTd7 = newTr.insertCell();//给单元格添加数据newTd1.innerHTML = id;newTd2.innerHTML = name;newTd3.innerHTML = email;newTd4.innerHTML = tel;newTd5.innerHTML = address;newTd6.innerHTML = "<a href='javascript:void(0)' onclick=''>编辑</a>";newTd7.innerHTML = "<a href='javascript:void(0)' onclick=''>删除</a>";}}</script></head>  <body onload="getInfo()">  <center>  <div class="style1" id="message"></div>  <table id="tableInfo" width="800" border="0" cellpadding="0" cellspacing="1" bordercolor="#CCCCCC"></table>  <table border="0" width="800">  <tr align="left">  <td align="left" id="msg" width="250"><br>    <br></td>  <td width="150">  <a href="javascript:void(0)" onclick="goupPage()">上一页</a>      <a href="javascript:void(0)" onclick="nextPage()">下一页</a>  </td>  <td align="left" width="80">  <input type="text" id="initValue" size="5"/>  <input type="button" id="gonum" value="GO" onclick="goPage()"/>  </td>  <td>  显示行数:  <select id="selectElement" onchange="goSelect(this.value)">  <option value="">全部</option>  <option value="5">5</option>  <option value="10">10</option>  <option value="15">15</option>  <option value="20">20</option>  <option value="50">50</option>  <option value="100">100</option>  </select>  </td>  </tr>  </table>  </center>  </body></html>

<?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">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <!-- 配置DWR的Servlet -->  <servlet><servlet-name>dwr-invoker</servlet-name><servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class><init-param><param-name>debug</param-name><param-value>true</param-value></init-param>  </servlet>  <servlet-mapping><servlet-name>dwr-invoker</servlet-name><url-pattern>/dwr/*</url-pattern>  </servlet-mapping></web-app>

dwr.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd" ><!-- <!DOCTYPE dwr SYSTEM "dwr20.dtd" > --><dwr>   <allow>   <create creator="new" javascript="test">   <param name="class" value="taotao.montao.service.ServiceMethod"/>   <include method="defaultAll"/>   <include method="next"/>   <include method="goup"/>   <include method="gotoPage"/>   <include method="caonimamPageSize"/>   </create>   <convert converter="bean" match="taotao.montao.vo.DeptVO">   <param name="include" value="id,name,email,tel,address"/>   </convert>      <convert converter="bean" match="taotao.montao.vo.PageVO">   <param name="include" value="pageCount,nowPage,pageSize,infoCount"/>   </convert>   </allow></dwr>

package taotao.montao.service;import java.util.List;import taotoa.montao.dao.TestDAO;public class ServiceMethod {TestDAO dao = new TestDAO();//GO页面@SuppressWarnings("unchecked")public List gotoPage(int nowpager,int pageContent,int pageSizeSize){return dao.gotoPage(nowpager,pageContent,pageSizeSize);}//设置页面显示大小@SuppressWarnings("unchecked")public List caonimamPageSize(int pagePageSize){return dao.setSize(pagePageSize);}//默认的页面显示数据@SuppressWarnings("unchecked")public List defaultAll(){return dao.defaultInfo();}//下一页的数据@SuppressWarnings("unchecked")public List next(int nowpager,int pageContent,int pageSizeSize){return dao.nextInfo(nowpager,pageContent,pageSizeSize);}//上一页的数据@SuppressWarnings("unchecked")public List goup(int nowpager,int pageContent,int pageSizeSize){return dao.goupInfo(nowpager,pageContent,pageSizeSize);}}

package taotoa.montao.dao;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import taotao.montao.dbcon.DBCon;import taotao.montao.vo.DeptVO;import taotao.montao.vo.PageVO;public class TestDAO {private DBCon db = null;private PageVO page = new PageVO();//获取总的信息数public int getInfoCount(){int infoCount = 0;//获取Connection连接db = new DBCon();Connection conn = db.getConnection();//创建SQL语句String strSql = "select * from dept";//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){infoCount++;}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}return infoCount;}@SuppressWarnings("unchecked")public List goupInfo(int nowpager,int pageContent,int pageSizeSize){this.defaultInfo();page.setNowPage(nowpager);page.setPageSize(pageSizeSize);page.setPageCount(pageContent);List mmm = new ArrayList();if(nowpager<=0){page.setNowPage(1);}int startLine = (page.getNowPage()*page.getPageSize())-page.getPageSize(); //设置起始行System.out.println("开始的数据: "+startLine);//返回的结果集对象List<DeptVO> list = new ArrayList<DeptVO>();//获取Connection连接db = new DBCon();Connection conn = db.getConnection();//创建SQL语句String strSql = "select * from dept limit "+startLine+","+page.getPageSize();System.out.println(strSql);//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){DeptVO dept = new DeptVO();dept.setId(rs.getInt("id"));dept.setName(rs.getString("name"));dept.setEmail(rs.getString("email"));dept.setTel(rs.getString("tel"));dept.setAddress(rs.getString("address"));list.add(dept);}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}mmm.add(0,list);mmm.add(1,page);return mmm;}@SuppressWarnings("unchecked")public List setSize(int pagePageSize){this.defaultInfo();page.setPageSize(pagePageSize);if(pagePageSize>=page.getInfoCount()){page.setPageCount(page.getInfoCount()/page.getPageSize());}else{page.setPageCount(page.getInfoCount()/page.getPageSize()+1);}System.out.println("总的页数: "+page.getPageCount());List mmm = new ArrayList();//返回的结果集对象List<DeptVO> list = new ArrayList<DeptVO>();//获取Connection连接db = new DBCon();Connection conn = db.getConnection();System.out.println(page.getPageSize());//创建SQL语句String strSql = "select * from dept limit 0,"+(page.getPageSize());System.out.println(strSql);//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){DeptVO dept = new DeptVO();dept.setId(rs.getInt("id"));dept.setName(rs.getString("name"));dept.setEmail(rs.getString("email"));dept.setTel(rs.getString("tel"));dept.setAddress(rs.getString("address"));list.add(dept);}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}mmm.add(0,list);mmm.add(1,page);return mmm;}@SuppressWarnings("unchecked")public List gotoPage(int nowpager,int pageContent,int pageSizeSize){this.defaultInfo();page.setNowPage(nowpager);page.setPageSize(pageSizeSize);page.setPageCount(pageContent);List mmm = new ArrayList();if(nowpager>page.getPageCount()){page.setNowPage(page.getPageCount());  //如果请求的页面大于总页数}int startLine = page.getNowPage()*page.getPageSize()-page.getPageSize(); //设置起始行//返回的结果集对象List<DeptVO> list = new ArrayList<DeptVO>();//获取Connection连接db = new DBCon();Connection conn = db.getConnection();//创建SQL语句String strSql = "select * from dept limit "+startLine+","+page.getPageSize();System.out.println(strSql);//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){DeptVO dept = new DeptVO();dept.setId(rs.getInt("id"));dept.setName(rs.getString("name"));dept.setEmail(rs.getString("email"));dept.setTel(rs.getString("tel"));dept.setAddress(rs.getString("address"));list.add(dept);}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}mmm.add(0,list);mmm.add(1,page);return mmm;}@SuppressWarnings("unchecked")public List nextInfo(int nowpager,int pageContent,int pageSizeSize){this.defaultInfo();page.setNowPage(nowpager);page.setPageSize(pageSizeSize);page.setPageCount(pageContent);List mmm = new ArrayList();if(nowpager>page.getPageCount()){page.setNowPage(page.getPageCount());  //如果请求的页面大于总页数}int startLine = (page.getNowPage()-1)*page.getPageSize(); //设置起始行//返回的结果集对象List<DeptVO> list = new ArrayList<DeptVO>();//获取Connection连接db = new DBCon();Connection conn = db.getConnection();//创建SQL语句String strSql = "select * from dept limit "+startLine+","+page.getPageSize();System.out.println(strSql);//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){DeptVO dept = new DeptVO();dept.setId(rs.getInt("id"));dept.setName(rs.getString("name"));dept.setEmail(rs.getString("email"));dept.setTel(rs.getString("tel"));dept.setAddress(rs.getString("address"));list.add(dept);}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}mmm.add(0,list);mmm.add(1,page);return mmm;}//默认页面显示的数据@SuppressWarnings("unchecked")public List defaultInfo(){int nowPage = 1;if (nowPage < 1){nowPage =1; }//返回的结果集对象List<DeptVO> list = new ArrayList<DeptVO>();//设置页对象page.setInfoCount(this.getInfoCount());      //设置记录总数page.setNowPage(nowPage);                    //设置当前页数page.setPageSize(5);                         //设置每页显示数page.setPageCount(page.getInfoCount()/page.getPageSize()+1);  //设置总页数//获取Connection连接db = new DBCon();Connection conn = db.getConnection();//创建SQL语句String strSql = "select * from dept limit 0,"+(page.getPageSize());//执行方法Statement stmt = null;ResultSet rs = null;try{stmt = conn.createStatement();rs = stmt.executeQuery(strSql);while(rs.next()){DeptVO dept = new DeptVO();dept.setId(rs.getInt("id"));dept.setName(rs.getString("name"));dept.setEmail(rs.getString("email"));dept.setTel(rs.getString("tel"));dept.setAddress(rs.getString("address"));list.add(dept);}}catch(Exception ex){ex.printStackTrace();}finally{try {rs.close();stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}List mm = new ArrayList();mm.add(0,list);mm.add(1,page);return mm;}}