Ошибка при переходе на mapping для добавления записи в бд(создание CRUD-приложения с использованием Sping Boot)
Ошибка при переходе на mapping /create-user на странице по mapping /users:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Хочу разместить там три текстовых поля с лейблами для каждого из них: Name, Surname, Phone и кнопку "создать", чтоб добавить запись в бд и после этого редиректить на начальную страницу user-list.html. То есть хочу сделать, что-то типа телефонного справочника в очень упрощенном варианте. Переход на страницу по мэппингу /update-user для обновления данных об пользователе работает нормально(то есть у меня корректно работает функция изменения данных пользователя) и вот, что странно: там я могу разместить текстовые поля, а на странице по мэппингу /user-create, нет, там мне удалось разместить только кнопку. Содержимое файла user-create.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="UTF-8">
<title>Create user</title>
</head>
<body>
<form action="#" th:action="@{/user-create}" th:object="${user}" method="post">
<label for="surname">Surname</label>
<input type="text" th:field="*{surname}" id="surname" placeholder="Surname">
<label for="nameuser">Name</label>
<input type="text" th:field="*{nameuser}" id="nameuser" placeholder="Name">
<label for="phoneuser">Name</label>
<input type="text" th:field="*{phoneuser}" id="phoneuser" placeholder="Phone">
<input type="submit" value="Create User">
</form>
</body>
</html>
Содержимое файла user-list.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Users</title>
</head>
<body>
<div th:switch="${users}">
<h2 th:case="null">No users found!</h2>
<div th:case="*">
<h2>Users</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>Surname</th>
<th>Name</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.nameuser}"></td>
<td th:text="${user.phoneuser}"></td>
<td><a th:href="@{user-delete/{id}(id=${user.id})}">Delete</a></td>
<td><a th:href="@{user-update/{id}(id=${user.id})}">Edit</a></td>
</tr>
</tbody>
</table>
</div>
<p><a href="/user-create">Create user</a></p>
</div>
</body>
</html>
Содержимое файла user-update:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="UTF-8">
<title>Create user</title>
</head>
<body>
<form action="#" th:action="@{/user-create}" th:object="${user}" method="post">
<label for="id">ID</label>
<input readonly type="number" th:field="*{id}" id="id" placeholder="ID">
<label for="surname">Surname</label>
<input type="text" th:field="*{surname}" id="surname" placeholder="Surname">
<br/>
<label for="nameuser">Name</label>
<input type="text" th:field="*{nameuser}" id="nameuser" placeholder="Name">
<br/>
<label for="phoneuser">Phone</label>
<input type="text" th:field="*{phoneuser}" id="phoneuser" placeholder="Phone">
<br/>
<input type="submit" value="Update User">
</form>
</body>
</html>
Код UserController.java:
import com.example.demo.model.Users;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users")
public String findAll(Model model){
List<Users> users = userService.findAll();
model.addAttribute("users", users);
return "user-list";
}
@GetMapping("/user-create")
public String createUserForm(Users user){
return "user-create";
}
@PostMapping("/user-create")
public String createUser(Users user){
userService.saveUser(user);
return "redirect:/users";
}
@GetMapping("user-delete/{id}")
public String deleteUser(@PathVariable("id") Long id){
userService.deleteById(id);
return "redirect:/users";
}
@GetMapping("/user-update/{id}")
public String updateUserForm(@PathVariable("id") Long id, Model model){
Users user = userService.findById(id);
model.addAttribute("user", user);
return "user-update";
}
@PostMapping("/user-update")
public String updateUser(Users user){
userService.saveUser(user);
return "redirect:/users";
}
}