Commit e88099ad by Patryk Czarnik

Inicjalizacja sesji za pomocą listenera

parent 15241479
package sklep.basket;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BasketConfiguration {
// Spring utworzy obiekt klasy BasketConfiguration i wykona metodę getSessionListener.
// Uzyskany w ten sposób obiekt HttpSessionListener zostanie zarejestrowany jako listener sesji,
// a zatem metoda sessionCreated będzie wykonywana za każdym razem, gdy w aplikacji będzie powstawać nowa sesja
// (gdy nowy klient wejdzie na stronę).
@Bean
public HttpSessionListener getSessionListener() {
return new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setAttribute("basket", new Basket());
}
};
}
}
......@@ -138,10 +138,6 @@ public class ProductController {
@PathVariable("id") int productId,
HttpSession sesja) {
Basket basket = (Basket) sesja.getAttribute("basket");
if(basket == null) {
basket = new Basket();
sesja.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