Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230617
Commits
0b188b34
Commit
0b188b34
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BindingResult
parent
7695428f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
8 deletions
+19
-8
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+19
-8
No files found.
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
0b188b34
...
@@ -4,6 +4,7 @@ import jakarta.validation.Valid;
...
@@ -4,6 +4,7 @@ import jakarta.validation.Valid;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
...
@@ -57,19 +58,29 @@ public class ProductController {
...
@@ -57,19 +58,29 @@ public class ProductController {
}
}
@PostMapping
({
"/{id}/edit"
,
"/new"
})
@PostMapping
({
"/{id}/edit"
,
"/new"
})
public
String
saveProduct
(
@Valid
Product
product
)
{
public
String
saveProduct
(
@Valid
Product
product
,
BindingResult
bindingResult
)
{
// W tej wersji dane z wypełnionego formularza odbieramy w postaci jednego obiektu Product.
// W tej wersji dane z wypełnionego formularza odbieramy w postaci jednego obiektu Product.
// Na taki obiekt mówi się czasami "form backing bean".
// Na taki obiekt mówi się czasami "form backing bean".
// Spring sam wpisze dane do pól o takich samych nazwach.
// Spring sam wpisze dane do pól o takich samych nazwach.
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// W tej wersji z adnotacją @Valid obiekt jest walidowany przed wywołaniem tej metody.
// Kwestia Bean Validation:
// Jesli nie spełnia warunków → bład 400.
// W tej wersji przed parametrem Product jest adnotacja @Valid
// i dodatkowo do metoda posiada parametr BindingResult.
System
.
out
.
println
(
"id przed zapisem: "
+
product
.
getProductId
());
// Wówczas Spring dokonuje walidacji obiektu przed wywołaniem tej metody
// Gdyby obiekt nie spełniał warunków walidacji, to zostanie odrzucony przez metodę save. Będzie wyjątek.
// i informacje o wyniku walidacji przekazuje w parametrze BindingResult.
productRepository
.
save
(
product
);
// Metoda jest wywoływana zawsze, a to programista ma sprawdzić czy walidacja się powiodła.
System
.
out
.
println
(
"id po zapisie: "
+
product
.
getProductId
());
// W BindingResult znajdują się też informacje o błędach.
if
(
bindingResult
.
hasErrors
())
{
System
.
err
.
println
(
"Błędy w formulazu: "
+
bindingResult
.
getAllErrors
());
// nie próbujemy robić save
}
else
{
System
.
out
.
println
(
"id przed zapisem: "
+
product
.
getProductId
());
// Gdyby obiekt nie spełniał warunków walidacji, to zostanie odrzucony przez metodę save. Będzie wyjątek.
productRepository
.
save
(
product
);
System
.
out
.
println
(
"id po zapisie: "
+
product
.
getProductId
());
}
return
"product_form"
;
return
"product_form"
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment