/
CheckoutController.java
87 lines (73 loc) · 3.01 KB
/
CheckoutController.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.apress.prospringmvc.bookstore.web.controller;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.apress.prospringmvc.bookstore.domain.Account;
import com.apress.prospringmvc.bookstore.domain.Cart;
import com.apress.prospringmvc.bookstore.domain.Order;
import com.apress.prospringmvc.bookstore.service.BookstoreService;
import com.apress.prospringmvc.bookstore.validation.OrderValidator;
/**
*
* @author Marten Deinum
* @author Koen Serneels
*/
@Controller
@SessionAttributes(types = { Order.class })
@RequestMapping("/cart/checkout")
public class CheckoutController {
private final Logger logger = LoggerFactory.getLogger(CheckoutController.class);
@Autowired
private Cart cart;
@Autowired
private BookstoreService bookstoreService;
@ModelAttribute("countries")
public Map<String, String> countries(Locale currentLocale) {
Map<String, String> countries = new TreeMap<String, String>();
for (Locale locale : Locale.getAvailableLocales()) {
countries.put(locale.getCountry(), locale.getDisplayCountry(currentLocale));
}
return countries;
}
@RequestMapping(method = RequestMethod.GET)
public void show(HttpSession session, Model model) {
Account account = (Account) session.getAttribute(LoginController.ACCOUNT_ATTRIBUTE);
Order order = this.bookstoreService.createOrder(this.cart, account);
model.addAttribute(order);
}
@RequestMapping(method = RequestMethod.POST, params = "order")
public String checkout(SessionStatus status, @Validated @ModelAttribute Order order, BindingResult errors) {
if (errors.hasErrors()) {
return "cart/checkout";
} else {
this.bookstoreService.store(order);
status.setComplete(); //remove order from session
this.cart.clear(); // clear the cart
return "redirect:/index.htm";
}
}
@RequestMapping(method = RequestMethod.POST, params = "update")
public String update(@ModelAttribute Order order) {
order.updateOrderDetails();
return "cart/checkout";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setValidator(new OrderValidator());
}
}