thymeleaf模板获取不到model中的map数据
发布日期:2021-10-06 15:05:15 浏览次数:3 分类:技术文章

本文共 809 字,大约阅读时间需要 2 分钟。

问题:

在Controller中的model中存放了一个map数据,代码如下:

@Controllerpublic class GoodsController {
@Autowired private GoodsService goodsService; @GetMapping("item/{id}.html") public String toItemPage(@PathVariable("id")Long id, Model model){
Map
map = this.goodsService.loadData(id); model.addAttribute(map); return "item"; }}

在模板页面中尝试获取model中的数据:

得不到model中的数据:

原因

在model中放入map调用的方法不对。

解决

应该使用这个方法保存map类型的数据:

model.addAllAttributes(map);
正确代码如下:

@Controllerpublic class GoodsController {
@Autowired private GoodsService goodsService; @GetMapping("item/{id}.html") public String toItemPage(@PathVariable("id")Long id, Model model){
Map
map = this.goodsService.loadData(id); model.addAllAttributes(map); return "item"; }}

转载地址:https://blog.csdn.net/m0_37815017/article/details/103064912 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Job for network.service failed because the control process exited with error code问题
下一篇:feign.FeignException$InternalServerError: status 500 reading xxx

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月02日 05时32分43秒