简体   繁体   English

如何迭代春季百里香中的对象列表

[英]How to iterate list of objects in spring thymeleaf

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? 从上面的代码中,如何在html部分的spring thymeleaf中迭代和显示templateMap中的值?

In the controller method, you have to add it as an attribute to the Model like so: model.addAttribute("map",getTemplateMap()); 在controller方法中,您必须像这样将其添加为Model的属性: model.addAttribute("map",getTemplateMap()); (make a Greeting object to get the templateMap from) (创建一个Greeting对象以从中获取templateMap)

In ur HTML you then acces it like so: 在您的HTML中,您可以按以下方式进行访问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM