1、申 请 I D : Java-小建
2、个人邮箱:lijiandt@foxmail.com
3、原创技术文章:
基于ssh框架的分页实现:[Java] 纯文本查看 复制代码 //我的项目名为sheep
//首先先建一个page实体类
package com.sheep.entity;
/**
* 分页实体类
* @author lijian
*
*/
public class Page {
private Integer pageNow;//当前页
private Integer totalSize;//总条数
private Integer totalPage;//总页数
private Integer pageSize=10;//每页条数
private Integer firstNum;//当前页第一条记录的数字
public Integer getFirstNum() {
return firstNum;
}
public void setFirstNum(Integer pageNow,Integer pageSize) {
this.firstNum = (pageNow-1)*pageSize+1;
}
//构造方法
public Page(Integer pageNow,Integer totalSize){
this.setPageNow(pageNow);
this.setTotalSize(totalSize);
}
//构造方法
public Page(Integer pageNow,Integer totalSize,Integer pageSize){
this.setPageNow(pageNow);
this.setTotalSize(totalSize);
this.setPageSize(pageSize);
}
public Integer getPageNow() {
return pageNow;
}
public void setPageNow(Integer pageNow) {
this.pageNow = pageNow;
}
public Integer getTotalSize() {
return totalSize;
}
public void setTotalSize(Integer totalSize) {
this.totalSize = totalSize;
}
public Integer getTotalPage() {
totalPage = this.getTotalSize()/this.getPageSize();
if(this.getTotalSize()%this.getPageSize()!=0){
totalPage++;
}
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
//dao接口定义两个方法
/**
* 分页查询所有对象
* @param pageNow
* @param pageSize
* @return List<Staff>
*/
List<Staff> getList(int pageNow,int pageSize);
/**
* 查询所有对象个数
* @return
*/
int totalSize();
//dao实现
public List<Staff> getList(int pageNow, int pageSize) {
Query query = sessionFactory.getCurrentSession().createQuery("from Staff");
query.setFirstResult((pageNow-1)*pageSize);
query.setMaxResults(pageSize);
List<Staff> list = query.list();
if(list.size()>0){
return list;
}else{
return null;
}
}
//service接口
/**
* 分页查询所有对象
* @param pageNow
* @param pageSize
* @return List<Staff>
*/
List<Staff> getList(int pageNow,int pageSize);
/**
* 查询所有对象个数
* @return
*/
int totalSize();
//service实现
public List<Staff> getList(int pageNow, int pageSize) {
return staffDao.getList(pageNow,pageSize);
}
//action中,注意页面是通过ajax获取到的数据
private Map<String,Object> map = new HashMap<String, Object>();//get set
public String pageList(){
HttpServletRequest request1 = ServletActionContext.getRequest();
Integer pageNow = Integer.valueOf(request1.getParameter("pageNow"));
Integer pageSize = Integer.valueOf(request1.getParameter("pageSize"));
Integer totalSize = staffService.totalSize();
Page page = new Page(pageNow, totalSize, pageSize);
page.setFirstNum(pageNow, pageSize);
//System.out.println(pageNow);
List<Staff> staffList = staffService.getList(pageNow,pageSize);
map.put("staffList", staffList);
map.put("page", page);
return "pageList";
}
//struts.xml配置,这里配置了一个继承json-default的包,和继承了struts-default的包并不冲突,可以同时存在
<package name="myjson" namespace="/myjson" extends="json-default">
<action name="staff_*" class="staffAction" method="{1}">
<result name="pageList" type="json">
<param name="root">map</param>
</result>
</action>
</package>
//页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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>员工列表展示</title>
<script type="text/javascript" src="<%=basePath %>js/jquery.js"></script>
<link href="<%=basePath %>css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
$(function(){
showPage(pageNow,pageSize);
});
var pageNow=1;
var pageSize=10;
function showPage(pageNow,pageSize){
$.ajax({
url:"${pageContext.request.contextPath}/myjson/staff_pageList",
type:"post",
data:{pageNow:pageNow, pageSize:pageSize},
success:function(data){
console.info(data);
$("#totalSize").html(data.page.totalSize);
$("#totalPage").html(data.page.totalPage);
$("#pageNow").html(data.page.pageNow);
$("#staTable").html("<tr><th>编号</th><th>员工姓名</th><th>登录名</th><th>密码</th><th>所属公司</th><th>所属部门</th><th>操作</th></tr>");
for(var i=0;i<data.staffList.length;i++){
$("#staTable").append("<tr><td width='9%'>"+(data.page.firstNum+i)+"</td><td width='15%'>"+data.staffList[i].staName+"</td><td width='15%'>"+data.staffList[i].loginName+"</td><td width='15%'>"+data.staffList[i].password+"</td><td width='15%'>"+data.staffList[i].corporation.corName+"</td><td width='15%'>"+data.staffList[i].corporation.dept.deptName+"</td><td width='16%'><a class='tablelink' href='${pageContext.request.contextPath}/staff/staff_viewUpdate?staId="+data.staffList[i].staId+"'>修改</a><a class='tablelink' href='javascript:void(0)' onclick='del("+data.staffList[i].staId+")'>删除</a></td></tr>");
}
},
error:function(){
alert("请稍后重试");
}
})
}
function del(staId){
var flag=confirm("确认删除员工,并删除员工对应的所有信息?");
if(flag){
$.ajax({
url:"${pageContext.request.contextPath}/myjson/staff_delete",
type:"post",
data:{staId:staId,pageNow:pageNow,pageSize:pageSize},
error:function(){
alert("请稍后重试");
},
success:function(data){
console.info(data);
showPage(data.page.pageNow,data.page.pageSize);
}
})
}
}
function prePage(){
if(pageNow>1){
pageNow=pageNow-1;
}
showPage(pageNow,pageSize);
}
function nextPage(){
var totalPage = $("#totalPage").text();
if(pageNow<totalPage){
pageNow=pageNow+1;
}
showPage(pageNow,pageSize);
}
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="${pageContext.request.contextPath}/jsp/index.jsp">首页</a></li>
<li><a href="<%=basePath%>staff/staff_list">员工列表</a></li>
</ul>
</div>
<div class="rightinfo">
<table id="staTable" class="tablelist">
</table>
</div>
<div class="pagin">
<div id="message" class="message" style="font-size:10pt;">
共<i class="blue" id="totalSize"></i>条记录,共<i id="totalPage" class="blue"></i>页
</div>
<ul class="paginList" id="paginList">
<li class="paginItem"><a href="javascript:void(0);"><span class="prePage"></span></a></li>
<li class="paginItem current"><a href="javascript:void(0);" id="pageNow"></a></li>
<li class="paginItem"><a href="javascript:void(0);"><span class="nextPage"></span></a></li>
</ul>
</div>
</body>
</html> |