public class Members {
private String fname;
private String lname;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
public class Greeting {
Map<String,List<Members>> templateMap;
public Map<String, List<Members>> getTemplateMap() {
return templateMap;
}
public void setTemplateMap( Map<String, List<Members>> templateMap) {
this.templateMap = templateMap;
}
}
From above code, how can i iterate and display values in templateMap in spring thymeleaf in html part?
In the controller method, you have to add it as an attribute to the Model like so: model.addAttribute("map",getTemplateMap());
(make a Greeting object to get the templateMap from)
In ur HTML you then acces it like so:
<div th:each="stepEntry: ${map}"> // stepEntry now is each entry of your map.
<p th:text="${stepEntry.key}"></p> // this is the key value of the entry.
<div th:each="member, iterStat : ${stepEntry.value}"> //this will iterate over the value (which is a list in this case) from the entry.
<p th:text="${member.fname}"></p> //this prints the fname of each member in the list
</div>
</div>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments