Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
f115bfc8
Commit
f115bfc8
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Zapis danych formularza - oddzielne parametry
parent
0d9be63c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
1 deletions
+37
-1
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+28
-0
Product.java
PC30-SklepSpring/src/main/java/sklep/model/Product.java
+7
-0
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+2
-1
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
f115bfc8
package
sklep
.
controller
;
package
sklep
.
controller
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
...
@@ -9,6 +10,7 @@ import org.springframework.stereotype.Controller;
...
@@ -9,6 +10,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
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
...
@@ -55,4 +57,30 @@ public class ProductController {
...
@@ -55,4 +57,30 @@ public class ProductController {
}
}
}
}
@PostMapping
({
"/products/new"
,
"/products/{id}/edit"
})
public
String
saveProduct
(
Integer
productId
,
String
productName
,
BigDecimal
price
,
BigDecimal
vat
,
String
description
,
Model
model
)
{
// W tej wersji dane z wypełnionego formularza odbieramy w postaci oddzielnych parametrów metody.
// Na podstawie przysłanych parametrów tworzę obiekt Product
Product
product
=
new
Product
();
product
.
setProductId
(
productId
);
product
.
setProductName
(
productName
);
product
.
setPrice
(
price
);
product
.
setVat
(
vat
);
product
.
setDescription
(
description
);
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
productRepository
.
save
(
product
);
System
.
out
.
println
(
"Produkt po zapisie : "
+
product
);
// Aby w formularzu było widać dane produktu, dodajemy go też do modelu
model
.
addAttribute
(
"product"
,
product
);
return
"product_form"
;
}
}
}
PC30-SklepSpring/src/main/java/sklep/model/Product.java
View file @
f115bfc8
...
@@ -82,4 +82,10 @@ public class Product implements Serializable {
...
@@ -82,4 +82,10 @@ public class Product implements Serializable {
this
.
vat
=
vat
;
this
.
vat
=
vat
;
}
}
@Override
public
String
toString
()
{
return
"Product [productId="
+
productId
+
", description="
+
description
+
", price="
+
price
+
", productName="
+
productName
+
", vat="
+
vat
+
"]"
;
}
}
}
\ No newline at end of file
PC30-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
f115bfc8
...
@@ -13,7 +13,8 @@ public class SecurityConfig {
...
@@ -13,7 +13,8 @@ public class SecurityConfig {
@Bean
@Bean
SecurityFilterChain
setHttpSecurity
(
HttpSecurity
httpSecurity
)
throws
Exception
{
SecurityFilterChain
setHttpSecurity
(
HttpSecurity
httpSecurity
)
throws
Exception
{
httpSecurity
.
authorizeRequests
()
httpSecurity
.
authorizeRequests
()
.
anyRequest
().
permitAll
();
.
anyRequest
().
permitAll
()
.
and
().
csrf
().
disable
();
return
httpSecurity
.
build
();
return
httpSecurity
.
build
();
}
}
...
...
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