So iterieren Sie die Liste der Objekte im Frühjahr


Bhanu Krishnan:
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; 
    } 

} 

Wie kann ich aus dem obigen Code Werte in templateMap im Frühjahrs-Thymeleaf im HTML-Teil iterieren und anzeigen ?

JensW:

In der Controller-Methode müssen Sie es wie folgt als Attribut zum Modell hinzufügen: model.addAttribute("map",getTemplateMap());(Erstellen Sie ein Begrüßungsobjekt, um die templateMap abzurufen.)

In ur ​​HTML greifen Sie dann folgendermaßen darauf zu:

<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>

Verwandte Artikel