Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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
java_dzienna_15_09
Commits
9e1482a8
Commit
9e1482a8
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BindingResult
parent
1d50dbbd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
4 deletions
+10
-4
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+10
-4
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
9e1482a8
package
sklep
.
controller
;
package
sklep
.
controller
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
...
@@ -10,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -10,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.data.domain.Sort
;
import
org.springframework.data.domain.Sort
;
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
;
...
@@ -63,6 +63,7 @@ public class ProductController {
...
@@ -63,6 +63,7 @@ public class ProductController {
@PostMapping
({
"/products/new"
,
"/products/{id}/edit"
})
@PostMapping
({
"/products/new"
,
"/products/{id}/edit"
})
public
String
saveProduct
(
public
String
saveProduct
(
@Valid
Product
product
,
@Valid
Product
product
,
BindingResult
bindingResult
,
Model
model
)
{
Model
model
)
{
// 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.
// Spring sam wpisze dane do pól o takich samych nazwach.
// Spring sam wpisze dane do pól o takich samych nazwach.
...
@@ -71,10 +72,15 @@ public class ProductController {
...
@@ -71,10 +72,15 @@ public class ProductController {
// Kwestia Bean Validation:
// Kwestia Bean Validation:
// W tej wersji przed parametrem Product jest adnotacja @Valid
// W tej wersji przed parametrem Product jest adnotacja @Valid
//
a nie ma parametru
BindingResult.
//
i dodatkowo do metoda posiada parametr
BindingResult.
// Wówczas Spring dokonuje walidacji obiektu przed wywołaniem tej metody
// Wówczas Spring dokonuje walidacji obiektu przed wywołaniem tej metody
// i w przypadku błędów walidacji NIE WYWOŁA tej metody, tylko zwróci błąd 400.
// i informacje o wyniku walidacji przekazuje w parametrze BindingResult.
try
{
// Metoda jest wywoływana zawsze, a to programista ma sprawdzić czy walidacja się powiodła.
// W BindingResult znajdują się też informacje o błędach.
// Gdybyśmy mimo błędów spróbowali wywołać save, to wtedy Hibernate zablokuje taki zapis.
if
(
bindingResult
.
hasErrors
())
{
model
.
addAttribute
(
"errors"
,
bindingResult
.
getAllErrors
());
}
else
try
{
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
productRepository
.
save
(
product
);
productRepository
.
save
(
product
);
System
.
out
.
println
(
"Produkt po zapisie : "
+
product
);
System
.
out
.
println
(
"Produkt po zapisie : "
+
product
);
...
...
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