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
b3836ae4
Commit
b3836ae4
authored
Apr 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przekierowanie na inną stronę po zaakceptowaniu formularza
parent
95bc5cf8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
9 deletions
+8
-9
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+8
-5
product_form.jsp
...Spring/src/main/webapp/WEB-INF/templates/product_form.jsp
+0
-4
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
b3836ae4
...
...
@@ -77,7 +77,6 @@ public class ProductController {
// 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.
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
System
.
out
.
println
(
"Obiekt z formularza: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
// Kwestia Bean Validation:
// W tej wersji przed parametrem Product jest adnotacja @Valid
...
...
@@ -87,15 +86,19 @@ public class ProductController {
// 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
());
// W końcowej wersji nie dodajemy errorów, które i tak wyświetlą się przy polach formularza
// model.addAttribute("errors", bindingResult.getAllErrors());
return
"product_form"
;
}
else
try
{
// Gdy próbujemy wywołać save, a obiekt nie spełnia wymagań validation, to wtedy Hibernate zablokuje taki zapis (wyrzuci wyjątek).
// W tej wersji do tego nie dojdzie, bo if sprawdził, że nie ma błędów walidacji. Mogą być błedy techniczne bazy danych.
productRepository
.
save
(
product
);
System
.
out
.
println
(
"Obiekt po zapisie: "
+
product
.
getProductId
()
+
" "
+
product
.
getProductName
());
model
.
addAttribute
(
"saved"
,
true
);
// Po pomyślnym zapisaniu przechodzimy na stronę tego produktu
return
"redirect:/products/"
+
product
.
getProductId
(
);
}
catch
(
Exception
e
)
{
model
.
addAttribute
(
"errors"
,
ExceptionUtils
.
allMessages
(
e
));
return
"product_form"
;
}
return
"product_form"
;
}
}
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/product_form.jsp
View file @
b3836ae4
...
...
@@ -64,10 +64,6 @@
</div>
</c:if>
<c:if
test=
"${saved}"
>
<div
class=
"info"
>
Zapisano produkt nr ${product.productId}
</div>
</c:if>
<div><a
class=
"action"
href=
"/products"
>
powrót do listy produktów
</a></div>
<div><a
class=
"action"
href=
"/"
>
powrót do spisu treści
</a></div>
</body>
...
...
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