Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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_20230928
Commits
a8285ec0
Commit
a8285ec0
authored
Nov 07, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basket - podejście z listenerem sesji
parent
e9c2002b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
5 deletions
+33
-5
BasketConfiguration.java
...pring/src/main/java/sklep/basket/BasketConfiguration.java
+32
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+1
-5
No files found.
PC30-SklepSpring/src/main/java/sklep/basket/BasketConfiguration.java
0 → 100644
View file @
a8285ec0
package
sklep
.
basket
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.servlet.http.HttpSessionEvent
;
import
jakarta.servlet.http.HttpSessionListener
;
@Configuration
public
class
BasketConfiguration
{
// Adnotacja Configuration powoduje, że:
// - Spring tworzy obiekt tej klasy (BasketConfiguration)
// - dla każdej meody oznaczonej @Bean uruchamia tę metodę, a jej wynik rejestruje jako bean
// - gdy typ wynikowy "znaczy dla Springa coś specjalnego", to Spring weźmie to pod uwagę,
// właśnie w ten sposób często podaje się Springowi elementy konfiguracji
@Bean
HttpSessionListener
createListener
()
{
return
new
HttpSessionListener
()
{
@Override
public
void
sessionCreated
(
HttpSessionEvent
se
)
{
HttpSession
sesja
=
se
.
getSession
();
// sesja.setMaxInactiveInterval(30); // pół minuty i sesja wygasa
// System.out.println("sessionCreated " + sesja.getId());
Basket
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
};
}
}
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
a8285ec0
...
@@ -128,12 +128,8 @@ public class ProductController {
...
@@ -128,12 +128,8 @@ public class ProductController {
HttpSession
sesja
)
{
HttpSession
sesja
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
if
(
product
.
isPresent
())
{
// w tej wersji
stosujemy leniwą inicjalizację
// w tej wersji
zakładamy, że listener przygotował pusty koszyk
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
if
(
basket
==
null
)
{
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
.
get
());
basket
.
addProduct
(
product
.
get
());
}
else
{
}
else
{
System
.
err
.
println
(
"Nieznany produkt dodawany do koszyka: "
+
productId
);
System
.
err
.
println
(
"Nieznany produkt dodawany do koszyka: "
+
productId
);
...
...
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