java后台生成分页_Java实现分页的前台页面和后台代码

本文实例为大家分享了Java分页展示的具体代码,供大家参考,具体内容如下

先上图吧,大致如图,也就提供个思路(ps:使用了SSH框架)

5a51c4d1949d849e62f698442945d492.png

前台JSP页面

pageEncoding="utf-8"%>

数据交易猫

//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');

}

});

})

需求列表

筛选:按

最新

价格降序


悬赏积分:${price}

需求描述:${requirementDescription}


  • «

  • 首页
  • ${currentPage}/ ${allPage}页

  • 尾页
  • »

  • 跳转到第 

     页


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 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 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 queryByPage(String hql, int pageNo, int pageSize) {

return getSession()

.createQuery(hql)

.setFirstResult((pageNo - 1) * pageSize)

.setMaxResults(pageSize)

.list();

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java分页实现的示例代码: ```java public class Paging { private int pageSize; // 每页显示的记录数 private int currentPage; // 当前页码 private int totalCount; // 总记录数 private int totalPage; // 总页数 public Paging(int pageSize, int currentPage, int totalCount) { this.pageSize = pageSize; this.currentPage = currentPage; this.totalCount = totalCount; // 计算总页数 if (this.totalCount % this.pageSize == 0) { this.totalPage = this.totalCount / this.pageSize; } else { this.totalPage = this.totalCount / this.pageSize + 1; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } /** * 获取当前页的起始记录索引 * * @return */ public int getStartIndex() { return (this.currentPage - 1) * this.pageSize; } /** * 获取当前页的结束记录索引 * * @return */ public int getEndIndex() { return Math.min(this.currentPage * this.pageSize - 1, this.totalCount - 1); } } ``` 使用方法: ```java // 假设数据库中有100条记录,每页显示10条记录 Paging paging = new Paging(10, 1, 100); System.out.println("总共有" + paging.getTotalPage() + "页"); System.out.println("当前页的起始记录索引为:" + paging.getStartIndex()); System.out.println("当前页的结束记录索引为:" + paging.getEndIndex()); ``` 输出结果: ``` 总共有10页 当前页的起始记录索引为:0 当前页的结束记录索引为:9 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值