【问题标题】:Implementing pagination in MVC - Servlets + JSP在 MVC 中实现分页 - Servlet + JSP
【发布时间】:2014-01-03 16:08:40
【问题描述】:

[MVC、Servlet + JSP、JPA、MySQL] 我正在开发简单的博客应用程序。我正在使用 JPA 将实体映射到 MySQL 表。以下是相关实体的代码摘录:

实体Post

@NamedQueries({
    @NamedQuery(name = "getNewestPosts", query = "SELECT p FROM Post p ORDER BY p.date DESC"), // getting resultList ordered by date
    @NamedQuery(name = "getMostVisitedPosts", query = "SELECT p FROM Post p ORDER BY p.visitors DESC") // ordered by most visited
})
    @Entity
    @Table(name = "post")
    public class Post implements Serializable {

        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "post_id", unique = true, nullable = false)
        private Integer id; 

        @Column(name = "post_title", length=300, unique = false, nullable = false)
        private String title;

        @Column(name = "post_date", unique = false, nullable = false)
        private Date date;

        @Column(name = "post_summary", length=1000, unique = false, nullable = true)
        private String summary;

        @Column(name = "post_content", length=50000, unique = false, nullable = false)
        private String content;

        @Column(name = "post_visitors", unique = false, nullable = false)
        private Integer visitors;

        @OneToMany(cascade = { ALL }, fetch = LAZY, mappedBy = "post")
        private Set<Comment> comments = new HashSet<Comment>();
    ...

实体Comment

@Entity
@Table(name = "comment")
public class Comment implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "comment_id", unique = true, nullable = false)
    private Integer id; 

    @Column(name = "comment_title", length=300, unique = false, nullable = false)
    private String title;

    @Column(name = "comment_date", unique = false, nullable = false)
    private Date date;

    @Column(name = "comment_content", length=600, unique = false, nullable = false)
    private String content;

    @ManyToOne
    @JoinColumn (name = "post_id", referencedColumnName="post_id", nullable = false)
    private Post post; ...

博客主页应包含 10 篇最新帖子的摘要。所以,在PostDAO 对象中,我定义了下一个方法(返回数据库中按日期排序的所有帖子):

public List<Post> getNewestPosts(){

    Query q = em.createNamedQuery("getNewestPosts");
    List<Post> resultList = (List<Post>) q.getResultList();

    if (resultList.isEmpty())
        return null;
    else
        return resultList;
}

我想以某种简单的方式实现分页,可能会传递某些请求参数并使用jstl 读取jsp 中的数据(我还不熟悉jquery)。现在,如何在MVC 中实现分页?我需要附加哪些参数来请求?我应该如何在JSP 中实现页面导航链接(previous, page numbers, next)?

【问题讨论】:

    标签: java jsp servlets model-view-controller pagination


    【解决方案1】:

    我认为您可以在 namedQuery 上使用 setMaxResults 和 setFirstResult 方法。继续传递第一个结果作为要在页面上显示的记录数和页码的函数。

    如果您使用 Spring MVC,已经有办法做到这一点,您可以查看 PageListHolder api 文档。我没有使用过这个,但我偶然发现了 API。

    【讨论】:

    • 谢谢,已将setFirstResult(pageNumber)setMaxResults(postsPerPage) 添加到方法getNewestPosts(int pageNumber, postsPerPage)(为方法添加属性)
    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2018-03-22
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多