Java实现分页的前台页面和后台代码
作者:dKnightL 发布时间:2021-07-22 17:10:04
标签:Java,分页
本文实例为大家分享了Java分页展示的具体代码,供大家参考,具体内容如下
先上图吧,大致如图,也就提供个思路(ps:使用了SSH框架)
前台JSP页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>数据交易猫</title>
<script type="text/javascript">
//1分页下,动态添加disable给分页按钮
/*
$(function(){
var myPageId="#"+$("#hidCurrentPage").val();
var myPageAId="#"+$("#hidCurrentPage").val()+" a";
$(myPageAId).addClass('main-bgcolor');
$(myPageAId).attr('href','javascript:void(0)')
$(myPageId).addClass('disabled');
$(myPageId).addClass('disabledControl');
})
*/
//
$(function(){
})
//根据页数查询数据列表
function queryRequirListByPage(i) {
var pageNo=i;
var sortValue=$('#hidSortValue').val();
$.ajax({
url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
type:'POST',
data:{
sortValue:sortValue,
pageNo:pageNo
},
success:function(datas){
$('#requireContentDiv').html(datas);
},
error:function(){
alert("失败");
},
});
}
//根据下拉查询数据列表
function selectPage(obj){
var pageNo=obj.options[obj.selectedIndex].value;
var sortValue=$('#hidSortValue').val();
$.ajax({
url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
type:'POST',
data:{
sortValue:sortValue,
pageNo:pageNo
},
success:function(datas){
$('#requireContentDiv').html(datas);
},
error:function(){
alert("失败");
},
});
}
//根据下拉选择排序方式
function selectSort(obj){
var sortValue = obj.options[obj.selectedIndex].value;
var pageNo =1;
$.ajax({
url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
type:'POST',
data:{sortValue:sortValue,
pageNo:pageNo
},
success:function(datas){
$('#requireContentDiv').html(datas);
},
error:function(){
alert("失败");
},
});
}
$(document).ready(function(){
var backSortValue=$('#backSortValue').val();
console.log("backSortValue"+backSortValue)
$("#category option").each(function(){
var thisId='#'+this.id;
var thisValue=this.value;
if(backSortValue==thisValue){
$(thisId).attr('selected','selected');
}
});
})
</script>
</head>
<body>
<!-- 内容-->
<div class="well">
<!-- 标题-->
<div class="box"><h3><span class="glyphicon glyphicon-list" ></span>需求列表</h3></div>
<!-- 筛选条件-->
<div class="box">
<div class="row">
<div class="col-xs-12">
<span>筛选:按</span>
<select id="category" name="category" onchange="selectSort(this)">
<option id="categoryTime" value="publishDatetime">最新</option>
<option id="categoryPrice" value="price">价格降序</option>
<input id="backSortValue" type="hidden" value="${sortValue}">
</select>
<hr class="mrgZero mrgTopSma"/>
</div>
</div>
</div>
<!-- 内容-->
<input type="hidden" name="hidCurrentPage2" id="hidCurrentPage" value="${currentPage}">
<input type="hidden" id="hidAllPage" value="${allPage}">
<input type="hidden" id="hidSortValue" value="${sortValue}">
<s:iterator value="#requiList">
<div class="data-down-box">
<div class="row">
<div class="col-xs-12">
<h4 class="ellipsis"><a href="${pageContext.request.contextPath}/bid/bidAction_queryById?id=${id}" rel="external nofollow" onclick="reward()">${title}</a></h4>
</div>
</div>
<div class="row mrgTopSma">
<div class="col-xs-12 ">
<p class="data-provider padLeftBig sec-color ellipsis">悬赏积分:<span>${price}</span></p>
<p class="data-intro padLeftBig ellipsis sec-color">需求描述:<span>${requirementDescription}</span></p>
</div>
</div>
<hr/>
</div>
</s:iterator>
<!-- 分页 -->
<div id="rePagerDiv" class="rePagerDiv box">
<nav>
<ul class="pager">
<!-- 判断当前页是否位1,如果不为1则显示上一页, -->
<s:if test="1 == #currentPage">
</s:if>
<s:else>
<li>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous" onclick="queryRequirListByPage(${currentPage-1})">
<span aria-hidden="true">«</span>
</a>
</li>
</s:else>
<!-- 首页 -->
<li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="queryRequirListByPage(1)">首页</a></li>
<li>
<span><span class="main-color">${currentPage}</span>/ ${allPage}页</span>
</li>
<!-- 尾页 -->
<li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="queryRequirListByPage(${allPage})">尾页</a></li>
<!-- 判断当前页和总页数,小于则显示下一页, -->
<s:if test="#currentPage < #allPage">
<li>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next" onclick="queryRequirListByPage(${currentPage+1})">
<span aria-hidden="true">»</span>
</a>
</li>
</s:if>
<li>
<span class="skipPageSpan">跳转到第
<select onchange="selectPage(this)">
<s:iterator var="lst" begin="1" end="#allPage" step="1">
<s:if test="%{#lst == #currentPage}">
<option selected="selected" value="<s:property/>" ><s:property/></option>
</s:if>
<s:else>
<option value="<s:property/>" ><s:property/></option>
</s:else>
</s:iterator>
</select>
页
</span>
</li>
</ul>
</nav>
</div>
</div>
<hr/>
</body>
</html>
action
//查询需求列表
public String queryRequirListByPage(){
int pageSize=5;//每页记录
String hql="select r from Requirement r where r.reStatus !=2 ";
if(sortValue == null || sortValue.length() <= 0){
hql=hql+"order by r.publishDatetime desc";
ActionContext.getContext().put("sortValue", "publishDatetime"); //当前页码条件
session.put("sessionReqSortValue","publishDatetime");
}else{
hql=hql+"order by r."+sortValue+" desc";
ActionContext.getContext().put("sortValue", sortValue); //当前页码条件
session.put("sessionReqSortValue",sortValue);
}
long icount=requirementService.countAllRe();//总记录数
long allPage;//总页数
//判断是否能整除,能则直接,不能则+1;
if((icount%pageSize)==0){
allPage=icount/pageSize;
}
else{
allPage=(icount/pageSize)+1;
}
System.out.println("总记录:"+icount+";总页数:"+allPage+";当前页码:"+pageNo);
List<Requirement> requiList=requirementService.queryByPage(hql, pageNo, pageSize);
ActionContext.getContext().put("requiList", requiList);//需求列表
ActionContext.getContext().put("icount", icount);//总记录数
ActionContext.getContext().put("allPage", allPage);//总页数
ActionContext.getContext().put("currentPage", pageNo); //当前页码
session.put("sessionCurrentPage", pageNo);
return "requireContent";
}
service
public long countAllRe() {
return requirementDao.countAllRe();
}
public List<T> queryByPage(String hql, int pageNo, int pageSize) {
return requirementDao.queryByPage(hql, pageNo, pageSize);
}
dao
//这里可能会报错,就是直接查询数据列表(使用了SSH)
public long countAll() {
List<?> l = getSession().createQuery("select count(*) from "
+ clazz.getSimpleName()).list();
if (l != null && l.size() == 1 )
{
return (Long)l.get(0);
}
return 0;
}
public List<T> queryByPage(String hql, int pageNo, int pageSize) {
return getSession()
.createQuery(hql)
.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
}


猜你喜欢
- 简单来说抽象类通常用来作为一个类族的最顶端的父类,用最底层的类表示现实中的具体事物,用最顶层的类表示该类族所有事物的共性。用abstract
- 一、安装JDK1.卸载旧版本或者系统自带的JDK(1)列出所有已安装的JDKrpm -qa | grep jdk(2)卸载不需要的JDKyu
- 本文实例讲述了Android编程计算函数时间戳的相关方法。分享给大家供大家参考,具体如下:对于做性能的人来说,知道时间的花在哪了是比较重要的
- 前言我们在使用spring security的时候可以通过好几种方法获取用户信息, 但是今天这篇文章介绍的是一个笔者觉得最优雅的实现; 借鉴
- 一、项目简述功能包括: 登录注册,办理借阅。借阅记录,预约借阅,借出未还, 借阅逾期,学生管理,图书管理,书库分类查询搜索。二、项目运行环境
- 由于今天用Security进行权限管理的时候出现了一些Bug,特此发这篇博客来补习一下对SpringSecurity的理解前言引入当今市面上
- 本文以实例形式讲述了C#命令模式的实现方法,分享给大家供大家参考。具体实现方法如下:现假设想让遥控器控制电灯的开关、电视机的开关和切换,该如
- 需求: 使用IO流将指定目录下的若干个音频文件的高潮部分,进行剪切,并重新拼接成一首新的音频文件 思路(以两首歌为例):第一首歌有
- JOL简介JOL的全称是Java Object Layout。是一个用来分析JVM中Object布局的小工具。包括Object在内存中的占用
- javabean与map的转换有很多种方式,比如:1、通过ObjectMapper先将bean转换为json,再将json转换为map,但是
- 本文讲述的是Android中RelativeLayout、FrameLayout的用法。具体如下:RelativeLayout是一个按照相对
- 前言:在日常开发中经常会遇到字符串匹配问题,我们就来学习使用Java中的一些方便快捷的方法来解决这个问题吧使用String类Java自带的字
- 仅供学习交流,禁止商业用途。如侵害利益,联系必删!前言最近一位小伙伴钟爱二次元文化,于是找到半次元这个app,但是很快他就遇到了问题。一、案
- 这篇文章主要介绍了Spring事务失效问题分析及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 传统方式克隆羊问题现在有一只羊 tom,姓名为: tom,年龄为:1,颜色为:白色,请编写程序创建和 tom羊属性完全相同的10只羊。传统方
- 之前知道spring支持JSR校验,在自己定义的bean中加入@NotNull,@NotBlank,@Length等之类的校验用于处理前台传
- 【说明】 TextView是用来显示文本的组件。以下介绍的是XML代码中的属性,在java代码中同样可通过 ”组件名.setXXX()方法设
- 网上关于下拉刷新的文章也不少,不过都太长了。恰好发现了官方的下拉刷新库,而且效果还是不错的,简洁美观,用得也挺方便。下面是效果图:我的好友原
- 项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,异常BOOT-INF/classes!/conf
- 1、引用*.wsdl文件WebService服务端会提供wsdl文件,客户端通过该文件生成.cs文件以及生成.dll.注意:若服务端只提供的