Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
5ba4f3c4
Commit
5ba4f3c4
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basket - HttpSession
parent
474e279a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletions
+34
-1
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+18
-0
products.jsp
...klepSpring/src/main/webapp/WEB-INF/templates/products.jsp
+16
-1
No files found.
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
5ba4f3c4
package
sklep
.
controller
;
package
sklep
.
controller
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.validation.Valid
;
import
jakarta.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.validation.BindingResult
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
sklep.basket.Basket
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
sklep.util.PhotoUtil
;
import
sklep.util.PhotoUtil
;
...
@@ -86,6 +88,22 @@ public class ProductController {
...
@@ -86,6 +88,22 @@ public class ProductController {
}
}
}
}
@GetMapping
(
"/{id}/add-to-basket"
)
public
String
addToBasket
(
@PathVariable
(
"id"
)
int
productId
,
HttpSession
sesja
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
if
(
basket
==
null
)
{
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
.
get
());
}
return
"redirect:/products"
;
}
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
public
String
szukaj
(
Model
model
,
public
String
szukaj
(
Model
model
,
String
name
,
String
name
,
...
...
PC27-SklepSpring/src/main/webapp/WEB-INF/templates/products.jsp
View file @
5ba4f3c4
...
@@ -9,6 +9,20 @@
...
@@ -9,6 +9,20 @@
</head>
</head>
<body>
<body>
<c:if
test=
"${not empty basket and not empty basket.elements}"
>
<div
class=
"basket"
>
<h4>
Koszyk
</h4>
<ul>
<c:forEach
var=
"p"
items=
"${basket.elements}"
>
<li>
${p.productName}: ${p.quantity} × ${p.price} =
<b>
${p.value}
</b></li>
</c:forEach>
</ul>
<p>
Wartość koszyka: ${basket.totalValue}
</p>
</div>
</c:if>
<h1>
Wszystkie produkty
</h1>
<h1>
Wszystkie produkty
</h1>
<c:forEach
var=
"product"
items=
"${products}"
>
<c:forEach
var=
"product"
items=
"${products}"
>
...
@@ -18,7 +32,8 @@
...
@@ -18,7 +32,8 @@
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<p
class=
"product-description"
>
${product.description}
</p>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
</div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/add-to-basket"
>
Dodaj do koszyka
</a></div>
</div>
</c:forEach>
</c:forEach>
<div><a
href=
"/products/new"
>
Dodaj nowy produkt
</a></div>
<div><a
href=
"/products/new"
>
Dodaj nowy produkt
</a></div>
...
...
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