Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
8b0dd8ea
Commit
8b0dd8ea
authored
Nov 26, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
f:form i błędy widoczne w formularzu
parent
a13e42f5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
38 deletions
+58
-38
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+18
-3
product_form.jsp
...Spring/src/main/webapp/WEB-INF/templates/product_form.jsp
+40
-35
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
8b0dd8ea
...
@@ -4,9 +4,12 @@ import java.math.BigDecimal;
...
@@ -4,9 +4,12 @@ import java.math.BigDecimal;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
import
javax.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
;
...
@@ -69,7 +72,7 @@ public class ProductController {
...
@@ -69,7 +72,7 @@ public class ProductController {
}
}
@GetMapping
(
"/new"
)
@GetMapping
(
"/new"
)
public
String
newProduct
()
{
public
String
newProduct
(
Product
product
)
{
return
"product_form"
;
return
"product_form"
;
}
}
...
@@ -86,12 +89,24 @@ public class ProductController {
...
@@ -86,12 +89,24 @@ public class ProductController {
}
}
@PostMapping
({
"/new"
,
"/{id}/edit"
})
@PostMapping
({
"/new"
,
"/{id}/edit"
})
public
String
saveProduct
(
Model
model
,
Product
product
)
{
public
String
saveProduct
(
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)
// i nie trzeba dodawać go w osobnym poleceniu.
// i nie trzeba dodawać go w osobnym poleceniu.
try
{
// Kwestia Bean Validation:
// 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
{
// Gdy próbujemy wywołać save, a obiekt nie spełnia wymagań validation, to wtedy Hibernate zablokuje taki zapis (wyrzuci wyjątek).
// Gdy próbujemy wywołać save, a obiekt nie spełnia wymagań validation, to wtedy Hibernate zablokuje taki zapis (wyrzuci wyjątek).
productRepository
.
save
(
product
);
productRepository
.
save
(
product
);
model
.
addAttribute
(
"saved"
,
true
);
model
.
addAttribute
(
"saved"
,
true
);
...
...
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/product_form.jsp
View file @
8b0dd8ea
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/core"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/core"
%
>
<
%@
taglib
prefix=
"s"
uri=
"http://www.springframework.org/tags"
%
>
<
%@
taglib
prefix=
"f"
uri=
"http://www.springframework.org/tags/form"
%
>
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
<meta
charset=
"UTF-8"
>
<meta
charset=
"UTF-8"
>
<title>
Edycja
towar
u
</title>
<title>
Edycja
danych produkt
u
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
>
</head>
</head>
<body>
<body>
...
@@ -18,36 +20,39 @@
...
@@ -18,36 +20,39 @@
</c:otherwise>
</c:otherwise>
</c:choose>
</c:choose>
<form
id=
"product-form"
method=
"post"
>
<f:form
id=
"product-form"
method=
"post"
modelAttribute=
"product"
>
<table
class=
"form"
>
<table
class=
"form-tab"
>
<tr>
<tr>
<td><label
for=
"productId"
>
Numer:
</label></td>
<td><f:label
path=
"productId"
>
Numer:
</f:label></td>
<td><input
name=
"productId"
placeholder=
"brak"
type=
"number"
readonly=
"readonly"
value=
"${product.productId}"
/></td>
<td><f:input
path=
"productId"
placeholder=
"brak"
type=
"number"
readonly=
"true"
/></td>
</tr>
</tr>
<tr>
<tr>
<td><label
for=
"productName"
>
Nazwa towaru:
</label></td>
<td><f:label
path=
"productName"
>
Nazwa towaru:
</f:label></td>
<td><input
name=
"productName"
placeholder=
"nazwa..."
type=
"text"
value=
"${product.productName}"
/>
<td><f:input
path=
"productName"
placeholder=
"nazwa..."
type=
"text"
/>
</td>
<f:errors
path=
"productName"
cssClass=
"form-error"
element=
"div"
/>
</tr>
</td>
<tr>
</tr>
<td><label
for=
"price"
>
Cena:
</label></td>
<tr>
<td><input
name=
"price"
placeholder=
"12.90"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.price}"
/>
<td><f:label
path=
"price"
>
Cena:
</f:label></td>
</td>
<td><f:input
path=
"price"
placeholder=
"12.90"
type=
"number"
step=
"0.01"
/>
</tr>
<f:errors
path=
"price"
cssClass=
"form-error"
element=
"div"
/>
<tr>
</td>
<td><label
for=
"vat"
>
Stawka VAT:
</label></td>
</tr>
<td><input
name=
"vat"
placeholder=
"0.23"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.vat}"
/>
<tr>
</td>
<td><f:label
path=
"vat"
>
Stawka VAT:
</f:label></td>
</tr>
<td><f:input
path=
"vat"
placeholder=
"0.23"
type=
"number"
step=
"0.01"
/>
<tr>
<f:errors
path=
"vat"
cssClass=
"form-error"
element=
"div"
/>
<td><label
for=
"description"
>
Opis:
</label></td>
</td>
<td><textarea
name=
"description"
rows=
"10"
cols=
"120"
>
${product.description}
</textarea></td>
</tr>
</tr>
<tr>
<tr>
<td><f:label
path=
"description"
>
Opis:
</f:label></td>
<td><button>
Zapisz
</button></td>
<td><f:textarea
path=
"description"
rows=
"10"
/></td>
</tr>
</tr>
</table>
<tr>
</form>
<td><f:button>
Zapisz
</f:button></td>
</tr>
</table>
</f:form>
<c:if
test=
"${not empty(errors)}"
>
<c:if
test=
"${not empty(errors)}"
>
<div
class=
"error"
>
<div
class=
"error"
>
...
@@ -64,8 +69,8 @@
...
@@ -64,8 +69,8 @@
<div
class=
"info"
>
Zapisano produkt nr ${product.productId}
</div>
<div
class=
"info"
>
Zapisano produkt nr ${product.productId}
</div>
</c:if>
</c:if>
<p>
<a
href=
"/products"
>
przejdź do listy produktów
</a>
</p>
<p>
[
<a
href=
"/products"
>
powrót do listy produktów
</a>
]
</p>
<p>
<a
href=
"/"
>
strona główna
</a>
</p>
<p>
[
<a
href=
"/"
>
powrót do spisu treści
</a>
]
</p>
</body>
</body>
</html>
</html>
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