Hej. Chcę dodać walidację do pól. Mam klasę domenową:
@Entity
public class Dog {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min=2, max=30)
private String name;
+ wiadomo gettery, settery.
Mam kontroler:
@RequestMapping(value="/addDog", method = RequestMethod.GET)
public String addDog(Model model) {
model.addAttribute("newDog", new Dog());
return "adddog";
}
@RequestMapping(value="/addDog", method = RequestMethod.POST)
public String saveDog(@ModelAttribute @Valid Dog newDog, BindingResult result) {
if (result.hasErrors()) {
return "adddog";
}else{
dogService.addDog(newDog);
return "redirect:/success";
}
}
@RequestMapping(value="/success", method = RequestMethod.GET)
public String succes(Model model) {
model.addAttribute("newDog", new Dog());
model.addAttribute("msgAdded", "Dodano nowego psa!");
return "adddog";
}
No i mam stronę:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<title>Psy</title>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Moje psy</a>
</div>
<ul class="nav navbar-nav">
<li><a href="#" th:href="@{/}">Home</a></li>
<li><a href="#" th:href="@{/dogs}">Lista psów</a></li>
<li class="active"><a href="#" th:href="@{/addDog}">Dodaj psa</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><form th:action="@{/logout}" method="post">
<input type="submit" value="Wyloguj się"/>
</form>
</li>
</ul>
</div>
</nav>
<section class="container">
<h1>Dodaj nowego psa</h1>
<form action="#" th:action="@{/addDog}" method="post" th:object="${newDog}">
<table>
<tr>
<td>Imię psa:</td>
<td><input type="text" th:field="*{name}"/></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Wiek psa:</td>
<td><input type="text" th:field="*{age}" /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Wiek błąd</td>
</tr>
<tr>
<td>Imię właściciela:</td>
<td><input type="text" th:field="*{ownersName}" /></td>
<td th:if="${#fields.hasErrors('ownersName')}" th:errors="*{ownersName}">Imię właściciela błąd</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
<p th:if="${msgAdded}">
<div class="alert alert-success alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><h1 th:text="${msgAdded}"></h1></strong>
</div>
</p>
</section>
</body>
</html>
Po wysłaniu formularza pojawia się błąd:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Feb 22 16:47:42 CET 2018
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (adddog:33)
Siedzę nad tym drugi dzień i nie rozumiem, na czym polega błąd. Być może coś nie tak jest z polem ID?
Nie rozumiem, jak działa
BindingResult
Obiekt sprawdza poprawność pól po kliknięciu przycisku submit? Rozumiem, że polu ID przypisuje kolejną wolną wartość?
Do Gradle'a dodałem: compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.7.Final'