“This is the 12th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge 2021”.
Since the separation of the front and back ends, the front and back ends are rarely put together. Recently, I wrote a simple background, suddenly step on the hole, using the themeleaf template rendering, found th:each to iterate through the generated form data, has been throwing exceptions. Property or field ‘XXX’ cannot be found on null
Now let’s see what happens to this problem
I. Project construction
1. Project dependencies
This project is developed by SpringBoot 2.2.1.RELEASE + Maven 3.5.3 + IDEA
Open a Web service for testing
<dependencies>
<! -- Core dependencies for sending messages -->
<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>
Copy the code
Configuration file application.yml
server:
port: 8080
spring:
thymeleaf:
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false
Copy the code
II. Problem recurrence and treatment
1. Scene repetition
A basic demo to show you the problem
@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"."A grey"));
list.add(newMap("user"."2h"."name"."Two ashes."));
list.add(newMap("user"."3h"."name"."3 gray"));
model.addAttribute("list", list);
return "list"; }}Copy the code
The corresponding HTML file looks like this (note, put it under resources templates)
<! DOCTYPEhtml>
<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>
<span th:text="${item.name}"></span>
</div>
<hr/>
<p th:each="item: ${list}">
<p th:text="${item.user}"></p>
<p th:text="${item.name}"></p>
</p>
</div>
</body>
</html>
Copy the code
Notice in the template above that there are two each traversals, and the second one is the one that has the problem
2. Reasons
User does not have a syntax problem. When you change your HTML to the one below
<! DOCTYPEhtml>
<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>
<span th:text="${item.name}"></span>
</div>
</div>
</body>
</html>
Copy the code
So why can’t each tag be used?
This reason may need to take a look at the realization of the logic, there are friends who know can popular science
III. Can’t miss the source code and related knowledge points
0. Project
- Project: github.com/liuyueyi/sp…
- Source: github.com/liuyueyi/sp…
1. Wechat official account: Yash Blog
As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate
Below a gray personal blog, record all the study and work of the blog, welcome everyone to go to stroll
- A grey Blog Personal Blog blog.hhui.top
- A Grey Blog-Spring feature Blog Spring.hhui.top