简体   繁体   中英

Assign a list<T> to a variable in javascript using thymeleaf

I'm trying to get a list of objects in a variable in javascript, added to model in the controller. As a test, I passed a list of Strings and it worked fine but it doesn't work with the list of objects. Can anybody help me please? I've been searching for a solution but I can't find anything.

This is the controller

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

and this is the javascript code

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

This is the entity for more information

@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;
} 

It produces two calls to the database and then the 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]

I advise you to convert to JSON list variable and to manage an array of json object in javascript handler.

For example using 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";
}

Your javscript handler should work without any change.

EDIT: maybe you Need to parse dates JSON.parse(dates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM