[项目构建]babasport 分页的使用及解析.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[项目构建]babasport 分页的使用及解析.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含29663字,纯文字阅读大概需要43分钟。
内容图文
首先说明: 这里分页是使用了SSM框架+ jsp 来做的, 当然分页还有其他的很多做法, 比如easyUI自带的分页效果. 但是这些原理都是很相似的, 再次只做为学习总结之用.
一, 效果图
这里的截图是来自百度, 当然我们的项目也是做成这种效果, 当点击第10页时, 相应页码数也要发生相应变化.
二, 代码实例
1, 首先我们在项目中加入了一个page 的jar包, 这个jar包有3个java文件组成, 这个jar包是事先写好的通用分页插件.
另外这里将源码也写上来:
Paginable.java:
1 package cn.itcast.common.page; 2 3 /** 4 * 分页接口 5 */ 6 public interface Paginable { 7 /** 8 * 总记录数 9 * 10 * @return 11 */ 12 public int getTotalCount(); 13 14 /** 15 * 总页数 16 * 17 * @return 18 */ 19 public int getTotalPage(); 20 21 /** 22 * 每页记录数 23 * 24 * @return 25 */ 26 public int getPageSize(); 27 28 /** 29 * 当前页号 30 * 31 * @return 32 */ 33 public int getPageNo(); 34 35 /** 36 * 是否第一页 37 * 38 * @return 39 */ 40 public boolean isFirstPage(); 41 42 /** 43 * 是否最后一页 44 * 45 * @return 46 */ 47 public boolean isLastPage(); 48 49 /** 50 * 返回下页的页号 51 */ 52 public int getNextPage(); 53 54 /** 55 * 返回上页的页号 56 */ 57 public int getPrePage(); 58 }
SimplePage.java:
1 package cn.itcast.common.page; 2 3 /** 4 * 简单分页类 5 */ 6 public class SimplePage implements java.io.Serializable,Paginable { 7 8privatestaticfinallong serialVersionUID = 1L; 9publicstaticfinalint DEF_COUNT = 20; 10 11/** 12 * 检查页码 checkPageNo 13 * 14 * @param pageNo 15 * @return if pageNo==null or pageNo<1 then return 1 else return pageNo 16*/ 17publicstaticint cpn(Integer pageNo) { 18return (pageNo == null || pageNo < 1) ? 1 : pageNo; 19 } 20 21public SimplePage() { 22 } 23 24/** 25 * 构造器 26 * 27 * @param pageNo 28 * 页码 29 * @param pageSize 30 * 每页几条数据 31 * @param totalCount 32 * 总共几条数据 33*/ 34public SimplePage(int pageNo, int pageSize, int totalCount) { 35 setTotalCount(totalCount); 36 setPageSize(pageSize); 37 setPageNo(pageNo); 38 adjustPageNo(); 39 40 } 41 42/** 43 * 调整页码,使不超过最大页数 44*/ 45publicvoid adjustPageNo() { 46if (pageNo == 1) { 47return; 48 } 49int tp = getTotalPage(); 50if (pageNo > tp) { 51 pageNo = tp; 52 } 53 } 54 55/** 56 * 获得页码 57*/ 58publicint getPageNo() { 59return pageNo; 60 } 61 62/** 63 * 每页几条数据 64*/ 65publicint getPageSize() { 66return pageSize; 67 } 68 69/** 70 * 总共几条数据 71*/ 72publicint getTotalCount() { 73return totalCount; 74 } 75 76/** 77 * 总共几页 78*/ 79publicint getTotalPage() { 80int totalPage = totalCount / pageSize; 81if (totalPage == 0 || totalCount % pageSize != 0) { 82 totalPage++; 83 } 84return totalPage; 85 } 86 87/** 88 * 是否第一页 89*/ 90publicboolean isFirstPage() { 91return pageNo <= 1; 92 } 93 94/** 95 * 是否最后一页 96*/ 97publicboolean isLastPage() { 98return pageNo >= getTotalPage(); 99 } 100101/**102 * 下一页页码 103*/104publicint getNextPage() { 105if (isLastPage()) { 106return pageNo; 107 } else { 108return pageNo + 1; 109 } 110 } 111112/**113 * 上一页页码 114*/115publicint getPrePage() { 116if (isFirstPage()) { 117return pageNo; 118 } else { 119return pageNo - 1; 120 } 121 } 122123protectedint totalCount = 0; 124protectedint pageSize = 20; 125protectedint pageNo = 1; 126127/**128 * if totalCount<0 then totalCount=0 129 * 130 * @param totalCount 131*/132publicvoid setTotalCount(int totalCount) { 133if (totalCount < 0) { 134this.totalCount = 0; 135 } else { 136this.totalCount = totalCount; 137 } 138 } 139140/**141 * if pageSize< 1 then pageSize=DEF_COUNT 142 * 143 * @param pageSize 144*/145publicvoid setPageSize(int pageSize) { 146if (pageSize < 1) { 147this.pageSize = DEF_COUNT; 148 } else { 149this.pageSize = pageSize; 150 } 151 } 152153/**154 * if pageNo < 1 then pageNo=1 155 * 156 * @param pageNo 157*/158publicvoid setPageNo(int pageNo) { 159if (pageNo < 1) { 160this.pageNo = 1; 161 } else { 162this.pageNo = pageNo; 163 } 164 } 165 }
Pagination.java:
1 package cn.itcast.common.page; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 列表分页。包含list属性。 8 */ 9 public class Pagination extends SimplePage{ 10 11public Pagination() { 12 } 13 14/** 15 * 构造器 16 * 17 * @param pageNo 18 * 页码 19 * @param pageSize 20 * 每页几条数据 21 * @param totalCount 22 * 总共几条数据 23*/ 24public Pagination(int pageNo, int pageSize, int totalCount) { 25super(pageNo, pageSize, totalCount); 26 27 } 28 29/** 30 * 构造器 31 * 32 * @param pageNo 33 * 页码 34 * @param pageSize 35 * 每页几条数据 36 * @param totalCount 37 * 总共几条数据 38 * @param list 39 * 分页内容 40*/ 41public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) { 42super(pageNo, pageSize, totalCount); 43this.list = list; 44 } 45 46/** 47 * 第一条数据位置 48 * 49 * @return 50*/ 51publicint getFirstResult() { 52return (pageNo - 1) * pageSize; 53 } 54 55/** 56 * 当前页的数据 57*/ 58private List<?> list; 59 60/** 61 * 当前页的分页样式 62*/ 63private List<String> pageView; 64 65/** 66 * 获得分页内容 67 * 68 * @return 69*/ 70public List<?> getList() { 71return list; 72 } 73 74/** 75 * 设置分页内容 76 * 77 * @param list 78*/ 79 @SuppressWarnings("unchecked") 80publicvoid setList(List list) { 81this.list = list; 82 } 83/** 84 * 获得分页样式 85 * 86 * @return 87*/ 88public List<String> getPageView() { 89return pageView; 90 } 91/** 92 * 设置分页样式 93 * 94 * @param list 95*/ 96publicvoid setPageView(List<String> pageView) { 97this.pageView = pageView; 98 } 99100101/**102 * 分页显示样示部分 103*/104publicvoid pageView(String url,String params){ 105106 pageView = new ArrayList<String>(); 107108if(this.pageNo != 1){ 109 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo=1‘\"><font size=2>首页</font></a>"); 110 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"‘\"><font size=2>上一页</font></a>"); 111 }else{ 112 pageView.add("<font size=2>首页</font>"); 113 pageView.add("<font size=2>上一页</font>"); 114 } 115116if(this.getTotalPage() <= 10){ 117for (int i = 0; i < this.getTotalPage(); i++) { 118if((i+1)==this.pageNo){ 119 pageView.add("<strong>"+this.pageNo+"</strong>"); 120 i = i+1; 121if(this.pageNo==this.getTotalPage())break; 122 } 123 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 124 } 125 }elseif(this.getTotalPage() <= 20){ 126//没有把...加上127int l = 0; 128int r = 0; 129if(this.pageNo<5){ 130 l=this.pageNo-1; 131 r=10-l-1; 132 }elseif(this.getTotalPage()-this.pageNo<5){ 133 r=this.getTotalPage()-this.pageNo; 134 l=10-1-r; 135 }else{ 136 l=4; 137 r=5; 138 } 139int tmp = this.pageNo-l; 140for (int i = tmp; i < tmp+10; i++) { 141if(i==this.pageNo){ 142 pageView.add("<strong>"+this.pageNo+"</strong>"); 143 i = i+1; 144if(this.pageNo==this.getTotalPage()) break; 145 } 146 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i)+"‘\">"+(i)+"</a>"); 147 } 148149 }elseif(this.pageNo<7){ 150for (int i = 0; i < 8; i++) { 151if(i+1==this.pageNo){ 152 pageView.add("<strong>"+this.pageNo+"</strong>"); 153 i = i+1; 154 } 155 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 156 } 157 pageView.add("..."); 158 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"‘\">"+(this.getTotalPage()-1)+"</a>"); 159 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"‘\">"+(this.getTotalPage())+"</a>"); 160 }elseif(this.pageNo>this.getTotalPage()-6){ 161 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(1)+"‘\">"+(1)+"</a>"); 162 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(2)+"‘\">"+(2)+"</a>"); 163 pageView.add("..."); 164for (int i = this.getTotalPage()-8; i <this.getTotalPage() ; i++) { 165if(i+1==this.pageNo){ 166 pageView.add("<strong>"+this.pageNo+"</strong>"); 167 i = i+1; 168if(this.pageNo==this.getTotalPage()) break; 169 } 170 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 171 } 172 }else{ 173 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(1)+"‘\">"+(1)+"</a>"); 174 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(2)+"‘\">"+(2)+"</a>"); 175 pageView.add("..."); 176177 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-2)+"‘\">"+(this.pageNo-2)+"</a>"); 178 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"‘\">"+(this.pageNo-1)+"</a>"); 179 pageView.add("<strong>"+this.pageNo+"</strong>"); 180 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"‘\">"+(this.pageNo+1)+"</a>"); 181 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+2)+"‘\">"+(this.pageNo+2)+"</a>"); 182183 pageView.add("..."); 184 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"‘\">"+(this.getTotalPage()-1)+"</a>"); 185 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"‘\">"+(this.getTotalPage())+"</a>"); 186 } 187if(this.pageNo != this.getTotalPage()){ 188 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"‘\"><font size=2>下一页</font></a>"); 189 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+this.getTotalPage()+"‘\"><font size=2>尾页</font></a>"); 190 } else{ 191 pageView.add("<font size=2>下一页</font>"); 192 pageView.add("<font size=2>尾页</font>"); 193 } 194 pageView.add("共<var>" + getTotalPage() + "</var>页 到第<input type=‘text‘ id=‘PAGENO‘ size=‘3‘ />页 <input type=‘button‘ id=‘skip‘ class=‘hand btn60x20‘ value=‘确定‘ onclick=\"javascript:window.location.href = ‘" + url + "?" + params + "&pageNo=‘ + $(‘#PAGENO‘).val() \"/>"); 195 } 196 }
2, 下面源码就是需要自己在项目中构建
下面开始从Controller(babasport-console)-->ServiceImpl(babasport-product)-->DaoMapper(babasport-dao)-->jsp展示
Controller层:
BrandController.java:
1 package cn.itcast.core.controller; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import cn.itcast.common.page.Pagination; 11 import cn.itcast.core.bean.product.Brand; 12 import cn.itcast.core.service.product.BrandService; 13 14 /* 15 * 品牌 16 */ 17 @Controller 18 @RequestMapping(value="/brand") 19publicclass BrandController { 2021 @Autowired 22private BrandService brandService; 2324 @RequestMapping(value="/list.do") 25public String list(Integer pageNo, String name, Integer isDisplay, Model model){ 26//List<Brand> brands = brandService.selectBrandListByQuery(name, isDisplay);27 Pagination pagination = brandService.selectPaginationByQuery(pageNo, name, isDisplay); 28 model.addAttribute("pagination", pagination); 29//回显查询条件30 model.addAttribute("name", name); 31if(isDisplay != null){ 32 model.addAttribute("isDisplay", isDisplay); 33 }else{ 34 model.addAttribute("isDisplay", 1); 35 } 36return "brand/list"; 37 } 3839 @RequestMapping(value="/toEdit.do") 40public String selectBrandById(Long id, Model model){ 4142 Brand brand = brandService.selectBrandById(id); 43 model.addAttribute("brand", brand); 4445return "brand/edit"; 46 } 47 }
解析: 这个controller 包括两个方法, 一个是查询分页数据, 另一个是根据ID 回显数据.
两个方法都很简单, 第一个就是根据页面传递过来的当前页数和查询条件通过service层去做进一步处理后再通过Dao层去查询结果, 将查询的结果返回后再封装到Model里面, 然后转发到视图层.
第二个方法更简单了, 通过Id查询到相应的商品然后回显数据即可.
Service层:
BrandServiceImpl.java:
1 package cn.itcast.core.service.product; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import cn.itcast.common.page.Pagination; 10 import cn.itcast.core.bean.product.Brand; 11 import cn.itcast.core.bean.product.BrandQuery; 12 import cn.itcast.core.dao.product.BrandDao; 13 @Service("brandService") 14@Transactional 15publicclass BrandServiceImpl implements BrandService { 1617 @Autowired 18private BrandDao brandDao; 1920 @Override 21public List<Brand> selectBrandListByQuery(String name, Integer isDisplay) { 22 BrandQuery brandQuery = new BrandQuery(); 23if (name != null) { 24 brandQuery.setName(name); 25 } 26if (isDisplay != null) { 27 brandQuery.setIsDisplay(isDisplay); 28 } 29else { 30//是: 1, 否:031 brandQuery.setIsDisplay(1); 32 } 3334return brandDao.selectBrandListByQuery(brandQuery); 35 } 3637//分页38public Pagination selectPaginationByQuery(Integer pageNo, String name, Integer isDisplay) { 39 BrandQuery brandQuery = new BrandQuery(); 40//设置当前页 cpn方法: 如果pageNumber 是null或者 小于1, 那么就将pageNumber设置为1, 如果不是则使用传递进来的pageNumber41 brandQuery.setPageNo(Pagination.cpn(pageNo)); 42//设置每页数43 brandQuery.setPageSize(3); 4445//拼接条件46 StringBuilder sb = new StringBuilder(); 4748if (name != null) { 49 brandQuery.setName(name); 50 sb.append("name=").append(name); 51 } 5253if (isDisplay != null) { 54 brandQuery.setIsDisplay(isDisplay); 55 sb.append("&isDisplay=").append(isDisplay); 56 } 57else { 58//是: 1, 否:059 brandQuery.setIsDisplay(1); 60 sb.append("&isDisplay=").append(1); 61 } 62//构建分页对象 63//三个参数: 当前页,每页显示行数,总记录数64 Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(), brandDao.selectCount(brandQuery)); 65//查询结果集 66//使用查询语句: select * from bbs_brand where ... limit (pageNumber - 1) * 3, 367 pagination.setList(brandDao.selectBrandListByQuery(brandQuery)); 6869//分页在页面显示 /brand/list.do?name=aaa&&isDisplay=070 String url = "/brand/list.do"; 71 pagination.pageView(url, sb.toString()); 7273return pagination; 74 } 7576//通过ID查询一个品牌77public Brand selectBrandById(Long id){ 78return brandDao.selectBrandById(id); 79 } 8081 }
解析: Service层中主要来说第二个分页的方法.
1 brandQuery.setPageNo(Pagination.cpn(pageNo));
这个cpn方法是Pagination中封装好的静态方法, 我们来看下源码:
1 /** 2 * 检查页码 checkPageNo 3 * 4 * @param pageNo 5 * @return if pageNo==null or pageNo<1 then return 1 else return pageNo 6 */ 7 public static int cpn(Integer pageNo) { 8 return (pageNo == null || pageNo < 1) ? 1 : pageNo; 9 }
使用StringBuilder 封装查询条件, 因为当我们根据查询条件查询到的数据也有分页效果时, 这时候我们点击页码的按钮时跳转到相应的页数后, 查询条件也应该回显.
1 // 构建分页对象 2 // 三个参数: 当前页,每页显示行数,总记录数 3 Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(), brandDao.selectCount(brandQuery)); 4//查询结果集 5//使用查询语句: select * from bbs_brand where ... limit (pageNumber - 1) * 3, 3 6pagination.setList(brandDao.selectBrandListByQuery(brandQuery)); 7 8//分页在页面显示 /brand/list.do?name=aaa&&isDisplay=0 9 String url = "/brand/list.do"; 10 pagination.pageView(url, sb.toString());
构建分页对象, 将查询的结果封装到pagination中, 且 将url和封装的条件封装到pageView中. 这里因为页码按钮的样式是固定的, 不固定的只是我们点击 每一个按钮跳转的url和查询的条件不同, 所以这里使用pageView属性来封装url和查询条件.
Dao层:
BrandDao.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <mapper namespace="cn.itcast.core.dao.product.BrandDao"> 5 6 <resultMap type="Brand" id="brand"> 7 <result column="img_url" property="imgUrl"/> 8 <result column="is_display" property="isDisplay"/> 9 </resultMap> 10 <!-- 查询品牌结果集 List<Brand> --> 11 <select id="selectBrandListByQuery" parameterType="BrandQuery" resultMap="brand"> 12 select id ,name,description,img_url,is_display,sort 13 from bbs_brand 14 <where> 15 <if test="name != null"> 16 name like "%"#{name}"%" 17 </if> 18 <if test="isDisplay != null"> 19 and is_display = #{isDisplay} 20 </if> 21 </where> 22 <if test="startRow != null"> 23 limit #{startRow}, #{pageSize} 24 </if> 25 </select> 2627 <!-- 查询品牌结果集 List<Brand> --> 28 <select id="selectCount" parameterType="BrandQuery" resultType="Integer"> 29 select count(1) 30 from bbs_brand 31 <where> 32 <if test="name != null"> 33 name like "%"#{name}"%" 34 </if> 35 <if test="isDisplay != null"> 36 and is_display = #{isDisplay} 37 </if> 38 </where> 39 </select> 4041 <!-- 根据ID查询 --> 42 <select id="selectBrandById" parameterType="Long" resultMap="brand"> 43 select id ,name,description,img_url,is_display,sort 44 from bbs_brand 45 <where> 46 id = #{id} 47 </where> 48 </select> 49 </mapper>
Model
BrandQuery.java:
1 package cn.itcast.core.bean.product; 2 3 import java.io.Serializable; 4 5 public class BrandQuery implements Serializable{ 6/** 7 * 默认的ID 8*/ 9privatestaticfinallong serialVersionUID = 1L; 10 11//品牌ID bigint 12private Long id; 13//品牌名称 14private String name; 15//描述 16private String description; 17//图片URL 18private String imgUrl; 19//排序 越大越靠前 20private Integer sort; 21//是否可用 0 不可用 1 可用 22private Integer isDisplay;//is_display 23public Long getId() { 24return id; 25 } 26publicvoid setId(Long id) { 27this.id = id; 28 } 29public String getName() { 30return name; 31 } 32publicvoid setName(String name) { 33this.name = name; 34 } 35public String getDescription() { 36return description; 37 } 38publicvoid setDescription(String description) { 39this.description = description; 40 } 41public String getImgUrl() { 42return imgUrl; 43 } 44publicvoid setImgUrl(String imgUrl) { 45this.imgUrl = imgUrl; 46 } 47public Integer getSort() { 48return sort; 49 } 50publicvoid setSort(Integer sort) { 51this.sort = sort; 52 } 53public Integer getIsDisplay() { 54return isDisplay; 55 } 56publicvoid setIsDisplay(Integer isDisplay) { 57this.isDisplay = isDisplay; 58 } 59publicstaticlong getSerialversionuid() { 60return serialVersionUID; 61 } 62 @Override 63public String toString() { 64return "Brand [id=" + id + ", name=" + name + ", description=" + description + ", imgUrl=" + imgUrl + ", sort=" 65 + sort + ", isDisplay=" + isDisplay + "]"; 66 } 67 68//附加字段 69//当前页 70private Integer pageNo = 1; 71 72//每页数 73private Integer pageSize = 10; 74 75//开始行 76private Integer startRow; 77public Integer getPageNo() { 78return pageNo; 79 } 80publicvoid setPageNo(Integer pageNo) { 81//计算开始行 82this.startRow = (pageNo - 1)*pageSize; 83this.pageNo = pageNo; 84 } 85public Integer getPageSize() { 86return pageSize; 87 } 88publicvoid setPageSize(Integer pageSize) { 89//计算开始行 90this.startRow = (pageNo - 1)*pageSize; 91this.pageSize = pageSize; 92 } 93public Integer getStartRow() { 94return startRow; 95 } 96publicvoid setStartRow(Integer startRow) { 97this.startRow = startRow; 98 } 99100101 }
解析: mapper和model
这里model这接计算好了startRow, 所以在mapper就可以直接查询了.
1 // 附加字段 2 // 当前页 3 private Integer pageNo = 1; 4 5//每页数 6private Integer pageSize = 10; 7 8//开始行 9private Integer startRow; 10public Integer getPageNo() { 11return pageNo; 12} 13publicvoid setPageNo(Integer pageNo) { 14//计算开始行15this.startRow = (pageNo - 1)*pageSize; 16this.pageNo = pageNo; 17} 18public Integer getPageSize() { 19return pageSize; 20} 21publicvoid setPageSize(Integer pageSize) { 22//计算开始行23this.startRow = (pageNo - 1)*pageSize; 24this.pageSize = pageSize; 25} 26public Integer getStartRow() { 27return startRow; 28} 29publicvoid setStartRow(Integer startRow) { 30this.startRow = startRow; 31 }
View层:
list.jsp:
1 <% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %> 2 <% @ include file = " ../head.jsp " %> 3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 4 < html xmlns ="http://www.w3.org/1999/xhtml" > 5 < head > 6 < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" /> 7 < title >babasport-list</title> 8</head> 9<body>10<div class="box-positon">11<div class="rpos">当前位置: 品牌管理 - 列表</div>12<form class="ropt">13<input class="add" type="button" value="添加" onclick="javascript:window.location.href=‘add.jsp‘"/>14</form>15<div class="clear"></div>16</div>17<div class="body-box">18<form action="/brand/list.do" method="post" style="padding-top:5px;">19 品牌名称: <input type="text" name="name" value="${name}"/>20<select name="isDisplay">21<option value="1" <c:if test="${isDisplay==1 }">selected="selected"</c:if>>是</option>22<option value="0" <c:if test="${isDisplay==0 }">selected="selected"</c:if>>否</option>23</select>24<input type="submit" class="query" value="查询"/>25</form>26<table cellspacing="1" cellpadding="0" border="0" width="100%" class="pn-ltable">27<thead class="pn-lthead">28<tr>29<th width="20"><input type="checkbox" onclick="checkBox(‘ids‘,this.checked)"/></th>30<th>品牌ID</th>31<th>品牌名称</th>32<th>品牌图片</th>33<th>品牌描述</th>34<th>排序</th>35<th>是否可用</th>36<th>操作选项</th>37</tr>38</thead>39<tbody class="pn-ltbody">40<c:forEach items="${pagination.list }" var="brand">41<tr bgcolor="#ffffff" onmouseout="this.bgColor=‘#ffffff‘" onmouseover="this.bgColor=‘#eeeeee‘">42<td><input type="checkbox" value="${brand.id }" name="ids"/></td>43<td align="center">${brand.id }</td>44<td align="center">${brand.name }</td>45<td align="center"><img width="40" height="40" src="/images/pic/ppp.jpg"/></td>46<td align="center"></td>47<td align="center">${brand.sort}</td>48<td align="center">49<c:if test="${brand.isDisplay == 1 }">是</c:if>50<c:if test="${brand.isDisplay == 0 }">否</c:if>51</td>52<td align="center">53<a class="pn-opt" href="/brand/toEdit.do?id=${brand.id}">修改</a> | <a class="pn-opt" onclick="if(!confirm(‘您确定删除吗?‘)) {return false;}" href="#">删除</a>54</td>55</tr>5657</c:forEach>5859</tbody>60</table>61<div class="page pb15">62<span class="r inb_a page_b">63<c:forEach items="${pagination.pageView }" var="page">64 ${page } 65</c:forEach>66</span>67</div>68<div style="margin-top:15px;"><input class="del-button" type="button" value="删除" onclick="optDelete();"/></div>69</div>70</body>71</html>
解析: 这里需要添加一些必要的说明:
这里 在显示分页页码的时候直接使用了 ${page}, 到底这个是怎么实现的呢? 下面来看下源码中的pageView.
1 /** 2 * 分页显示样示部分 3 */ 4 public void pageView(String url,String params){ 5 6 pageView = new ArrayList<String>(); 7 8if(this.pageNo != 1){ 9 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo=1‘\"><font size=2>首页</font></a>"); 10 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"‘\"><font size=2>上一页</font></a>"); 11 }else{ 12 pageView.add("<font size=2>首页</font>"); 13 pageView.add("<font size=2>上一页</font>"); 14 } 1516if(this.getTotalPage() <= 10){ 17for (int i = 0; i < this.getTotalPage(); i++) { 18if((i+1)==this.pageNo){ 19 pageView.add("<strong>"+this.pageNo+"</strong>"); 20 i = i+1; 21if(this.pageNo==this.getTotalPage())break; 22 } 23 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 24 } 25 }elseif(this.getTotalPage() <= 20){ 26//没有把...加上27int l = 0; 28int r = 0; 29if(this.pageNo<5){ 30 l=this.pageNo-1; 31 r=10-l-1; 32 }elseif(this.getTotalPage()-this.pageNo<5){ 33 r=this.getTotalPage()-this.pageNo; 34 l=10-1-r; 35 }else{ 36 l=4; 37 r=5; 38 } 39int tmp = this.pageNo-l; 40for (int i = tmp; i < tmp+10; i++) { 41if(i==this.pageNo){ 42 pageView.add("<strong>"+this.pageNo+"</strong>"); 43 i = i+1; 44if(this.pageNo==this.getTotalPage()) break; 45 } 46 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i)+"‘\">"+(i)+"</a>"); 47 } 4849 }elseif(this.pageNo<7){ 50for (int i = 0; i < 8; i++) { 51if(i+1==this.pageNo){ 52 pageView.add("<strong>"+this.pageNo+"</strong>"); 53 i = i+1; 54 } 55 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 56 } 57 pageView.add("..."); 58 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"‘\">"+(this.getTotalPage()-1)+"</a>"); 59 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"‘\">"+(this.getTotalPage())+"</a>"); 60 }elseif(this.pageNo>this.getTotalPage()-6){ 61 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(1)+"‘\">"+(1)+"</a>"); 62 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(2)+"‘\">"+(2)+"</a>"); 63 pageView.add("..."); 64for (int i = this.getTotalPage()-8; i <this.getTotalPage() ; i++) { 65if(i+1==this.pageNo){ 66 pageView.add("<strong>"+this.pageNo+"</strong>"); 67 i = i+1; 68if(this.pageNo==this.getTotalPage()) break; 69 } 70 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(i+1)+"‘\">"+(i+1)+"</a>"); 71 } 72 }else{ 73 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(1)+"‘\">"+(1)+"</a>"); 74 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(2)+"‘\">"+(2)+"</a>"); 75 pageView.add("..."); 7677 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-2)+"‘\">"+(this.pageNo-2)+"</a>"); 78 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo-1)+"‘\">"+(this.pageNo-1)+"</a>"); 79 pageView.add("<strong>"+this.pageNo+"</strong>"); 80 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"‘\">"+(this.pageNo+1)+"</a>"); 81 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+2)+"‘\">"+(this.pageNo+2)+"</a>"); 8283 pageView.add("..."); 84 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage()-1)+"‘\">"+(this.getTotalPage()-1)+"</a>"); 85 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.getTotalPage())+"‘\">"+(this.getTotalPage())+"</a>"); 86 } 87if(this.pageNo != this.getTotalPage()){ 88 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+(this.pageNo+1)+"‘\"><font size=2>下一页</font></a>"); 89 pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href=‘" + url + "?" + params + "&pageNo="+this.getTotalPage()+"‘\"><font size=2>尾页</font></a>"); 90 } else{ 91 pageView.add("<font size=2>下一页</font>"); 92 pageView.add("<font size=2>尾页</font>"); 93 } 94 pageView.add("共<var>" + getTotalPage() + "</var>页 到第<input type=‘text‘ id=‘PAGENO‘ size=‘3‘ />页 <input type=‘button‘ id=‘skip‘ class=‘hand btn60x20‘ value=‘确定‘ onclick=\"javascript:window.location.href = ‘" + url + "?" + params + "&pageNo=‘ + $(‘#PAGENO‘).val() \"/>"); 95 }
这里是直接将展示页拼接了出来, 而且加了url和查询参数.
到了这里整个分页的流程就搞完了, 下面来看下整体效果:
原文:http://www.cnblogs.com/wang-meng/p/5792934.html
内容总结
以上是互联网集市为您收集整理的[项目构建]babasport 分页的使用及解析.全部内容,希望文章能够帮你解决[项目构建]babasport 分页的使用及解析.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。