使用thymeleaf将列表<t>分配给javascript中的变量

时间:2017-06-07 10:27:42

标签: javascript java spring-boot thymeleaf

我正在尝试在javascript中获取变量中的对象列表,添加到控制器中的模型中。作为测试,我传递了一个字符串列表,它工作正常,但它不适用于对象列表。有人能帮帮我吗?我一直在寻找解决方案但我找不到任何东西。

这是控制器

@GetMapping(value= "/view_dates")
public String viewDates(Model model){
    List<Division> list =divisionService.getActiveDates();
    model.addAttribute("dates", list);
    return  "course_view_dates";
}

这是javascript代码

   <script th:inline = "javascript">
        /*<![CDATA[*/
            var examDates = /*[[${dates}]]*/ null;
            for (i=0; i< examDates.length; i++){
                alert(examDates[i].name);
            }
        /*]]>*/
    </script>

这是获取更多信息的实体

@Entity
@Table(name="division")
public class Division implements Serializable{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", unique = true, nullable = false)  
  private Integer id;
  @OneToMany(mappedBy = "division")
  private List<Kind> kinds;
  @JsonIgnore
  @OneToMany(mappedBy = "division", cascade = CascadeType.ALL)
  private List<Sample> samples;
  @ManyToMany(mappedBy = "divisions", cascade = CascadeType.ALL)
  private List<Candidate> candidates;
  @Column(name = "name", nullable = false)
  private String name;
  @Column(name = "description", nullable = true)
  private String description;
  @Column(name = "course_days", nullable = true)
  private Integer courseDays;
  @Temporal(TemporalType.DATE)
  @DateTimeFormat(pattern= "dd/MM/yyy")
  @Column(name = "exam_date", nullable = true)
  private Date examDate;
  @Column(name = "active", nullable = true)
  private Boolean active;

  public Division() {
    this.name = "";
    this.description = "";
    this.courseDays = 0;
    this.examDate = null;
    this.active = false;
  }

public Division(String name, String description, Integer courseDays, Date examDate, Boolean dateActive){
    this.name = name;
    this.description = description;
    this.courseDays = courseDays;
    this.examDate = examDate;
    this.active = dateActive;
}

public Integer getId() {
    return id;
}

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

public String getName() {
    return name;
}

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

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Integer getCourseDays() {
    return courseDays;
}

public void setCourseDays(Integer courseDays) {
    this.courseDays = courseDays;
}

public List<Sample> getSamples() {
    return this.samples;
}

public void setSamples(List<Sample> samples) {
    this.samples = samples;
}

public Date getExamDate() {
    return examDate;
}

public void setExamDate(Date ExamDate) {
    this.examDate = ExamDate;
}

public Boolean getActive() {
    return active;
}

public void setActive(Boolean active) {
    this.active = active;
}

public List<Kind> getKinds() {
    return this.kinds;
}

public void setKinds(List<Kind> varieties) {
    this.kinds = varieties;
}

public List<Candidate> getCandidates() {
    return this.candidates;
}

public void setCandidates(List<Candidate> candidates) {
    this.candidates = candidates;
} 

它产生两次对数据库的调用,然后是ERROR

Hibernate: select candidates0_.division_id as division2_1_0_,             candidates0_.candidate_id as candidat1_1_0_, candidate1_.id as id1_0_1_, candidate1_.name as name2_0_1_ from ostscourses.candidate_division candidates0_ inner join ostscourses.candidate candidate1_ on candidates0_.candidate_id=candidate1_.id where candidates0_.division_id=?
Hibernate: select divisions0_.candidate_id as candidat1_1_0_, divisions0_.division_id as division2_1_0_, division1_.id as id1_2_1_, division1_.active as active2_2_1_, division1_.course_days as course_d3_2_1_, division1_.description as descript4_2_1_, division1_.exam_date as exam_dat5_2_1_, division1_.name as name6_2_1_ from ostscourses.candidate_division divisions0_ inner join ostscourses.division division1_ on divisions0_.division_id=division1_.id where divisions0_.candidate_id=? order by division1_.name asc
2017-06-07 10:55:19.994 ERROR 5576 --- [pr-8080-exec-17] o.s.boot.web.support.ErrorPageFilter     : Forwarding to error page from request [/course/view_dates/] due to exception [null]

java.lang.StackOverflowError: null
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:562) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:277) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.thymeleaf.util.JavaScriptUtils.printCollection(JavaScriptUtils.java:316) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

1 个答案:

答案 0 :(得分:0)

我建议您转换为JSON列表变量并在javascript处理程序中管理json对象数组。

例如使用GSON library

//class fields...
private Gson GSON = new GsonBuilder().create();

@GetMapping(value= "/view_dates")
public String viewDates(Model model){
    List<Division> list =divisionService.getActiveDates();
    model.addAttribute("dates", GSON.toJson(list));
    return  "course_view_dates";
}

你的javscript处理程序应该没有任何改变。

编辑:也许你需要解析日期JSON.parse(日期)