亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

狐的傳說 2023-07-19 16:24:55
我是 Spring Boot 新手,我有一個問題,是否可以在JSP中對表格進行分頁JpaRepository<T, I>,我已經在互聯網上搜索了兩天但沒有找到。查詢結果主要針對Thymeleaf,但我不想使用thymeleaf。我知道如何在JSP中使用分頁Jdbctemplate,但為此,我必須手動編寫查詢和頁數。我已經編寫了 Spring Boot 和 JSP 代碼。員工存儲庫:public interface EmployeeRepository extends JpaRepository<Emp, Integer> {}員工控制器:@Controllerpublic 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 -->
查看完整描述

3 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

對的,這是可能的。但首先你必須創建一個PagingAndSortingRepository<T, I>存儲庫和控制器的接口,在你的情況下我沒有看到一個,你必須添加頁面、大小、元素、stc 的屬性,這些屬性由返回的PagingAndSortingRepository<T, I>所以你的 jsp 頁面將了解頁面等屬性,您可以使用它${page}來顯示頁碼。我指的是這個存儲庫。


員工存儲庫:


public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Integer> {}

有PagingAndSortingRepository<T, I>一個findAll(page)根據頁面數據傳遞對象的方法。


員工道:


@Service

public class EmpDao {


    @Autowired

    private EmpRepository repo;


    public Page<Emp> getPage(Pageable pageable){

        return repo.findAll(pageable);

    }

如果您想要根據頁面顯示對象,那么您必須使用Page<T>接口,因為頁面是對象列表的子列表。它允許獲取有關它在包含的整個列表中的位置的信息。


@Controller

public class EmpController {


    @Autowired

    private EmpDao dao;


    @RequestMapping("/viewemp")

    public String viewemp(Model m, Pageable pageable){

        Page<Emp> pages=dao.getPage(pageable);

        m.addAttribute("number", pages.getNumber());

        m.addAttribute("totalPages", pages.getTotalPages());

        m.addAttribute("totalElements", pages.getTotalElements());

        m.addAttribute("size", pages.getSize());

        m.addAttribute("data",pages.getContent());

        return "viewemp";

    }

}

Pageable分頁信息的抽象接口

  1. getNumber()- 返回當前切片的編號。始終為非負數。

  2. getTotalPages()- 返回總頁數。

  3. getTotalElements()- 返回元素的總數。

  4. getSize()- 返回切片的大小。

  5. getContent()- 以列表形式返回頁面內容。

Jsp頁面假設你已經存儲了數據:

只需將其寫在您的表體中即可<tbody>

<tbody id="myTable">

    <c:choose>

        <c:when test="${data.size() > 0 }">

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

                <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>

            </c:forEach>

        </c:when>

        <c:otherwise>

            <tr align="center">

                <td colspan="5">No Users available</td>

            </tr>

        </c:otherwise>

    </c:choose>


    <c:if test="${data.size() > 0 }">

        <div class="panel-footer">

            Showing ${number+1} of ${size+1} of ${totalElements}

            <ul class="pagination">

                <c:forEach begin="0" end="${totalPages-1}" var="page">

                    <li class="page-item">

                        <a href="viewemp?page=${page}&size=${size}" class="page-link">${page+1}</a>

                    </li>

                </c:forEach>

            </ul>

        </div>

    </c:if>


</tbody>

你就可以走了..


希望能幫助到你..


查看完整回答
反對 回復 2023-07-19
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

通過這個例子可以確定一些方法。


public class StudentRepository {


    @PersistenceContext

    private EntityManager entityManager;


    public List<StudentEntity> listAll()  {

        Query query = entityManager.createQuery("from StudentEntity");

        return query.getResultList();

    }

}


查看完整回答
反對 回復 2023-07-19
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

試試這個:使用 PagingAndSortingRepository 擴展您的 EmployeeRepository。然后更改 findall() 方法類型 PageRequest findAll(PageRequest pagerequest)。


public interface EmployeeRepository extends PagingAndSortingRepository<Emp, Integer> {

    PageRequest<Emp> findAll(PageRequest pagerequest)

}


查看完整回答
反對 回復 2023-07-19
  • 3 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號