Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
3d48fc9a
Commit
3d48fc9a
authored
Jun 27, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Formularz edycji danych z f:form i f:error
parent
31b71fc5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
21 deletions
+59
-21
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+30
-4
product_form.jsp
...Spring/src/main/webapp/WEB-INF/templates/product_form.jsp
+29
-17
No files found.
PC33-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
3d48fc9a
package
sklep
.
controller
;
package
sklep
.
controller
;
import
jakarta.validation.Valid
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.*
;
import
org.springframework.web.bind.annotation.*
;
import
sklep.basket.Basket
;
import
sklep.basket.Basket
;
import
sklep.model.Product
;
import
sklep.model.Product
;
...
@@ -84,7 +86,7 @@ public class ProductController {
...
@@ -84,7 +86,7 @@ public class ProductController {
}
}
@GetMapping
(
"/new"
)
@GetMapping
(
"/new"
)
public
String
nowyProdukt
()
{
public
String
nowyProdukt
(
@ModelAttribute
Product
product
)
{
return
"product_form"
;
return
"product_form"
;
}
}
...
@@ -101,9 +103,33 @@ public class ProductController {
...
@@ -101,9 +103,33 @@ public class ProductController {
}
}
@PostMapping
({
"/new"
,
"/{id}/edit"
})
@PostMapping
({
"/new"
,
"/{id}/edit"
})
public
String
zapiszProdukt
(
Product
product
)
{
// Ta metoda zapisuje dane przysłane z formularza obojętnie, czy to było edit, czy new
productRepository
.
save
(
product
);
// Adnotacja @Valid powoduje, że Spring dokona walidacji obiektu PRZED uruchomieniem tej metody.
return
"product_form"
;
// Jeśli nie ma dodatkowego parametru BindingResult, a są błędy walidacji, to Spring naszej metody w ogóle nie uruchomi.
// Jeśli jednak metoda ma parametr BindingResult, to
// metoda zawsze jest uruchamiana przez Springa,
// a w tym parametrze zawarte są informacje o przebiegu walidacji, w tym błędy.
// Bez adnotacji @ModelAttribute też działało
public
String
saveProduct
(
@ModelAttribute
@Valid
Product
product
,
BindingResult
bindingResult
)
{
// 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)
if
(
bindingResult
.
hasErrors
())
{
System
.
err
.
println
(
"Są błędy: "
+
bindingResult
.
getAllErrors
());
// normalnie wyświetlilibyśmy coś na stronie...
// ale robi to za nas tag f:form i f:errors
// Dopóki są błędy, pozostajemy na stronie formularza.
return
"product_form"
;
}
else
{
// Gdy podczas próby zapisu (operacja save) obiekt nie spełnia warunków walidacji, to jest wyrzucany wyjątek.
System
.
out
.
println
(
"id przed zapisem: "
+
product
.
getId
());
productRepository
.
save
(
product
);
System
.
out
.
println
(
"id po zapisie: "
+
product
.
getId
());
return
"redirect:/products"
;
// Po zaakceptowaniu danych, klient jest przenoszony do listy produktów.
}
}
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
...
...
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/product_form.jsp
View file @
3d48fc9a
<
%@
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=
"jakarta.tags.core"
%
>
<
%@
taglib
prefix=
"c"
uri=
"jakarta.tags.core"
%
>
<
%@
taglib
prefix=
"f"
uri=
"http://www.springframework.org/tags/form"
%
>
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
...
@@ -8,40 +9,51 @@
...
@@ -8,40 +9,51 @@
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
>
</head>
</head>
<body>
<body>
<h1>
Edycja produktu
</h1>
<form
id=
"product-form"
method=
"post"
>
<c:choose>
<table
class=
"form"
>
<c:when
test=
"${not empty(product.id)}"
>
<h1>
Edycja produktu nr ${product.id}
</h1>
</c:when>
<c:otherwise>
<h1>
Edycja nowego produktu
</h1>
</c:otherwise>
</c:choose>
<f:form
id=
"product-form"
method=
"post"
modelAttribute=
"product"
>
<table
class=
"form-tab"
>
<tr>
<tr>
<td><
label
for=
"productId"
>
Numer:
</
label></td>
<td><
f:label
path=
"id"
>
Numer:
</f:
label></td>
<td><
input
id=
"productId"
name=
"id"
placeholder=
"brak"
type=
"number"
readonly=
"readonly"
value=
"${product.id}
"
/></td>
<td><
f:input
path=
"id"
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
id=
"productName"
name=
"productName"
placeholder=
"nazwa..."
type=
"text"
value=
"${product.productName}"
/>
<td><f:input
path=
"productName"
placeholder=
"nazwa..."
type=
"text"
/>
<f:errors
path=
"productName"
cssClass=
"form-error"
element=
"div"
/>
</td>
</td>
</tr>
</tr>
<tr>
<tr>
<td><label
for=
"price"
>
Cena:
</label></td>
<td><f:label
path=
"price"
>
Cena:
</f:label></td>
<td><input
id=
"price"
name=
"price"
placeholder=
"12.90"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.price}"
/>
<td><f:input
path=
"price"
placeholder=
"12.90"
type=
"number"
step=
"0.01"
/>
<f:errors
path=
"price"
cssClass=
"form-error"
element=
"div"
/>
</td>
</td>
</tr>
</tr>
<tr>
<tr>
<td><label
for=
"vat"
>
Stawka VAT:
</label></td>
<td><f:label
path=
"vat"
>
Stawka VAT:
</f:label></td>
<td><input
id=
"vat"
name=
"vat"
placeholder=
"0.23"
title=
"tu wpisz vat"
type=
"number"
step=
"0.01"
value=
"${product.vat}"
/>
<td><f:input
path=
"vat"
placeholder=
"0.23"
type=
"number"
step=
"0.01"
/>
<f:errors
path=
"vat"
cssClass=
"form-error"
element=
"div"
/>
</td>
</td>
</tr>
</tr>
<tr>
<tr>
<td><
label
for=
"description"
>
Opis:
</
label></td>
<td><
f:label
path=
"description"
>
Opis:
</f:
label></td>
<td><
textarea
id=
"description"
name=
"description"
rows=
"10"
cols=
"120"
>
${product.description}
</textarea
></td>
<td><
f:textarea
path=
"description"
rows=
"10"
/
></td>
</tr>
</tr>
<tr>
<tr>
<td><
button>
Zapisz
</
button></td>
<td><
f:button>
Zapisz
</f:
button></td>
</tr>
</tr>
</table>
</table>
</form>
</f
:f
orm>
<div
class=
"action"
><a
href=
"/products"
>
powrót do listy produktów
</a></div>
<div
><a
class=
"action"
href=
"/products"
>
powrót do listy produktów
</a></div>
<div
class=
"action"
><a
href=
"/"
>
powrót do spisu treści
</a></div>
<div
><a
class=
"action"
href=
"/"
>
powrót do spisu treści
</a></div>
</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