是否可以使用 JpaRepository<T, I> 在 jsp 中分页

我是 Spring Boot 新手,


我有一个问题,是否可以在JSP中对表格进行分页JpaRepository<T, I>,我已经在互联网上搜索了两天但没有找到。查询结果主要针对Thymeleaf,但我不想使用thymeleaf。我知道如何在JSP中使用分页Jdbctemplate,但为此,我必须手动编写查询和页数。我已经编写了 Spring Boot 和 JSP 代码。


员工存储库:


public interface EmployeeRepository extends JpaRepository<Emp, Integer> {}

员工控制器:


@Controller

public class EmployeeController {


    @Autowired

    private EmployeeRepository repo;


    @GetMapping("/")

    public String showPaginate(Model m, @RequestParam(defaultValue = "0") int page) {

        m.addAttribute("data", repo.findAll(new PageRequest(page, 4)));

        return "index";

    }

}

索引.jsp


<table border="2" width="70%" cellpadding="3"

        class="table">

        <thead class="thead-dark">

            <tr align="center">

                <th scope="col">Id</th>

                <th scope="col">Name</th>

                <th scope="col">Designation</th>

                <th scope="col">Edit</th>

                <th scope="col">Delete</th>

            </tr>

        </thead>

        <c:forEach var="emp" items="${list}">

            <tbody>

                <tr align="center">

                    <td>${emp.id}</td>

                    <td>${emp.name}</td>

                    <td>${emp.designation}</td>

                    <td><a href="editemp/${emp.id}" class="btn btn-outline-info">Edit</a></td>

                    <td><a href="deleteemp/${emp.id}" class="btn btn-outline-danger">Delete</a></td>

                </tr>

            </tbody>

        </c:forEach>

    </table>

<hr>

<!-- Pagination code will come here -->


狐的传说
浏览 127回答 3
3回答

拉风的咖菲猫

对的,这是可能的。但首先你必须创建一个PagingAndSortingRepository<T, I>存储库和控制器的接口,在你的情况下我没有看到一个,你必须添加页面、大小、元素、stc 的属性,这些属性由返回的PagingAndSortingRepository<T, I>所以你的 jsp 页面将了解页面等属性,您可以使用它${page}来显示页码。我指的是这个存储库。员工存储库:public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Integer> {}有PagingAndSortingRepository<T, I>一个findAll(page)根据页面数据传递对象的方法。员工道:@Servicepublic class EmpDao {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private EmpRepository repo;&nbsp; &nbsp; public Page<Emp> getPage(Pageable pageable){&nbsp; &nbsp; &nbsp; &nbsp; return repo.findAll(pageable);&nbsp; &nbsp; }如果您想要根据页面显示对象,那么您必须使用Page<T>接口,因为页面是对象列表的子列表。它允许获取有关它在包含的整个列表中的位置的信息。@Controllerpublic class EmpController {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private EmpDao dao;&nbsp; &nbsp; @RequestMapping("/viewemp")&nbsp; &nbsp; public String viewemp(Model m, Pageable pageable){&nbsp; &nbsp; &nbsp; &nbsp; Page<Emp> pages=dao.getPage(pageable);&nbsp; &nbsp; &nbsp; &nbsp; m.addAttribute("number", pages.getNumber());&nbsp; &nbsp; &nbsp; &nbsp; m.addAttribute("totalPages", pages.getTotalPages());&nbsp; &nbsp; &nbsp; &nbsp; m.addAttribute("totalElements", pages.getTotalElements());&nbsp; &nbsp; &nbsp; &nbsp; m.addAttribute("size", pages.getSize());&nbsp; &nbsp; &nbsp; &nbsp; m.addAttribute("data",pages.getContent());&nbsp; &nbsp; &nbsp; &nbsp; return "viewemp";&nbsp; &nbsp; }}是Pageable分页信息的抽象接口getNumber()- 返回当前切片的编号。始终为非负数。getTotalPages()- 返回总页数。getTotalElements()- 返回元素的总数。getSize()- 返回切片的大小。getContent()- 以列表形式返回页面内容。Jsp页面假设你已经存储了数据:只需将其写在您的表体中即可<tbody><tbody id="myTable">&nbsp; &nbsp; <c:choose>&nbsp; &nbsp; &nbsp; &nbsp; <c:when test="${data.size() > 0 }">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <c:forEach var="emp" items="${data}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr align="center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${emp.id}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${emp.name}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${emp.designation}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="editemp/${emp.id}" class="btn btn-outline-info">Edit</a></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><a href="deleteemp/${emp.id}" class="btn btn-outline-danger">Delete</a></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </c:forEach>&nbsp; &nbsp; &nbsp; &nbsp; </c:when>&nbsp; &nbsp; &nbsp; &nbsp; <c:otherwise>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr align="center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="5">No Users available</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </c:otherwise>&nbsp; &nbsp; </c:choose>&nbsp; &nbsp; <c:if test="${data.size() > 0 }">&nbsp; &nbsp; &nbsp; &nbsp; <div class="panel-footer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Showing ${number+1} of ${size+1} of ${totalElements}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="pagination">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <c:forEach begin="0" end="${totalPages-1}" var="page">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="viewemp?page=${page}&size=${size}" class="page-link">${page+1}</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </c:forEach>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </c:if></tbody>你就可以走了..希望能帮助到你..

慕姐8265434

通过这个例子可以确定一些方法。public class StudentRepository {&nbsp; &nbsp; @PersistenceContext&nbsp; &nbsp; private EntityManager entityManager;&nbsp; &nbsp; public List<StudentEntity> listAll()&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Query query = entityManager.createQuery("from StudentEntity");&nbsp; &nbsp; &nbsp; &nbsp; return query.getResultList();&nbsp; &nbsp; }}

猛跑小猪

试试这个:使用 PagingAndSortingRepository 扩展您的 EmployeeRepository。然后更改 findall() 方法类型 PageRequest findAll(PageRequest pagerequest)。public interface EmployeeRepository extends PagingAndSortingRepository<Emp, Integer> {&nbsp; &nbsp; PageRequest<Emp> findAll(PageRequest pagerequest)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java