【发布时间】: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