SpringBoot + thymeleaf foreach踩坑记录

简介: 话说自从前后端分离之后,前后端放在一起的场景就很少了,最近写个简单的后台,突然踩坑了,使用themeleaf模板渲染时,发现th:each来遍历生成表单数据,一直抛异常,提示Property or field 'xxx' cannot be found on null接下来看一下这个问题到底是个什么情况

image.png


话说自从前后端分离之后,前后端放在一起的场景就很少了,最近写个简单的后台,突然踩坑了,使用themeleaf模板渲染时,发现th:each来遍历生成表单数据,一直抛异常,提示Property or field 'xxx' cannot be found on null


接下来看一下这个问题到底是个什么情况


I. 项目搭建



1. 项目依赖


本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

开一个web服务用于测试


<dependencies>
    <!-- 邮件发送的核心依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>
复制代码


配置文件application.yml


server:
  port: 8080
spring:
  thymeleaf:
    mode: HTML
    encoding: UTF-8
    servlet:
      content-type: text/html
    cache: false
复制代码


II. 问题复现与处理



1. 场景复现


一个最基础的demo,来演示一下问题


@Controller
public class IndexController {
  public Map<String, Object> newMap(String key, Object val, Object... kv) {
      Map<String, Object> map = new HashMap<>();
      map.put(key, val);
      for (int i = 0; i < kv.length; i += 2) {
          map.put(String.valueOf(kv[i]), kv[i + 1]);
      }
      return map;
  }
  @GetMapping(path = "list")
  public String list(Model model) {
      List<Map> list = new ArrayList<>();
      list.add(newMap("user", "yh", "name", "一灰"));
      list.add(newMap("user", "2h", "name", "2灰"));
      list.add(newMap("user", "3h", "name", "3灰"));
      model.addAttribute("list", list);
      return "list";
  }
}
复制代码


对应的html文件如下(注意,放在资源目录 templates 下)


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div>
    <div th:each="item: ${list}">
        <span th:text="${item.user}"></span>
        &nbsp;&nbsp;
        <span th:text="${item.name}"></span>
    </div>
    <hr/>
    <p th:each="item: ${list}">
        <p th:text="${item.user}"></p>
        &nbsp;&nbsp;
        <p th:text="${item.name}"></p>
    </p>
</div>
</body>
</html>
复制代码


注意上面的模板,有两个each遍历,出现问题的是第二个


网络异常,图片无法展示
|


2. 原因说明


上面提示user没有,那么是否是语法问题呢?将html改成下面这个时


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div>
    <div th:each="item: ${list}">
        <span th:text="${item.user}"></span>
        &nbsp;&nbsp;
        <span th:text="${item.name}"></span>
    </div>
</div>
</body>
</html>
复制代码

image.png


相同的写法,上面这个就可以,经过多方尝试,发现出现问题的原因居然是<p>这个标签


简单来讲,就是<p>标签不能使用th:each,测试一下其他的标签之后发现<img><input>标签也不能用


那么问题来了,为啥这几个标签不能使用each呢?


这个原因可能就需要去瞅一下实现逻辑了,有知道的小伙伴可以科普一下



相关文章
|
JSON JavaScript 数据可视化
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
902 0
SpringBoot+Thymeleaf+ECharts实现大数据可视化(基础篇)
|
缓存 JavaScript 前端开发
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
2231 0
SpringBoot——Thymeleaf中的th:inline(内敛文本text、内敛脚本javascript)
|
缓存 编解码 移动开发
SpringBoot 整合 Thymeleaf|学习笔记
快速学习 SpringBoot 整合 Thymeleaf
158 0
SpringBoot 整合 Thymeleaf|学习笔记
|
安全 前端开发 Java
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
149 0
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(三)
|
移动开发 安全 前端开发
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
227 0
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(二)
|
消息中间件 JSON 前端开发
SpringBoot2.x系列教程08--SpringBoot中整合Thymeleaf动态模板
前言 在前面的章节中,壹哥 带各位利用SpringBoot实现了SSM整合,发现现在SSM整合变得的非常简单,通过几个配置就能搭建出一个项目开发环境。但是在之前的案例中,我们并没有提供UI界面,那么在SpringBoot中如何整合UI界面呢?使用JSP展示页面,还是用HTML展示页面,或者还有其他方案? 在今天的内容里,壹哥 会带大家学习如何在SpringBoot中展示UI界面,这样大家以后就可以把数据信息在页面上渲染展示了。 一. Web开发方式简介 SpringBoot作为一个简化项目开发的利器,其实它为我们提供了一套完整的Web开发方案,从前端到后端,再到数据库、定时任务、消息队列等都
244 0
|
开发框架 安全 Java
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(一)
狂神说SpringBoot:SpringSecurity笔记及thymeleaf静态资源(一)
453 0
|
Dubbo Java 应用服务中间件
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象
SpringBoot——Thymeleaf中的表达式基本对象、表达式功能对象
|
缓存 Java
SpringBoot——Thymeleaf中的四种字面量(文本、数字、布尔、null)、字符串拼接、运算符
SpringBoot——Thymeleaf中的四种字面量(文本、数字、布尔、null)、字符串拼接、运算符
SpringBoot——Thymeleaf中的四种字面量(文本、数字、布尔、null)、字符串拼接、运算符