Th:每个迭代来自hashSet的特定变量Thymeleaf Spring MVC

时间:2016-02-17 05:05:20

标签: spring-mvc thymeleaf

所以我使用了Thymeleaf和spring MVC,我有一个Course对象,TreeSetPostsposts的集合类似于reddit我试图在我创建的用户主页上代表一组Posts。我希望帖子按时间顺序排列,以新闻源的方式排序,我已经拥有了用户的{{ 1}}他们在模型上订阅了。

我的问题是,当我尝试遍历每个帖子时,我只能提取帖子的ID,看起来像courses,当我尝试显示[com.quizbanks.domain.Post@26]时,这是帖子标题的变量我总是得到一个弹簧处理器错误。

所以这就是我的百万美元代码的样子

title

然后我尝试了这个,但这给了我弹簧处理器错误

<table class="table table-bordered">
    <tr th:each="course : ${courses}" th:object="${course}">
        <td><span th:text="${course.posts}"></span></td>
    </tr>
</table>

所以我不确定该怎么做,如果有人能够看到我的问题并帮助我,那将是非常好的,提前谢谢。

此外,如果您想查看我的控制器代码或其他任何内容,请告诉我,我会发布它。

更新

用户类

<table class="table table-bordered">
    <tr th:each="course : ${courses}" th:object="${course}">
        <td><span th:text="${course.posts.title}"></span></td>
    </tr>
</table>

发布课程

    package com.quizbanks.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.quizbanks.security.Authorities;
import com.quizbanks.validators.ValidEmail;

@Entity
@Table(name="users")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class User
{
  private Long id;
  @ValidEmail
  @NotNull
  @NotEmpty
  private String email;
  private String username;
  private String password;
  private University university;
  private Set<Authorities> authorities = new HashSet<>();
  private Set<Course> courses = new HashSet<>();
  private Set<Post> posts = new HashSet<>();
  private Set<Comment> comments = new HashSet<>();
  private Set<StudySet> studySet = new HashSet<>();

  private Set<Course> myCourses = new HashSet<Course>();


  public User ()
  {

  }

  public User(User user)
  {
      this.id = user.getId();
      this.email = user.getEmail();
      this.username = user.getUsername();
      this.password = user.getPassword();
      this.university = user.getUniversity();
      this.authorities = user.getAuthorities();
      this.courses = user.getCourses();
      this.posts = user.getPosts();
      this.comments = user.getComments();
      this.studySet = user.getStudySet();
      this.myCourses = user.getMyCourses();
  }



  @Id
  @GeneratedValue
  public Long getId()
  {
    return id;
  }
  public void setId(Long id)
  {
    this.id = id;
  }

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="user", orphanRemoval=true)
  public Set<Course> getCourses()
  {
    return courses;
  }

  public void setCourses(Set<Course> courses)
  {
    this.courses = courses;
  }
  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="user", orphanRemoval=true)
  public Set<Post> getPosts() {
    return posts;
  }

  public void setPosts(Set<Post> posts) {
    this.posts = posts;
  }

  public String getEmail()
  {
    return email;
  }
  public void setEmail(String email)
  {
    this.email = email;
  }
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword()
  {
    return password;
  }
  public void setPassword(String password)
  {
    this.password = password;
  }
  @ManyToOne
  public University getUniversity() {
    return university;
  }

  public void setUniversity(University university) {
    this.university = university;
  }
  @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="user")
  @JsonManagedReference
  @JsonIgnoreProperties(allowGetters=true, value = "user" )
  public Set<Comment> getComments() {
    return comments;
  }
  public void setComments(Set<Comment> comments) {
    this.comments = comments;
  }
  @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="user")
  public Set<Authorities> getAuthorities()
  {
    return authorities;
  }
  public void setAuthorities(Set<Authorities> authorities)
  {
    this.authorities = authorities;
  }
  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="user", orphanRemoval=true)
  public Set<StudySet> getStudySet() {
    return studySet;
  } 
  public void setStudySet(Set<StudySet> studySet) {
    this.studySet = studySet;
  }
  @ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
  @JoinTable(name="user_myCourses")
  public Set<Course> getMyCourses()
  {
    return myCourses;
  }
  public void setMyCourses(Set<Course> myCourses)
  {
    this.myCourses = myCourses;
  }
}

发布         包com.quizbanks.domain;

    package com.quizbanks.domain;

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Course
{
  private Long id;
  @Size(min=1, max=50)
  private String name;
  @Size(min=1, max=50)
  private String professor;
  @Size(min=1, max=50)
  private University university;
  private Set<Post> posts = new TreeSet<>();

  private User user;

  @Id
  @GeneratedValue
  public Long getId()
  {
    return id;
  }

  public void setId(Long id)
  {
    this.id = id;
  }

  @ManyToOne
  public User getUser()
  {
    return user;
  }

  public void setUser(User user)
  {
    this.user = user;
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String getProfessor() 
  {
    return professor;
  }

  public void setProfessor(String professor) 
  {
    this.professor = professor;
  }
  @ManyToOne
  public University getUniversity() {
    return university;
  }

  public void setUniversity(University university) {
    this.university = university;
  }

  @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="course")
  public Set<Post> getPosts()
  {
    return posts;
  }

  public void setPosts(Set<Post> posts)
  {
    this.posts = posts;
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Course other = (Course) obj;
    if (id == null)
    {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    return true;
  }


}

控制器

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Post
{
  private Long id;
  @Size(min=1, max=140)
  @NotNull
  private String title;
  @Size(min=1, max=1000)
  private String content;
  private Course course;
  private Set<Comment> comments = new TreeSet<>();

  private User user;

  @Id
  @GeneratedValue
  public Long getId()
  {
    return id;
  }
  public void setId(Long id)
  {
    this.id = id;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }

  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
  @ManyToOne
  public Course getCourse()
  {
    return course;
  }
  public void setCourse(Course course)
  {
    this.course = course;
  }
  @ManyToOne
  public User getUser()
  {
    return user;
  }

  public void setUser(User user)
  {
    this.user = user;
  }

  @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="post")
  public Set<Comment> getComments() {
        return comments;
  }
  public void setComments(Set<Comment> comments) {
        this.comments = comments;
  }
  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
  }
  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Post other = (Post) obj;
    if (id == null)
    {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    return true;
  }


}

1 个答案:

答案 0 :(得分:2)

${courses}是一个内置posts的课程列表。 似乎问题是postsTreeSet,所以你应该迭代该集合中的每个元素以获得实际的Posts,然后获得标题。

<强>更新

List<Course> courses = courseRepo.findByUser(user);这里为您的用户提供所有课程。因此,下一步是迭代该列表以获取所有帖子:

Set<Post> posts = new TreeSet<>();
for (Course course : courses) {
    posts.addAll(course.getPosts);
}
model.addAttribute("posts", posts);