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
1d50dbbd
Commit
1d50dbbd
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
@Valid
parent
ba270f8c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
6 deletions
+7
-6
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+7
-6
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
1d50dbbd
...
...
@@ -4,6 +4,8 @@ import java.math.BigDecimal;
import
java.util.List
;
import
java.util.Optional
;
import
javax.validation.Valid
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.stereotype.Controller
;
...
...
@@ -60,7 +62,7 @@ public class ProductController {
@PostMapping
({
"/products/new"
,
"/products/{id}/edit"
})
public
String
saveProduct
(
Product
product
,
@Valid
Product
product
,
Model
model
)
{
// 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.
...
...
@@ -68,11 +70,10 @@ public class ProductController {
// i nie trzeba dodawać go w osobnym poleceniu.
// Kwestia Bean Validation:
// W tej wersji (bez adnotacji @Valid) Spring wchodzi do tej metody
// i dopiero podczas próby zapisania obiektu operacją save Hibernate
// dokonuje walidacji obiektu względem adnotacji.
// W przypadku niezgodności wyrzucany jest wyjątek, a obiekt nie jest zapisywany.
// Dzieje się to jeszcze zanim dane trafią do bazy danych.
// W tej wersji przed parametrem Product jest adnotacja @Valid
// a nie ma parametru BindingResult.
// 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.
try
{
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
productRepository
.
save
(
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