欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中对list分页并显示数据到页面实例代码

程序员文章站 2024-02-06 13:10:52
有时候需要根据条件查询得出的数据较多,需要分页显示到页面上。这时点击下一页就不方便每次带查询条件在数据库中分页。可以在list中进行分页。 page的model类:...

有时候需要根据条件查询得出的数据较多,需要分页显示到页面上。这时点击下一页就不方便每次带查询条件在数据库中分页。可以在list中进行分页。

page的model类:

public class page { 
  private integer currentpage;//当前页 
  private int pagesize;//每页显示记录条数 
  private int totalpage;//总页数 
  private list<?> datalist;//每页显示的数据 
  private int star;//开始数据 
 
  public integer getcurrentpage() { 
    return currentpage; 
  } 
  public void setcurrentpage(integer currentpage) { 
    this.currentpage = currentpage; 
  } 
  public int getpagesize() { 
    return pagesize; 
  } 
  public void setpagesize(int pagesize) { 
    this.pagesize = pagesize; 
  } 
  public int gettotalpage() { 
    return totalpage; 
  } 
  public void settotalpage(int totalpage) { 
    this.totalpage = totalpage; 
  } 
  public list<?> getdatalist() { 
    return datalist; 
  } 
  public void setdatalist(list<?> datalist) { 
    this.datalist = datalist; 
  } 
  public int getstar() { 
    return star; 
  } 
  public void setstar(int star) { 
    this.star = star; 
  } 
} 

控制层:

@requestmapping("fenye.do") 
  public string fen(page page,httpservletrequest request){ 
 
    try { 
      //查询出的list数据 
      list<data> showdata=loginservice.selectyichall(); 
      //刚开始的页面为第一页 
      if (page.getcurrentpage() == null){ 
        page.setcurrentpage(1); 
      } else { 
        page.setcurrentpage(page.getcurrentpage()); 
      } 
      //设置每页数据为十条 
      page.setpagesize(10); 
      //每页的开始数 
      page.setstar((page.getcurrentpage() - 1) * page.getpagesize()); 
      //list的大小 
      int count = showdata.size(); 
      //设置总页数 
      page.settotalpage(count % 10 == 0 ? count / 10 : count / 10 + 1); 
      //对list进行截取 
      page.setdatalist(showdata.sublist(page.getstar(),count-page.getstar()>page.getpagesize()?page.getstar()+page.getpagesize():count)); 
      //设置作用域 
      request.setattribute("paging", page);         
      return "add/show.jsp"; 
 
    } catch (exception e) { 
      // todo auto-generated catch block    
      e.printstacktrace(); 
      return "mistake.jsp"; 
    } 
  } 

jsp页面:

<script type="text/javascript" language="javascript"> 
 function gopage(page){ 
 location.href="fenye.do?currentpage=" rel="external nofollow" +page; 
 } 
</script> 
 
 
<div> 
 <table width="100%" border="0" align="center" cellspacing="1" > 
 <c:foreach items="${paging.datalist }" var="upl">    
   <tr> 
    <td align="center">${upl.name1 }</td> 
    <td align="center">${upl.name2 }</td> 
    <td align="center">${upl.name3 }</td>    
   </tr> 
   </c:foreach>   
 </table> 
</div> 
<div style="text-align:center; margin-top:10px;color: white;" id="venderfen"> 
 <c:if test="${paging.totalpage > 0}"> 
  当前第 ${paging.currentpage } 页/共  ${paging.totalpage} 页 
 </c:if> 
 <c:if test="${paging.totalpage > 1}"> 
   <c:choose> 
     <c:when test="${paging.currentpage==2 && paging.totalpage==2}"> 
      <a onclick="gopage(1)">首页</a> 
      <a onclick="gopage(${paging.currentpage-1})">上一页</a> 
     </c:when> 
     <c:when test="${paging.currentpage==1}"> 
      <a onclick="gopage(${paging.currentpage+1})">下一页</a> 
      <a onclick="gopage(${paging.totalpage})">末页</a> 
     </c:when> 
     <c:when test="${paging.currentpage==paging.totalpage}"> 
      <a onclick="gopage(1)">首页</a> 
      <a onclick="gopage(${paging.currentpage-1})">上一页</a> 
     </c:when> 
     <c:otherwise> 
      <a onclick="gopage(1)">首页</a> 
      <a onclick="gopage(${paging.currentpage-1})">上一页</a> 
      <a onclick="gopage(${paging.currentpage+1})">下一页</a> 
      <a onclick="gopage(${paging.totalpage})">末页</a> 
     </c:otherwise> 
  </c:choose> 
  </c:if> 
 共 ${paging.total} 条 
</div>  
stringbuilder sb = new stringbuilder(); 
sb.append("select distinct(m.time) "); 
sb.append(" from tbl_monitor m left join tbl_variable v on m.variable = v.id "); 
sb.append(" left join tbl_variable_group g on v.variable_group = g.id "); 
       
sb.append(" where v.active = true and g.active=true "); 
//sb.append(" and m.variable in " + variableids); 
sb.append(" and exists ( select vtemp.id from tbl_variable vtemp where vtemp.selected=true and vtemp.id=m.variable)"); 
sb.append(" and m.time between " + begintime + " and " + endtime ); 
sb.append(" order by m.time asc"); 
query query = em.createnativequery(sb.tostring()); 
//满足搜索条件的所有的时间的条数 
int total = query.getresultlist().size(); 
tableview.settotal(total); 
tableview.settotalpage(total % page.getpagesize() == 0 ? total / page.getpagesize() : (total / page.getpagesize()) + 1); 
query.setfirstresult(from); 
query.setmaxresults(offset); 
timelist = query.getresultlist(); 

总结

以上就是本文关于java中对list分页并显示数据到页面实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!