【发布时间】:2020-04-26 03:31:41
【问题描述】:
在我的 Spring Boot 2 项目中。
控制器:
@Controller
public class CategoryController {
@Value("${spring.application.name}")
private String appName;
@RequestMapping("category/add")
public String addtCategory(Model model) {
model.addAttribute("isAdd", true);
model.addAttribute("category", new Category());
return "category";
}
通过这个添加额外的参数:
model.addAttribute("isAdd", true);
模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${appName}">Category template title</title>
<link th:href="@{/public/style.css}" rel="stylesheet"/>
<meta charset="UTF-8"/>
</head>
<body>
<div class="container">
<form method="post" action="#" th:object="${category}" th:action="@{/category}">
<h3>Edit Category</h3>
<input type="hidden" id="isAdd" th:field="${isAdd}"/>
<input type="hidden" id="id" th:field="*{id}"/>
<input type="text" placeholder="Name" id="name" th:field="*{name}"/>
<input type="hidden" id="created" th:field="*{created}"/>
<textarea placeholder="Description" rows="5" id="description"
th:field="*{description}"></textarea>
<input type="submit" value="Submit"/>
</form>
<div class="result_message" th:if="${submitted}">
<h3>Your category has been submitted.</h3>
<p>Find all categories <a href="/categories">here</a></p>
</div>
</div>
</body>
</html>
并生成html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="/public/style.css" rel="stylesheet"/>
<meta charset="UTF-8"/>
</head>
<body>
<div class="container">
<form method="post" action="/category"><input type="hidden" name="_csrf" value="5cda30d8-6f11-4e82-bb06-a8eb141afb41"/>
<h3>Edit Category</h3>
<input type="hidden" id="isAdd" name="" value=""/>
<input type="hidden" id="id" name="id" value="0"/>
<input type="text" placeholder="Name" id="name" name="name" value=""/>
<input type="hidden" id="created" name="created" value=""/>
<textarea placeholder="Description" rows="5" id="description" name="description"></textarea>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
如您所见:
<input type="hidden" id="isAdd" name="" value=""/>
为空。为什么?它必须是 true 值
【问题讨论】:
标签: spring-boot thymeleaf