Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
0b7758f5
Commit
0b7758f5
authored
Apr 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BindingResult i sprawdzanie ifem czy są błędy
parent
e4c948af
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
8 deletions
+14
-8
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+13
-3
Product.java
PC30-SklepSpring/src/main/java/sklep/model/Product.java
+1
-5
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
0b7758f5
...
@@ -7,6 +7,7 @@ import java.util.Optional;
...
@@ -7,6 +7,7 @@ import java.util.Optional;
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
;
...
@@ -71,13 +72,22 @@ public class ProductController {
...
@@ -71,13 +72,22 @@ public class ProductController {
}
}
@PostMapping
({
"/new"
,
"/{id}/edit"
})
@PostMapping
({
"/new"
,
"/{id}/edit"
})
public
String
zapiszProdukt
(
Model
model
,
@Valid
Product
product
)
{
public
String
zapiszProdukt
(
Model
model
,
@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.
// 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)
System
.
out
.
println
(
"Obiekt z formularza: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
try
{
// Kwestia Bean Validation:
System
.
out
.
println
(
"Obiekt przed zapisem: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
// W tej wersji przed parametrem Product jest adnotacja @Valid
// i dodatkowo do metoda posiada parametr BindingResult.
// Wówczas Spring dokonuje walidacji obiektu przed wywołaniem tej metody
// i informacje o wyniku walidacji przekazuje w parametrze BindingResult.
// Metoda jest wywoływana zawsze, a to programista ma sprawdzić czy walidacja się powiodła.
// W BindingResult znajdują się też informacje o błędach.
if
(
bindingResult
.
hasErrors
())
{
model
.
addAttribute
(
"errors"
,
bindingResult
.
getAllErrors
());
}
else
try
{
productRepository
.
save
(
product
);
productRepository
.
save
(
product
);
System
.
out
.
println
(
"Obiekt po zapisie: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
System
.
out
.
println
(
"Obiekt po zapisie: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
model
.
addAttribute
(
"saved"
,
true
);
model
.
addAttribute
(
"saved"
,
true
);
...
...
PC30-SklepSpring/src/main/java/sklep/model/Product.java
View file @
0b7758f5
...
@@ -2,11 +2,7 @@ package sklep.model;
...
@@ -2,11 +2,7 @@ package sklep.model;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
jakarta.persistence.*
;
import
jakarta.persistence.*
;
import
jakarta.validation.constraints.DecimalMax
;
import
jakarta.validation.constraints.*
;
import
jakarta.validation.constraints.DecimalMin
;
import
jakarta.validation.constraints.Min
;
import
jakarta.validation.constraints.NotNull
;
import
jakarta.validation.constraints.Size
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
...
...
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