Struts2+jquery pagination插件无刷新分页

时间:2022-12-09 15:14:53

在IE8和firefox下没有问题(其他的浏览器还没来得及测试),如果其他浏览器有问题只需要重写pagination.css就可以了

Action类

package com.data3;

import com.data2.Databean;
import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport{
	private int rowCount=0;
public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
private int pageNo=1;
private int pageSize=10;
public int getPageNo() {
	return pageNo;
}
public void setPageNo(int pageNo) {
	this.pageNo = pageNo;
}
public int getPageSize() {
	return pageSize;
}
public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
}
public int getPageCount() {
	return pageCount;
}
public void setPageCount(int pageCount) {
	this.pageCount = pageCount;
}
public String getResult() {
	return result;
}
public void setResult(String result) {
	this.result = result;
}
private int pageCount=0;
private String result;
	@Override
	public String execute() throws Exception {
		Databean a=new Databean();
		
		rowCount=a.rowCount();
	return SUCCESS;
	}
	public String login(){
		Databean d=new Databean();
		//rowCount=d.rowCount();
	
		String s=d.allperson(pageNo, pageSize);
		this.setResult(s);
		
		return SUCCESS;
	}

}

业务逻辑类

package com.data2;
import com.data.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;

import net.sf.json.JSONArray;

import com.data.DBclose;

public class Databean {
	int rowCount=0;
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
public List all(int pageNo,int pageSize){
	List list=new ArrayList();
	try{
	ct=Conn.getinstance();
	ps=ct.prepareStatement("select top "+pageSize+"* from Person  where pid not in (select top "+pageSize*(pageNo-1)+" pid from Person ) ");
	rs=ps.executeQuery();
Person p=null;
	while(rs.next()){
		p=new Person();
		p.setPid(rs.getInt(1));
		p.setMname(rs.getString(2));
		list.add(p);
	}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		DBclose.close(ct, ps, rs);
	}
	return list;
}

public String allperson(int pageNo,int pageSize){
	System.out.println(pageNo);
	List list=this.all(pageNo, pageSize);
	JSONArray json=JSONArray.fromObject(list);
	
	return json.toString();
}
public int rowCount(){
	
	try{
		ct=Conn.getinstance();
		ps=ct.prepareStatement("select count(*) from Person");
		rs=ps.executeQuery();
		if(rs.next()){
			rowCount=rs.getInt(1);
		}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		DBclose.close(ct, ps, rs);
	}return rowCount;
}
}

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<package name="struts2" extends="struts-default">

<action name="all" class="com.data3.PersonAction">
<result name="success">/MyJsp.jsp</result>
</action>
</package>
<package name="default" extends="json-default">
<action name="go" class="com.data3.PersonAction" method="login">
<result type="json">
<param name="root">result</param>

</result>
</action>
</package>






</struts>

    

前台页面

Login.jsp

<%@ 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 'Login.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">
	-->

  </head>
  
  <body>
 <a href="all.action">查询</a>
  </body>
</html>

数据页面

MyJsp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>
  <link rel="stylesheet" type="text/css" href="css/pagination.css">
  <style type="text/css">
 
  .pagination .current.prev{
  font-size:12px;
  width:50px;
  color:#1155BB;
  }
 .pagination .current.next{
  font-size:12px;
  width:50px;
   color:#1155BB;
 }
  .pagination{
  width:600px;
  }
  
  body{font-size:84%; color:#333333; line-height:1.4;}
a{color:#34538b;}
  </style>
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
 <script type="text/javascript" src="js/jquery.pagination.js"></script>
 <script type="text/javascript">
 var pageNo=0;
 $(function(){
 //initData(0);
 $("#Pagination").pagination(<s:property value="rowCount"/>, { 
  
                   callback: pageselectCallback,  
                   prev_text:"上一页",  
                   next_text:"下一页",  
                   items_per_page:10,  
                    num_display_entries:6,  
                    current_page: pageNo, 
                    num_edge_entries:2  
               });  
  function pageselectCallback(pageindex,jq) {  

           initData(pageindex);
           return false;  
       }  
       function initData(pageNo){
var i=<s:property value='rowCount'/>;
   if(pageNo>1||pageNo<i){
   //pageNo<rowCount尾页,我这里是重新覆盖了pagination.css的属性.
   $(".pagination .current.next").css("color","#999999");
   $(" .pagination .current.prev").css("color","#999999");
   }
       $.ajax({
       type:"post",
      url:"go.action",
        async:false,
        data:"pageNo="+(pageNo+1)+"",
        success:function(data){
        if(data){
       $("#body").empty();
        var c=eval("("+data+")");
     
        for(var k=0;k<c.length;k++){
   
        $("#body").append("<tr><td>"+c[k].pid+"</td><td>"+c[k].mname+"</td></tr>");
        }
        }
        }
       });
       }
   
        
 });
   
 </script>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.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>
   <table>
   <thead><tr><th>编号</th><th>姓名</th></tr></thead>
   <tbody id="body"></tbody>
   </table>
   <div id="Pagination" class="pagination"></div>
  </body>
</html>