-2

我正在尝试使用 j2ee 构建一个网络应用程序。我正在尝试在表格中显示项目列表,如果返回的项目超过 25 个,则将列表分成多个页面。

这是一个例子:

在此处输入图像描述

请问我该如何进行?提前致谢。

4

1 回答 1

0

您可以使用 spring-data 来执行此操作。你的jsp代码显示记录:

<c:forEach var="user" items="${page.content}">
   <p>${user.name}</p>
</c:forEach>

显示分页控件的 jsp 代码(如附图所示):

<c:if test="${page.totalPages > 1}">
    <c:set var="THRESHOLD" value="3" />
    <div class="pagination">
        <ul>
            <c:choose>
                <c:when test="${page.number != 0}">
                    <li><a href="${home}/${targetUrl}?page=${page.number-1}${queryParams}">&larr;</a></li> 
                    <li><a href="${home}/${targetUrl}?page=0${queryParams}">1</a></li>
                    <c:choose>
                        <c:when test="${page.number <  (THRESHOLD + 3)}">
                            <c:forEach var="i" begin="1" end="${page.number}">
                                <c:choose>
                                    <c:when test="${i == page.number}"> 
                                        <li class="current"><span>${i+1}</span></li>
                                    </c:when>
                                    <c:otherwise>
                                        <li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </c:when>
                        <c:otherwise>
                            <li><span>...</span></li>
                            <c:forEach var="i" begin="${page.number - THRESHOLD}" end="${page.number}">
                                <c:choose>
                                    <c:when test="${i == page.number}"> 
                                        <li class="current"><span>${i+1}</span></li>
                                    </c:when>
                                    <c:otherwise>
                                        <li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </c:otherwise>
                    </c:choose>
                </c:when>
                <c:otherwise>
                    <li class="current"><span>1</span></li>
                </c:otherwise>
            </c:choose>
            <c:if test="${page.number != page.totalPages-1}">
                <c:choose>
                    <c:when test="${(page.totalPages - page.number) <  (THRESHOLD + 3)}">
                        <c:forEach var="i" begin="${page.number+1}" end="${page.totalPages-1}">
                            <li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <c:forEach var="i" begin="${page.number+1}" end="${page.number + THRESHOLD}">
                            <li><a href="${home}/${targetUrl}?page=${i}${queryParams}">${i+1}</a></li>
                        </c:forEach>
                        <li><span>...</span></li>
                        <li><a href="${home}/${targetUrl}?page=${page.totalPages-1}${queryParams}">${page.totalPages}</a></li>
                    </c:otherwise>
                </c:choose>
                <li><a href="${home}/${targetUrl}?page=${page.number+1}${queryParams}">&rarr;</a></li>
            </c:if>
        </ul>
    </div>
</c:if>

只需将 ${targetUrl} 和 ${queryParams} 替换为您的控制器 url 和所需的参数

你的控制器:

    @Autowired
    private UserDAO userDao;

    @RequestMapping(value = "/your-controller-url", method = RequestMethod.GET)
    public String adminUsersList(@RequestParam(required = false) Integer page, Model model) {
        if(page == null) {
            page = 0;
        }
        PageRequest pageReq = new PageRequest(page, "15", new Sort(new Order(Direction.ASC, "id"))); // 15 - is number of items on page
        Page<User> usersPage = userDao.findAll(pageReq);
        model.addAttribute("page", usersPage);
        return "your-view-name";
    }

和用户道:

public interface UserDAO extends JpaRepository<User, Long> {

}

为了更好地理解发生了什么,请阅读这个很棒的教程:http ://www.petrikainulainen.net/spring-data-jpa-tutorial/

于 2013-08-15T13:29:24.500 回答