在livejournal中显示消息树(java)
我正在使用SpringMVC+Hibernate编写一个论坛。Hibernate使用惰性初始化,为了使其工作,我使用OpenSessionInviewWinterCeptor,它可以工作。延迟初始化应该没有任何问题。 我正在尝试显示消息树,就像在livejournal回复中一样。 我只有id、parentId和文本列在livejournal中显示消息树(java),java,hibernate,spring,tree,message,Java,Hibernate,Spring,Tree,Message,我正在使用SpringMVC+Hibernate编写一个论坛。Hibernate使用惰性初始化,为了使其工作,我使用OpenSessionInviewWinterCeptor,它可以工作。延迟初始化应该没有任何问题。 我正在尝试显示消息树,就像在livejournal回复中一样。 我只有id、parentId和文本列 mysql> select * from posts; +----+----------+----------+----------+--------+-----------
mysql> select * from posts;
+----+----------+----------+----------+--------+------------+----------+
| id | threadId | authorId | parentId | text | created | modified |
+----+----------+----------+----------+--------+------------+----------+
| 1 | 5 | NULL | NULL | fda | 2011-11-24 | NULL |
| 2 | 5 | NULL | NULL | aff | 2011-11-24 | NULL |
| 3 | 5 | NULL | NULL | faee | 2011-11-24 | NULL |
| 13 | 6 | NULL | NULL | f52 | 2011-11-26 | NULL |
| 14 | 6 | NULL | 13 | c431 | 2011-11-26 | NULL |
| 15 | 6 | NULL | NULL | c31c13 | 2011-11-26 | NULL |
| 16 | 6 | NULL | 15 | n754 | 2011-11-26 | NULL |
| 23 | 4 | NULL | NULL | v52 | 2011-11-26 | NULL |
| 24 | 4 | NULL | 23 | v53 | 2011-11-26 | NULL |
| 25 | 4 | NULL | NULL | v423 | 2011-11-26 | NULL |
| 26 | 4 | NULL | 24 | v523 | 2011-11-26 | NULL |
| 27 | 4 | NULL | 23 | v253 | 2011-11-26 | NULL |
+----+----------+----------+----------+--------+------------+----------+
POJO类职位:
@Entity
@Table(name="posts")
public class Post{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
@JoinColumn(name="threadId")
private Thread thread;
@Column(name="authorId")
private Integer authorId;
@ManyToOne(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
@JoinColumn(name="parentId")
private Post parentPost;
@Column(name="text")
private String text;
@Column(name="created")
private Date created;
@Column(name="modified")
private Date modified;
....Many getters and setters....
}
我已经编写了一个JSP自定义标记:
<custom:tree postList="${posts}"/>
public class MessageTree extends SimpleTagSupport{
private List<Post> postList;
private StringBuffer output = new StringBuffer("<ul>");
public void setPostList(List<Post> postList){
this.postList = postList;
}
public void doTag()throws JspException,IOException{
retrieveOutput(null);
output.append("</ul>");
getJspContext().getOut().print(output.toString());
}
private void retrieveOutput(Integer parentId){
int j = 0;
while(j<postList.size()){
if(parentId==null && postList.get(j).getParentPost()==null){
output.append("<li>Id: "+postList.get(j).getId());
output.append("<ul>");
//retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}else{
if(postList.get(j).getParentPost().getId().equals(parentId)){ // !!!Here it throws java.lang.NullPointerException!!!!
output.append("<li>Id: "+postList.get(j).getId());
output.append("<ul>");
retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}
}
j++;
}
}
}
但是当我尝试时,例如
public void doTag()throws JspException,IOException{
output.append(postList.get(1).getParentPost().getId());
getJspContext().getOut().print(output.toString());
}
它工作,它检索23!
可能是我做得完全不对吗?你有什么建议
耶,我发现了错误!我把它改写了一点。问题是我没有在else子句中检查对象是否具有parrent post
private void retrieveOutput(Integer parentId){
int j = 0;
while(j<postList.size()){
if(parentId==null && postList.get(j).getParentPost()==null){
output.append("<li>Id: "+postList.get(j).getId()+"<br/>ParentId: 0<br/>Text: "+postList.get(j).getText()+"<br/>Posted: "+postList.get(j).getCreated()+"<br/><a href=\"/forums/deletePost/"+postList.get(j).getThread().getId()+"\">Delete this shit</a>");
output.append("<ul>");
retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}else{
if(postList.get(j).getParentPost()!=null && postList.get(j).getParentPost().getId().equals(parentId)){
output.append("<li>Id: "+postList.get(j).getId()+"<br/>ParentId: "+postList.get(j).getParentPost().getId()+"<br/>Text: "+postList.get(j).getText()+"<br/>Posted: "+postList.get(j).getCreated()+"<br/><a href=\"/forums/deletePost/"+postList.get(j).getThread().getId()+"\">Delete this shit</a>");
output.append("<ul>");
retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}
}
j++;
}
}
private void retrieveOutput(整数parentId){
int j=0;
虽然(j我认为您的问题在于延迟抓取,但您不能访问事务之外的延迟抓取属性。请检查此项
public void doTag()throws JspException,IOException{
output.append(postList.get(1).getParentPost().getId());
getJspContext().getOut().print(output.toString());
}
private void retrieveOutput(Integer parentId){
int j = 0;
while(j<postList.size()){
if(parentId==null && postList.get(j).getParentPost()==null){
output.append("<li>Id: "+postList.get(j).getId()+"<br/>ParentId: 0<br/>Text: "+postList.get(j).getText()+"<br/>Posted: "+postList.get(j).getCreated()+"<br/><a href=\"/forums/deletePost/"+postList.get(j).getThread().getId()+"\">Delete this shit</a>");
output.append("<ul>");
retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}else{
if(postList.get(j).getParentPost()!=null && postList.get(j).getParentPost().getId().equals(parentId)){
output.append("<li>Id: "+postList.get(j).getId()+"<br/>ParentId: "+postList.get(j).getParentPost().getId()+"<br/>Text: "+postList.get(j).getText()+"<br/>Posted: "+postList.get(j).getCreated()+"<br/><a href=\"/forums/deletePost/"+postList.get(j).getThread().getId()+"\">Delete this shit</a>");
output.append("<ul>");
retrieveOutput(postList.get(j).getId());
output.append("</ul></li>");
}
}
j++;
}
}