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
2adddba3
Commit
2adddba3
authored
Nov 26, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Formularz edycji danych - pierwsza wersja
parent
a5711c3e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
10 deletions
+110
-10
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+37
-10
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+2
-0
product_form.jsp
...Spring/src/main/webapp/WEB-INF/templates/product_form.jsp
+71
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
2adddba3
...
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
...
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
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.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
sklep.model.Product
;
import
sklep.model.Product
;
...
@@ -19,7 +20,7 @@ import sklep.repository.ProductRepository;
...
@@ -19,7 +20,7 @@ import sklep.repository.ProductRepository;
public
class
ProductController
{
public
class
ProductController
{
@Autowired
@Autowired
private
ProductRepository
productRepository
;
private
ProductRepository
productRepository
;
@GetMapping
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
public
String
wszystkieProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
List
<
Product
>
products
=
productRepository
.
findAll
();
...
@@ -38,17 +39,14 @@ public class ProductController {
...
@@ -38,17 +39,14 @@ public class ProductController {
return
"missing_product"
;
return
"missing_product"
;
}
}
}
}
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
public
String
wyszukiwarka
(
Model
model
,
public
String
wyszukiwarka
(
Model
model
,
String
name
,
BigDecimal
min
,
BigDecimal
max
)
{
String
name
,
BigDecimal
min
,
BigDecimal
max
)
{
List
<
Product
>
products
=
List
.
of
();
List
<
Product
>
products
=
List
.
of
();
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
min
==
null
&&
max
==
null
)
{
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
min
==
null
&&
max
==
null
)
{
products
=
productRepository
.
findByProductNameContainingIgnoringCase
(
name
);
products
=
productRepository
.
findByProductNameContainingIgnoringCase
(
name
);
}
else
if
((
name
==
null
||
name
.
isEmpty
())
&&
(
min
!=
null
||
max
!=
null
))
{
}
else
if
((
name
==
null
||
name
.
isEmpty
())
&&
(
min
!=
null
||
max
!=
null
))
{
if
(
min
==
null
)
{
if
(
min
==
null
)
{
min
=
BigDecimal
.
ZERO
;
min
=
BigDecimal
.
ZERO
;
}
}
...
@@ -56,16 +54,45 @@ public class ProductController {
...
@@ -56,16 +54,45 @@ public class ProductController {
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
}
}
products
=
productRepository
.
findByPriceBetween
(
min
,
max
);
products
=
productRepository
.
findByPriceBetween
(
min
,
max
);
}
else
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
(
min
!=
null
||
max
!=
null
))
{
}
else
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
(
min
!=
null
||
max
!=
null
))
{
if
(
min
==
null
)
{
if
(
min
==
null
)
{
min
=
BigDecimal
.
ZERO
;
min
=
BigDecimal
.
ZERO
;
}
}
if
(
max
==
null
)
{
if
(
max
==
null
)
{
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
max
=
BigDecimal
.
valueOf
(
1000_000_000
);
}
}
products
=
productRepository
.
findByProductNameContainingIgnoringCaseAndPriceBetween
(
name
,
min
,
max
);
products
=
productRepository
.
findByProductNameContainingIgnoringCaseAndPriceBetween
(
name
,
min
,
max
);
}
}
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
return
"wyszukiwarka"
;
return
"wyszukiwarka"
;
}
}
@GetMapping
(
"/new"
)
public
String
newProduct
()
{
return
"product_form"
;
}
@GetMapping
(
"/{id}/edit"
)
public
String
editProduct
(
@PathVariable
int
id
,
Model
model
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
id
);
if
(
product
.
isPresent
())
{
model
.
addAttribute
(
"product"
,
product
.
get
());
return
"product_form"
;
}
else
{
model
.
addAttribute
(
"product_id"
,
id
);
return
"missing_product"
;
}
}
@PostMapping
({
"/new"
,
"/{id}/edit"
})
public
String
saveProduct
(
Model
model
,
Product
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.
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// i nie trzeba dodawać go w osobnym poleceniu.
productRepository
.
save
(
product
);
return
"product_form"
;
}
}
}
PC30-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
2adddba3
...
@@ -12,6 +12,8 @@ public class SecurityConfig {
...
@@ -12,6 +12,8 @@ public class SecurityConfig {
SecurityFilterChain
setHttpSecurity
(
HttpSecurity
httpSecurity
)
throws
Exception
{
SecurityFilterChain
setHttpSecurity
(
HttpSecurity
httpSecurity
)
throws
Exception
{
httpSecurity
.
authorizeHttpRequests
()
httpSecurity
.
authorizeHttpRequests
()
.
anyRequest
().
permitAll
()
.
anyRequest
().
permitAll
()
.
and
()
.
csrf
().
disable
()
;
;
return
httpSecurity
.
build
();
return
httpSecurity
.
build
();
}
}
...
...
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/product_form.jsp
0 → 100644
View file @
2adddba3
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/core"
%
>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Edycja towaru
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
>
</head>
<body>
<c:choose>
<c:when
test=
"${not empty(product.productId)}"
>
<h1>
Edycja produktu nr ${product.productId}
</h1>
</c:when>
<c:otherwise>
<h1>
Edycja nowego produktu
</h1>
</c:otherwise>
</c:choose>
<form
id=
"product-form"
method=
"post"
>
<table
class=
"form"
>
<tr>
<td><label
for=
"productId"
>
Numer:
</label></td>
<td><input
name=
"productId"
placeholder=
"brak"
type=
"number"
readonly=
"readonly"
value=
"${product.productId}"
/></td>
</tr>
<tr>
<td><label
for=
"productName"
>
Nazwa towaru:
</label></td>
<td><input
name=
"productName"
placeholder=
"nazwa..."
type=
"text"
value=
"${product.productName}"
/>
</td>
</tr>
<tr>
<td><label
for=
"price"
>
Cena:
</label></td>
<td><input
name=
"price"
placeholder=
"12.90"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.price}"
/>
</td>
</tr>
<tr>
<td><label
for=
"vat"
>
Stawka VAT:
</label></td>
<td><input
name=
"vat"
placeholder=
"0.23"
title=
"tu wpisz cenę"
type=
"number"
step=
"0.01"
value=
"${product.vat}"
/>
</td>
</tr>
<tr>
<td><label
for=
"description"
>
Opis:
</label></td>
<td><textarea
name=
"description"
rows=
"10"
cols=
"120"
>
${product.description}
</textarea></td>
</tr>
<tr>
<td><button>
Zapisz
</button></td>
</tr>
</table>
</form>
<c:if
test=
"${not empty(errors)}"
>
<div
class=
"error"
>
<h4>
Błędy:
</h4>
<ul>
<c:forEach
var=
"error"
items=
"${errors}"
>
<li>
${error}
</li>
</c:forEach>
</ul>
</div>
</c:if>
<c:if
test=
"${saved}"
>
<div
class=
"info"
>
Zapisano produkt nr ${product.productId}
</div>
</c:if>
<p><a
href=
"/products"
>
przejdź do listy produktów
</a></p>
<p><a
href=
"/"
>
strona główna
</a></p>
</body>
</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