分页 Thymeleaf 3.0 Spring MVC

Mik*_*ike 0 pagination spring-mvc thymeleaf

我想使用 Spring PagingAndSortingRepository DAO 找到 Thymeleaf 分页的完整解决方案。我得到了一个部分解决方案,但我无法得到最后一个。我认为作为代码片段对其他人来说会很有趣,所以我会要求整个事情。我在网上找不到对我来说有点奇怪的解决方案(因为我认为这可能是一个非常普遍的问题)。

最终的解决方案应该表现得像谷歌的分页:只有在有意义的情况下才在两边都有箭头;最多 10 个数字(例如),当您单击向右箭头时,它应该从 1..10 --> 11..20 等移动;当你点击 10 时移动到 5..15。就像谷歌一样,你知道。大小控制每个页面中的项目数,而不是分页栏中的块或链接数。

我在 Spring 中有一个 DAO 存储库,它扩展了 PagingAndSortingRepository ...

包 music.bolo.domain.repository;

导入 org.springframework.data.repository.PagingAndSortingRepository; 导入 org.springframework.stereotype.Repository;

导入 music.bolo.domain.entity.Announcement;

@Repository 公共接口 NoticeDao 扩展 PagingAndSortingRepository {

公告 findFirstByOrderByDateDesc(); }

所以我的服务可以发出请求,每个页面都会得到 totalPageNumbers(http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Page.html)。 ..

私有最终静态 int PAGESIZE = 2;

.. .. @Autowired 注释...

  public Page<Announcement> readAnnouncementPage (int pageNumber){
          PageRequest request = new PageRequest(pageNumber-1, PAGESIZE, Sort.Direction.DESC, "date");
          return announcementDao.findAll(request);    }
Run Code Online (Sandbox Code Playgroud)

我的控制器使用数据将所有信息发送到 Thymeleaf

  @RequestMapping(value = "/viewannouncements", method = RequestMethod.GET)   
  ModelAndView viewAnnouncements(ModelAndView modelAndView, @RequestParam(name = "p", defaultValue = "1") int  pageNumber) {

  Page<Announcement> page =  announcementService.readAnnouncementPage(pageNumber);

  modelAndView.getModel().put("page2th", page);

  modelAndView.setViewName("viewannouncements");

  return modelAndView;    }
Run Code Online (Sandbox Code Playgroud)

我的解决方案是部分的,它一直显示所有页面,没有箭头控制(实际上没用),也没有任何其他功能,但这是我可以让它在没有错误的情况下工作的最多。

  public Page<Announcement> readAnnouncementPage (int pageNumber){
          PageRequest request = new PageRequest(pageNumber-1, PAGESIZE, Sort.Direction.DESC, "date");
          return announcementDao.findAll(request);    }
Run Code Online (Sandbox Code Playgroud)

kar*_*p90 5

这是使用SpringBootThymeleaf模板进行分页的示例,您可以尝试一下。
这是不言自明的。
您可以在下面找到 GitHub 存储库的链接 - https://github.com/karelp90/control_asp

看这些截图

看这些截图