-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseController.java
More file actions
59 lines (49 loc) · 2.01 KB
/
BaseController.java
File metadata and controls
59 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package onlineshop.controllers;
import jakarta.servlet.http.HttpSession;
import onlineshop.Cart;
import onlineshop.merchandise.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
@Controller
public class BaseController {
protected static final String MESSAGE = "message";
protected static final String SHOW_MESSAGE = "showMessage";
@Autowired
Cart cart;
/**
* Looks up the requested parameter in the session. If it doesn't exist, it uses the default value.
* @param session {@link jakarta.servlet.http.HttpSession}
* @param paramName {@link String}
* @param paramValue {@link Object}
* @param defaultValue {@link Object}
* @return sessionValue {@link Object}
*/
protected Object getSessionParam(HttpSession session,
String paramName,
Object paramValue,
Object defaultValue) {
if (paramValue == null) {
Object sessionValue = session.getAttribute(paramName);
paramValue = sessionValue != null ? sessionValue : defaultValue;
}
session.setAttribute(paramName, paramValue);
return paramValue;
}
/**
* Loads the cart items from the cart object and stores the corresponding attributes in the view view.
* @param view {@link Model}
*/
protected void loadCartItems(Model view) {
List<CartItem> cartItems = cart.getItems();
view.addAttribute("cartItems", cartItems);
view.addAttribute("numOfCartItems", cart.getNumOfItems());
view.addAttribute("grandTotal", cart.getGrandTotal());
}
protected void showMessage(RedirectAttributes atts, String message) {
atts.addFlashAttribute(SHOW_MESSAGE, true);
atts.addFlashAttribute(MESSAGE, message);
}
}