【发布时间】:2014-02-01 10:24:35
【问题描述】:
使用 JPA、MVC-JSP+Servlets、JSTL
实体和重要属性:
实体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_content", length=50000, unique = false, nullable = false)
private String content;
@Column(name = "post_visitors", unique = false, nullable = false)
private Integer visitors;
@ManyToOne
@JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
private User user;
@OneToMany(cascade = { ALL }, fetch = EAGER, mappedBy = "post")
@OrderBy("date DESC")
private Set<Comment> comments = new LinkedHashSet<Comment>();
...
}
实体User:
public class User implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer id;
@Column(name = "user_name", length=45, unique = false, nullable = false)
private String name;
@Column(name = "user_username", length=45, unique = false, nullable = false)
private String username;
@Column(name = "user_password", length=45, unique = false, nullable = false)
private String password;
@OneToMany(cascade = { ALL }, fetch = EAGER, mappedBy = "user")
private Set<Post> posts = new HashSet<Post>();
...
}
Servlets doGet 方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Post post = null;
int postId = Integer.parseInt(request.getParameter("postId"));
post = postDao.findById(postId);
// increase post "visitors" number
int visitors = post.getVisitors();
visitors ++;
post.setVisitors(visitors);
post = postDao.merge(post);
System.out.println("User id: " + post.getUser().getId());
System.out.println("User name: " + post.getUser().getName());
request.setAttribute("post", post);
RequestDispatcher dispatcher = request.getRequestDispatcher("post.jsp");
dispatcher.forward(request, response);
}
JSP(post.jsp)部分:
<section>
<header>${post.title}</header>
<div>
Posted by <strong>${post.user.name}</strong> on:
<fmt:formatDate value="${post.date}" type="date" />
comments(${fn:length(post.comments)}) views(${post.visitors})
</div>
<article>
<p>${post.content}</p>
</article>
</section>
现在,我遇到的问题是我没有在 jsp 中获得 ${post.user.name} 的值。 jsp 中的所有其他post 详细信息都存在,但${post.user.name} 不存在。这可能是什么原因?
编辑:
豆merge方法:
public T merge(T entity) {
entity = em.merge(entity);
return entity;
}
【问题讨论】:
-
用户可能为空。用户名可能为空。用户名可能是空字符串或空白字符串。检查它在控制器中的值。
-
@JBNizet 你很聪明。用户名为
null。但是用户ID在那里。请参阅我更新的 servlet doGet 方法内容。我收到User Id: 4和User name: null。并且用户名在数据库中不是null。我不确定我是否正确使用post = postDao.merge(post);。你能看看 servlets doGet 方法中的代码是否正确吗? -
我没有发现任何问题。
-
@JBNizet 我绝对没有正确设置和更新帖子
visitors字段。我已经评论了那部分代码,现在我正在获取用户详细信息(但没有更新visitors属性)。 -
@JBNizet 不知道为什么,但是从
post = postDao.merge(post);中删除post =会得到正确的post对象。 (您可以在问题更新中看到实体merge)如果您知道原因,请将其发布为答案。谢谢。
标签: java jsp servlets jpa jstl