в моем проекте весенней загрузки 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 value