Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用spring在jstl中显示相关对象?_Java_Spring_Jsp_Jstl - Fatal编程技术网

Java 如何使用spring在jstl中显示相关对象?

Java 如何使用spring在jstl中显示相关对象?,java,spring,jsp,jstl,Java,Spring,Jsp,Jstl,我有3个对象:用户、评论和状态更新(新闻)。这是用户 @Entity @Table(name = "users") @PasswordMatch(message = "{register.repeatpassword.mismatch}") public class SiteUser { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; @Column(name

我有3个对象:用户、评论和状态更新(新闻)。这是用户

@Entity
@Table(name = "users")
@PasswordMatch(message = "{register.repeatpassword.mismatch}")
public class SiteUser {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Column(name = "email", unique = true)
@Email(message = "{register.email.invalid}")
@NotBlank(message = "{register.email.invalid}")
private String email;

@Transient
@Size(min = 5, max = 15, message = "{register.password.size}")
private String plainPassword;

@Column(name = "password", length = 60)
private String password;

@Column(name = "enabled")
private Boolean enabled = false;

@NotNull
@Column(name = "firstname", length = 20)
@Size(min = 2, max = 20, message = "{register.firstname.size}")
private String firstname;

@NotNull
@Column(name = "surname", length = 25)
@Size(min = 2, max = 25, message = "{register.surname.size}")
private String surname;

@Transient
private String repeatPassword;

@Column(name = "role", length = 20)
private String role;

public SiteUser() {

}
这里是状态更新(你可以称之为新闻或文章)。有一个站点用户,就是创建该文章的用户

@Entity
@Table(name = "status_update")
public class StatusUpdate {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Size(min=5, max=255, message="{addstatus.title.size}")
@Column(name = "title")
private String title;

@Size(min=5, max=5000, message="{addstatus.text.size}")
@Column(name = "text")
private String text;

@Column(name = "added")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss")
private Date added;

@OneToOne(targetEntity = SiteUser.class)
@JoinColumn(name="user_id")
private SiteUser siteUser;

@PrePersist
protected void onCreate() {
    if (added == null) {
        added = new Date();
    }
}

public StatusUpdate() {

}
以及任何注册用户都可以做的评论,对吗?正如您将注意到的,注释没有用户对象来避免循环引用

@Entity
@Table(name = "comments")
public class Comment {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
@JoinColumn(name = "statusupdateid")
private StatusUpdate statusUpdate;

@Column(name = "commenttext")
private String commenttext;

@Column(name = "commentdate")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private Date commentdate;

@Column(name = "userid")
private Long userid;


public Comment() {
}
现在,我想在JSP中显示一篇文章,其中包含所有相关的注释,并且每个注释都属于不同的用户。我可以使用HashMap来关联用户和他们的评论吗?我不知道该怎么做

@RequestMapping(value ="/viewonestatus/{id}")
public ModelAndView viewOneStatus(@PathVariable("id") Long id) {

    StatusUpdate status = statusUpdateService.get(id);

    int countComments = commentService.countStatusComments(status);

    List<Comment> comments = commentService.readAllComments(status);

    ModelAndView modelAndView = new ModelAndView();

    for (Comment comment: comments){

        SiteUser user = userService.get(comment.getUserid());

        modelAndView.getModel().put("user", user);

    }

    modelAndView.getModel().put("commentscounter", countComments);
    modelAndView.getModel().put("status", status);
    modelAndView.getModel().put("comments", comments); //!!

    modelAndView.setViewName("app.viewonestatus");

    return modelAndView;
}
@RequestMapping(value=“/viewonestatus/{id}”)
公共模型和视图viewOneStatus(@PathVariable(“id”)长id){
StatusUpdate status=statusUpdateService.get(id);
int countComments=commentService.countStatusComments(状态);
列表注释=commentService.readAllComments(状态);
ModelAndView ModelAndView=新建ModelAndView();
对于(注释:注释){
SiteUser=userService.get(comment.getUserid());
modelAndView.getModel().put(“用户”,user);
}
modelAndView.getModel().put(“CommentsCenter”,countComments);
modelAndView.getModel().put(“status”,status);
modelAndView.getModel().put(“comments”,comments);/!!
modelAndView.setViewName(“app.viewonestatus”);
返回模型和视图;
}
正如您所期望的,当我的JSP只显示一个用户(最后一个用户)进行所有注释时,但我无法将所有注释与相应的用户关联起来

<table class="table table-hover">
    <c:forEach var="comment" items="${comments}">
    <tr>
        <td>

    <div class="col-sm-2 sm-margin-bottom-40">
        <img class="img-responsive profile-img margin-bottom-20" id="profilePhotoImage" src="/profilephoto/${comment.userid}" />
    </div>
                                                <h4> 
            ${user.firstname} ${user.surname} 
            <span> 
              <!--  <span>${counterUserMap[comment.key]}</span> -->
            5 hours ago / <a href="#">Reply</a>
            </span>
        </h4>
        <p>
            <fmt:formatDate pattern="EEEE d MMMM y 'at' H:mm:ss" value="${comment.commentdate}" />
        </p>
        <p>${comment.commenttext}</p>
    </td>
</tr>
</c:forEach>

${user.firstname}${user.lasname}
5小时前/

${comment.commenttext}


我不想使用JSON。我在考虑一个匿名课堂,里面有所有的东西。我愿意接受你的想法。谢谢。

Shokulei的答案是解决方案:


因为您有用户ID,所以可以使用@ManyToOne注释链接它。这将是最理想的方式。但是如果你真的不想链接它们,那么你可以创建一个新的@Transient SiteUser;注释类中的属性。然后在for循环中,可以使用comment.setSiteUser(user);而不是modelAndView.getModel().put(“user”,user);。希望这会有所帮助


感谢Shokulei

您的用户总是显示最后一个是因为代码[modelAndView.getModel().put(“user”,user);]在for循环中。它将始终使用最后一条注释中的用户并将其发送到JSP。不使用[${comment.user.firstname}${comment.user.namite}的原因是什么。这将为您提供发表评论的用户。首先,感谢您的帮助……因为该用户不是评论对象的一部分。它只有用户id。我这样做是为了避免堆栈溢出问题。(请参见此问题:)由于您拥有用户ID,因此可以使用@ManyToOne注释链接它。这将是最理想的方式。但是如果你真的不想链接它们,那么你可以创建一个新的
@Transient SiteUser属性。然后在for循环中,可以使用
comment.setSiteUser(user)
而不是
modelAndView.getModel().put(“user”,user)。希望这会有所帮助。我知道,但我有很多问题与堆栈溢出和循环引用,我试图避免。也许我错了,我应该创建@ManyToOne注释,但是ToString方法带来了很多我不知道如何避免的问题。你看到我的另一个问题了吗?[链接]临时用户能否避免StackOverflow问题?再次阅读文章后,您似乎应该能够从Comment类中的statusUpdate属性中获取用户信息。能否尝试使用
${comment.statusUpdate.siteUser.firstname}