Commit 80be96ed by Patryk Czarnik

Inicjalizacja sesji w listenerze

parent 99f48cf0
package sklep.basket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import jakarta.servlet.http.HttpSessionListener;
@Configuration
public class BasketConfig {
{ System.out.println("BasketConfig jest tworzony"); }
@Bean
HttpSessionListener createListener() {
return new BasketListener();
}
}
package sklep.basket;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
public class BasketListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setMaxInactiveInterval(30); // sesja wygasa po pół minuty - tylko na potrzeby zajęć
session.setAttribute("basket", new Basket());
System.out.println("Początek sesji " + session.getId());
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("Koniec sesji " + se.getSession().getId());
}
}
......@@ -109,10 +109,6 @@ public class ProductController {
HttpSession session) {
Basket basket = (Basket) session.getAttribute("basket");
if(basket == null) {
basket = new Basket();
session.setAttribute("basket", basket);
}
Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) {
basket.addProduct(product.get());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment