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
3d440089
Commit
3d440089
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
@SessionAttribute i HttpSessionListener
parent
5ba4f3c4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
7 deletions
+38
-7
BasketConfiguration.java
...pring/src/main/java/sklep/basket/BasketConfiguration.java
+37
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+1
-7
No files found.
PC27-SklepSpring/src/main/java/sklep/basket/BasketConfiguration.java
0 → 100644
View file @
3d440089
package
sklep
.
basket
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.servlet.http.HttpSessionEvent
;
import
jakarta.servlet.http.HttpSessionListener
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
BasketConfiguration
{
// Klasa typu Configuration działa tak:
// - na starcie aplikacji Spring tworzy jeden obiekt tej klasy
// - szuka metod z adnotacją @Bean i je wywołuje
// - wyniki tych metod zapamiętuje jako "beany"
// - jeśli typ wynikowy takiej metody jest specjalnym typem, który "coś dla springa znaczy",
// to może wpłynąć na działanie aplikacji
// Tak jest w tym przypadku: HttpSessionListener będzie używany do obserwacji tworzonych i usuwanych sesji.
@Bean
public
HttpSessionListener
createSesionListener
()
{
// Zwracam obiekt klasy anonimowej; obiekt zgodny z interfejsem HttpSessionListener
return
new
HttpSessionListener
()
{
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
);
}
public
void
sessionDestroyed
(
HttpSessionEvent
se
)
{
HttpSession
sesja
=
se
.
getSession
();
System
.
out
.
println
(
"sessionDestroyed "
+
sesja
.
getId
());
}
};
}
}
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
3d440089
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
;
...
@@ -91,14 +90,9 @@ public class ProductController {
...
@@ -91,14 +90,9 @@ public class ProductController {
@GetMapping
(
"/{id}/add-to-basket"
)
@GetMapping
(
"/{id}/add-to-basket"
)
public
String
addToBasket
(
public
String
addToBasket
(
@PathVariable
(
"id"
)
int
productId
,
@PathVariable
(
"id"
)
int
productId
,
HttpSession
sesja
)
{
@SessionAttribute
Basket
basket
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
if
(
product
.
isPresent
())
{
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
if
(
basket
==
null
)
{
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
.
get
());
basket
.
addProduct
(
product
.
get
());
}
}
return
"redirect:/products"
;
return
"redirect:/products"
;
...
...
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