页面分页
2016-06-02 14:30
357 查看
查询列表,实现页面分页:
public class Page {
private int curPage;//当前页
private int length;//每页条数
private int count;//总条数
private int pageNum;//总页数
private int start;
public Page(int curPage ,int length){
this.curPage = curPage;
this.length = length;
if(this.curPage < 1){
this.curPage = 1;
}
if(this.length < 1){
this.length = 1 ;
}
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
this.pageNum = (count+length-1) / length;
if(pageNum == 0)
pageNum = 1;
if(pageNum < curPage){
curPage = pageNum;
}
start = (curPage-1) * length;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
}sql语句:
public Content getById(long id); public List<Content> queryContentList(Map<String,Object> map);
public int queryContentListCount(Map<String,Object> map);
@RequestMapping(value="/list/{channelId}.html")
public ModelAndView contentPage(@PathVariable long channelId,ModelMap model,HttpServletRequest request,HttpServletResponse rps,@RequestParam(defaultValue="1") int curPage) throws IOException{
Channel channel = channelService.getById(channelId);
String pageView = "";
Page page = new Page(curPage,20);
if(channel != null && channel.getList() != null && channel.getList().size() > 0){
//跳转3级栏目页面
pageView = "f/content3";
}else{
//查询内容页信息
int count1 = contentService.queryContentListCount(channelId, 0);
page.setCount(count1);
List<Content> contentList = contentService.queryContentList(page, channelId, 0);
if(count1 == 1){
Content content = contentList.get(0);
Customer customer = null;
if(content.getDoctorId() != null){
customer = customerService.getCustomerById(content.getDoctorId());
if(customer != null){
content.setJob(customer.getTitle());
content.setDepartmentName(customer.getDeptmentName());
}
}
contentService.updateContentPv(content.getId());
content.setPv(content.getPv() + 1);
model.addAttribute("content", content);
pageView = "f/content";
}else{
//列表
pageView = "f/contentList";
model.add
4000
Attribute("contentList", contentList);
model.addAttribute("page", page);
}
}
model.addAttribute("curId", channelId);
model.addAttribute("channel", channel);
List<Channel> channelList = channel.getParent().getList();
model.addAttribute("channelList", channelList);
model.addAttribute("childChannelList", channel.getList());
return new ModelAndView(pageView);
}jsp
<!--分页-->
<div class="feyestyle" style="float:none;">
<c:set var="pageStart" value="1"></c:set>
<c:if test="${page.curPage - 7 > 1 }">
<c:set var="pageStart" value="${page.curPage - 7 }"></c:set>
</c:if>
<c:set var="pageEnd" value="${page.pageNum }"></c:set>
<c:if test="${page.curPage + 7 < page.pageNum }">
<c:set var="pageEnd" value="${page.curPage + 7 }"></c:set>
</c:if>
<c:choose>
<c:when test="${page.curPage - 1 > 0 }">
<a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage-1}">< 上一页</a>
</c:when>
<c:otherwise>
<span class="feyedisabled">< 上一页</span>
</c:otherwise>
</c:choose>
<c:forEach var="incPage" begin="${pageStart }" end="${pageEnd }"
step="1">
<c:choose>
<c:when test="${page.curPage == incPage }">
<span class="feyecurrent"><c:out value="${incPage }"></c:out></span>
</c:when>
<c:otherwise>
<a href="<ph:root/>/list/${channel.id }.html?curPage=${incPage}"><c:out value="${incPage }"></c:out></a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.curPage + 1 <= page.pageNum }">
<a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage+1}">下一页 > </a>
</c:when>
<c:otherwise>
<span class="feyedisabled">< 下一页</span>
</c:otherwise>
</c:choose>
<span style="margin-left: 20px;">共计 <font class="feyefont-color">${page.count}</font> 条信息
</span>
</div>
<!--分页-->
public class Page {
private int curPage;//当前页
private int length;//每页条数
private int count;//总条数
private int pageNum;//总页数
private int start;
public Page(int curPage ,int length){
this.curPage = curPage;
this.length = length;
if(this.curPage < 1){
this.curPage = 1;
}
if(this.length < 1){
this.length = 1 ;
}
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
this.pageNum = (count+length-1) / length;
if(pageNum == 0)
pageNum = 1;
if(pageNum < curPage){
curPage = pageNum;
}
start = (curPage-1) * length;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
}sql语句:
<select id="getById" parameterType="long" resultMap="ContentResultMap" > <span style="white-space:pre"> </span> SELECT * FROM t_content WHERE id = #{id} <span style="white-space:pre"> </span> </select> <select id="queryContentList" parameterType="java.util.Map" resultMap="ContentResultMap"> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>SELECT <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.ID, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.TITLE, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.time, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.pv, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.from_name, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.writer, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.content, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.top_thumb_url, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.article_abstract , <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>p.name as channel_name,//其他表字段 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.channel_id, <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c.type <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>FROM t_content c <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>LEFT JOIN t_plate p ON c.CHANNEL_ID = p.ID//左外连接 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>WHERE p.id = #{id} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span><if test="type != 0"> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>AND c.TYPE = #{type} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span></if> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>AND c.STATUS = 2 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>ORDER BY c.time DESC LIMIT #{start} , #{length}//分页时每一页长度 <span style="white-space:pre"> </span> </select><span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <select id="queryContentListCount" parameterType="java.util.Map" resultType="int"> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>SELECT count(c.id) FROM t_content c <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>LEFT JOIN t_plate p ON c.CHANNEL_ID = p.ID <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>WHERE p.id = #{id} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span><if test="type != 0"> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>AND c.TYPE = #{type} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span></if> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>AND c.STATUS = 2 <span style="white-space:pre"> </span> </select> <span style="white-space:pre"> </span>mapper、service、serviceimpl
public Content getById(long id); public List<Content> queryContentList(Map<String,Object> map);
public int queryContentListCount(Map<String,Object> map);
public Content getContentById(long id); public List<Content> queryContentList(Page page,long id,int type); public int queryContentListCount(long id,int type);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
public List<Content> queryContentList(Page page,long id,int type){ Map<String,Object> map = new HashMap<String,Object>(); map.put("start", page.getStart()); map.put("length",page.getLength()); map.put("id", id); map.put("type", type); List<Content> list = contentMapper.queryContentList(map); return list; } public int queryContentListCount(long id,int type){ Map<String,Object> map = new HashMap<String,Object>(); map.put("id", id); map.put("type", type); return contentMapper.queryContentListCount(map); }controller
@RequestMapping(value="/list/{channelId}.html")
public ModelAndView contentPage(@PathVariable long channelId,ModelMap model,HttpServletRequest request,HttpServletResponse rps,@RequestParam(defaultValue="1") int curPage) throws IOException{
Channel channel = channelService.getById(channelId);
String pageView = "";
Page page = new Page(curPage,20);
if(channel != null && channel.getList() != null && channel.getList().size() > 0){
//跳转3级栏目页面
pageView = "f/content3";
}else{
//查询内容页信息
int count1 = contentService.queryContentListCount(channelId, 0);
page.setCount(count1);
List<Content> contentList = contentService.queryContentList(page, channelId, 0);
if(count1 == 1){
Content content = contentList.get(0);
Customer customer = null;
if(content.getDoctorId() != null){
customer = customerService.getCustomerById(content.getDoctorId());
if(customer != null){
content.setJob(customer.getTitle());
content.setDepartmentName(customer.getDeptmentName());
}
}
contentService.updateContentPv(content.getId());
content.setPv(content.getPv() + 1);
model.addAttribute("content", content);
pageView = "f/content";
}else{
//列表
pageView = "f/contentList";
model.add
4000
Attribute("contentList", contentList);
model.addAttribute("page", page);
}
}
model.addAttribute("curId", channelId);
model.addAttribute("channel", channel);
List<Channel> channelList = channel.getParent().getList();
model.addAttribute("channelList", channelList);
model.addAttribute("childChannelList", channel.getList());
return new ModelAndView(pageView);
}jsp
<!--分页-->
<div class="feyestyle" style="float:none;">
<c:set var="pageStart" value="1"></c:set>
<c:if test="${page.curPage - 7 > 1 }">
<c:set var="pageStart" value="${page.curPage - 7 }"></c:set>
</c:if>
<c:set var="pageEnd" value="${page.pageNum }"></c:set>
<c:if test="${page.curPage + 7 < page.pageNum }">
<c:set var="pageEnd" value="${page.curPage + 7 }"></c:set>
</c:if>
<c:choose>
<c:when test="${page.curPage - 1 > 0 }">
<a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage-1}">< 上一页</a>
</c:when>
<c:otherwise>
<span class="feyedisabled">< 上一页</span>
</c:otherwise>
</c:choose>
<c:forEach var="incPage" begin="${pageStart }" end="${pageEnd }"
step="1">
<c:choose>
<c:when test="${page.curPage == incPage }">
<span class="feyecurrent"><c:out value="${incPage }"></c:out></span>
</c:when>
<c:otherwise>
<a href="<ph:root/>/list/${channel.id }.html?curPage=${incPage}"><c:out value="${incPage }"></c:out></a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.curPage + 1 <= page.pageNum }">
<a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage+1}">下一页 > </a>
</c:when>
<c:otherwise>
<span class="feyedisabled">< 下一页</span>
</c:otherwise>
</c:choose>
<span style="margin-left: 20px;">共计 <font class="feyefont-color">${page.count}</font> 条信息
</span>
</div>
<!--分页-->
相关文章推荐
- JSP/PHP基于Ajax的分页功能实现
- 开发阶段Jetty运行Jsp报错且响应空白
- more、less 和 most 的区别
- AJAX实现瀑布流触发分页与分页触发瀑布流的方法
- 十万条Access数据表分页的两个解决方法
- jsp简单实现页面之间共享信息的方法
- Apache Web让JSP“动”起来
- sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
- 高效的mysql分页方法及原理
- asp又一个分页的代码例子
- SqlServer 2000、2005分页存储过程整理第1/3页
- JQuery的Pager分页器实现代码
- ADO存取数据库时如何分页显示
- 透彻掌握ASP分页技术很详细的分析
- jQuery实现的简单分页示例
- Android ListView分页功能实现方法
- 在ASP.NET 2.0中操作数据之二十四:分页和排序报表数据
- ajax无刷新分页的简单实现