How do I access specific Set variables from Spring using Javascript?
My controller returns a data set containing the following information from multiple classes
Person
String firstName;
String lastName;
int age;
Address
String country;
String city;
Which is passed to the html as model.addAttribute("personSet", form);
When it gets passed to the html, the data becomes like so:
Set1
(from Person Class)
firstName = John
lastName = Doe
age = 20
(from Address Class)
country = Japan
city = Tokyo
Set 2
(from Person Class)
firstName = Mary
lastName = Smith
age = 30
(from Address Class)
country = Korea
city = Seoul
What I'm trying to do with javascript is that I want to get the specific variable data so I can perform an if condition for every item in the set. For example: For every person in personSet, if country = Japan, print firstName + lastName.
To solve this I created the following code
Please note I removed some variable declarations and this is an in-line js code snippet containing the following code:
<script th:inline="javascript"> and /*<![CDATA[*/
function printPeopleFromJapan() {
//get the data of the whole set
personSetJS = /*[[${personSet.Person}]]*/'default';
//loop through all persons in the set
for (person of personSet) {
country = /*[[${personSet.country}]]*/'default';
//if country is equal to Japan, get the person full name
if (country == "Japan") {
personFirstName = /*[[${personSet.firstName}]]*/'default';
personLastName = /*[[${personSet.lastName}]]*/'default';
console.log(personFirstName + personLastName);
}
}
}
But my program doesn't run and I'm getting errors and a blank page.
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "personSet.Person"
Please help.