博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用pager进行分页
阅读量:5291 次
发布时间:2019-06-14

本文共 5725 字,大约阅读时间需要 19 分钟。

pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm

 

package com.binary.entity;import java.util.List;public class PageModel
{ private long total;//页数 private List
dates;//当前页的数据 public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public List
getDates() { return dates; } public void setDates(List
dates) { this.dates = dates; } }

 

package com.binary.entity;public class Pager {	private int offset;//offset表示从那一页开始记录	public int getOffset() {		return offset;	}	public void setOffset(int offset) {		this.offset = offset;	}	}

 

 

package com.binary.entity;import java.util.HashSet;import java.util.Set;/** * User entity. @author MyEclipse Persistence Tools */public class User implements java.io.Serializable {    // Fields    private Integer id;    private String uname;    private String upass;    private String meun;    // Constructors    /** default constructor */    public User() {    }    /** minimal constructor */    public User(String meun) {        this.meun = meun;    }    /** full constructor */    public User(String uname, String upass, String meun, Set meuns) {        this.uname = uname;        this.upass = upass;        this.meun = meun;    }    // Property accessors    public Integer getId() {        return this.id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUname() {        return this.uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public String getUpass() {        return this.upass;    }    public void setUpass(String upass) {        this.upass = upass;    }    public String getMeun() {        return this.meun;    }    public void setMeun(String meun) {        this.meun = meun;    }}

 

 

import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.binary.entity.PageModel;import com.binary.entity.User;public class UserDao {    public PageModel
getUsers(int offset,int maxResult) { Configuration cf=new Configuration().configure(); SessionFactory sf=cf.buildSessionFactory(); Session session=sf.openSession(); Query q= session.createQuery("from User"); PageModel
users=new PageModel
(); users.setTotal(q.list().size()); q.setFirstResult(offset); q.setMaxResults(maxResult); users.setDates(q.list()); session.close(); return users; }}

 

 

package com.dan.biz;import com.binary.entity.PageModel;import com.binary.entity.User;import com.dan.dao.UserDao;public class UserBiz {    public PageModel
getUsers(int offset,int maxResult) { return new UserDao().getUsers(offset, maxResult); }}

 

 

package com.dan.action;import org.apache.struts2.ServletActionContext;import com.binary.entity.PageModel;import com.binary.entity.Pager;import com.binary.entity.User;import com.dan.biz.UserBiz;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {        private Pager pager=new Pager();//存放偏移量    private int numPerPage=2;//每页的数据量    private long totalCount;//总页数    private String str;            public String getStr() {        return str;    }    public void setStr(String str) {        this.str = str;    }    public Pager getPager() {        return pager;    }    public void setPager(Pager pager) {        this.pager = pager;    }    public int getNumPerPage() {        return numPerPage;    }    public void setNumPerPage(int numPerPage) {        this.numPerPage = numPerPage;    }    public long getTotalCount() {        return totalCount;    }    public void setTotalCount(long totalCount) {        this.totalCount = totalCount;    }    public String execute() {        System.out.println(str);        UserBiz biz=new UserBiz();        PageModel
users=biz.getUsers(pager.getOffset(), numPerPage); totalCount=users.getTotal(); ServletActionContext.getRequest().setAttribute("user", users.getDates()); return SUCCESS; }}

 

page.tag封装成tag标签<%@tag pageEncoding="utf-8" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="pg" uri="/WEB-INF/lib/pager-taglib.jar" %><%@attribute name="color" required="true" %><%@attribute name="totalCount" required="true" rtexprvalue="true" %><%@attribute name="numPerPage" required="true" rtexprvalue="true" %>
首页
前页
${pageNumber }
${pageNumber }
下一页
尾页
${pageUrl }

 

 

jsp代码<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="pg" uri="/WEB-INF/lib/pager-taglib.jar" %><%@ taglib prefix="page" tagdir="/WEB-INF/tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
${u.uname }

 

转载于:https://www.cnblogs.com/danmao/p/4190681.html

你可能感兴趣的文章
Linux 中【./】和【/】和【.】之间有什么区别?
查看>>
Ubuntu sudo 出现 is not in the sudoers file解决方案
查看>>
内存地址对齐
查看>>
看门狗 (监控芯片)
查看>>
#ifndef #define #endif
查看>>
css背景样式
查看>>
JavaScript介绍
查看>>
js中函数与对象的使用
查看>>
正则表达式
查看>>
开源网络漏洞扫描软件
查看>>
yum 命令跳过特定(指定)软件包升级方法
查看>>
创新课程管理系统数据库设计心得
查看>>
Hallo wolrd!
查看>>
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
springBoot配置elasticsearch搜索
查看>>
Chapter 3 Phenomenon——12
查看>>
中小学教育缴费遇到的一些问题
查看>>
FAIR开源Detectron:整合全部顶尖目标检测算法
查看>>
C语言中求最大最小值的库函数
查看>>