服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java中对list分页并显示数据到页面实例代码

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

2021-04-04 14:31你的承諾早已氾黃 Java教程

这篇文章主要介绍了java中对list分页并显示数据到页面实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

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

page的model类:

java" id="highlighter_493423">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
  }
}

控制层:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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页面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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分页并显示数据到页面实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/u012843873/article/details/79216654

延伸 · 阅读

精彩推荐