package com.github.ryoasai.springmvc.example.flashmap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
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;
@Controller
@RequestMapping(value="/account")
@SessionAttributes("account")
public class AccountController {
private Map<Long, Account> accounts = new ConcurrentHashMap<Long, Account>();
@RequestMapping(method=RequestMethod.GET)
public String getCreateForm(Model model) {
model.addAttribute(new Account());
return "account/createForm";
}
@RequestMapping(method=RequestMethod.POST)
public String create(@Valid Account account, BindingResult result, Model model, SessionStatus sessionStatus) {
if (result.hasErrors()) {
return "account/createForm";
}
this.accounts.put(account.assignId(), account);
sessionStatus.setComplete();
model.addAttribute("message", "An account of id = " + account.getId()+ " was created.");
return "redirect_with_flash:/account/" + account.getId();
}
@RequestMapping(value="{id}", method=RequestMethod.GET)
public String getView(@PathVariable Long id, Model model) {
Account account = this.accounts.get(id);
if (account == null) {
throw new ResourceNotFoundException(id);
}
model.addAttribute(account);
return "account/view";
}
}