1

I am trying to pass some objects from Java backend to the JavaScript frontend as following:

WeightController:

String jsonString = new JSONObject()
   .put("weightEntry", weightEntry).toString();

model.addAttribute("weightEntry", jsonString);

using the following library to create a json:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20220320</version>
</dependency> 
@Entity
@Table(name = "WeightEntry")
public class WeightEntry {

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

    @ManyToOne()
    private User user;

    @OneToMany(mappedBy = "weightEntry")
    private Set<Image> imageSet = new HashSet<>();

    private double weight;
    private Date date;

    public WeightEntry() {}
}
<script>
    var obj = eval('('+'${weightEntry}'+')'); //this is not working
    var obj2 = [[${weightEntry}]] //this shows blank??
</script>

I found this on stackoverflow which is outdated (it seems) How to access model attribute in Javascript

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
fromSeri
  • 91
  • 7
  • That `var obj2 = [[${weightEntry}]] ;` line looks like [Thymeleaf JavaScript inlining](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#javascript-inlining) to me. But (a) that would require you to use ` – andrewJames May 09 '22 at 00:40

0 Answers0