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