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

猜你喜欢

转载自www.cnblogs.com/chenyameng/p/12661758.html