我不知道问题出在哪里。问题出现在百里香碎片上。
让我逐部分描述问题。
home.html
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:each="msg : ${greetings}" th:text="${msg}" th:unless="${msg.contains('Hello')}">Hello, homepage!</h1>
<h2 th:each=" i : ${n} " th:text="${i}" th:unless="${i.contains('1')}">For loop</h2>
<div th:insert="fragments/create-credential :: credential"></div>
</body>
</html>
HomeController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
@Controller
@RequestMapping("/home")
public class HomeController {
@GetMapping
public String action(Model model){
model.addAttribute("greetings", new String[] {"Hello","What's up","Now it's Okey"});
model.addAttribute("n",new String[] {"1","2","3"});
return "home";
}
}
创建凭证.html
<!doctype html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="container" th:fragment="credential">
<h1 th:text="${hello}">Got the problem</h1>
</div>
</body>
</html>
CredentialController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/create-credential")
public class CredentialController {
@GetMapping
public String anotherAction(Model model){
model.addAttribute("hello","Got the problem");
return "create-credential";
}
}