thymeleaf關於Map的取值


thymeleaf關於Map的取值

需求:

如圖所示:需要按照分類去數據庫中查詢,比如查找文件表中標簽是python類型的有幾個,Java類型的又有個,然后存進map里面,然后再在前端通過thymeleaf獲取到map

解決:

controller層:

public ModelAndView toDownLoadCenter(){
    //查詢類型的文件有多少個
    ModelAndView modelAndView= new ModelAndView();
    Map<String,Long> fileTypeMap =  fileService.countByFileType();
    modelAndView.addObject("map",fileTypeMap);
    modelAndView.setViewName("/download");
    return modelAndView;
}

serviceImpl層:

@Override
public Map<String, Long> countByFileType() {
    Map<String,Long> map = new HashMap<>();
    String python = "Python";
    String java = "Java";
    String other = "Other";
    map.put(python,countFile(python));
    map.put(java,countFile(java));
    map.put(other,countFile(other));
    return map;
}
@Override
public Long countFile(String type){
    return fileRepository.countByFileTag(type);
}

前端:

<div class="extra content">
    <span class="right floated">
        最近更新:<span>2020</span>
    </span>
    <span>
        <i class="file icon"></i>
        共<span th:text="${map.get('Other')}">17</span>個文件
    </span>
</div>

總結:

如果是一個map的話,需要根據key值來取map的value值,采用th:text="${map.get('Other')}"來獲取other的值。

具體可以參考stackoverflow


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM