모델 정보를 HTTP 세션에 저장해주는 애노테이션
SessionStatus를 사용해서 세션 처리 완료를 알려줄 수 있다.
HttpSession을 직접 사용
@Controller
public class SampleController {
@GetMapping("/events/form")
public String eventsForm(Model model, HttpSession httpSession) {
Event newEvent = new Event();
newEvent.setLimit(50);
model.addAttribute("event", newEvent);
httpSession.setAttribute("event", newEvent);
return "events/form";
}
@PostMapping("/events")
public String createEvent(@ModelAttribute @Valid Event event, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "/events/form";
}
// 데이터베이스에서 save 했다고 가정
return "redirect:/events/list";
}
@GetMapping("/events/list")
public String getEvents(Model model){
Event event = new Event();
event.setName("spring");
event.setLimit(10);
// 데이터베이스에서 읽어왔다고 가정한다.
List<Event> eventList = new ArrayList<>();
eventList.add(event);
model.addAttribute("eventList", eventList);
return "/events/list";
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void eventForm() throws Exception{
MockHttpServletRequest request = mockMvc.perform(get("/events/form"))
.andDo(print())
.andExpect(view().name("/events/form"))
.andExpect(model().attributeExists("event"))
.andExpect(request().sessionAttribute("event", notNullValue()))
.andReturn().getRequest();
Object event = request.getSession().getAttribute("event");
System.out.println(event);
}
@Test
public void createEvent() throws Exception{
ResultActions result = mockMvc.perform(post("/events")
.param("name", "spring)
.param("limit", "-10"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().hasErrors());
ModelAndView mav = result.andReturn().getModelAndView();
Map<String, Object> model = mav.getModel();
System.out.println(model.size());
}
}
@SessionAttributes 사용
@Controller
@SessionAttributes("event")
public class SampleController {
@GetMapping("/events/form")
public String eventsForm(Model model) {
Event newEvent = new Event();
newEvent.setLimit(50);
model.addAttribute("event", newEvent);
// 자동으로 세션에 넣어줌
return "events/form";
}
@PostMapping("/events")
public String createEvent(@ModelAttribute @Valid Event event, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "/events/form";
}
// 데이터베이스에서 save 했다고 가정
return "redirect:/events/list";
}
@GetMapping("/events/list")
public String getEvents(Model model){
Event event = new Event();
event.setName("spring");
event.setLimit(10);
// 데이터베이스에서 읽어왔다고 가정한다.
List<Event> eventList = new ArrayList<>();
eventList.add(event);
model.addAttribute("eventList", eventList);
return "/events/list";
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void eventForm() throws Exception{
MockHttpServletRequest request = mockMvc.perform(get("/events/form"))
.andDo(print())
.andExpect(view().name("/events/form"))
.andExpect(model().attributeExists("event"))
.andExpect(request().sessionAttribute("event", notNullValue()))
.andReturn().getRequest();
Object event = request.getSession().getAttribute("event");
System.out.println(event);
}
}
@SessionAttributes + SessionStatus
@Controller
@SessionAttributes("event")
public class SampleController {
@GetMapping("/events/form")
public String eventsForm(Model model) {
Event newEvent = new Event();
newEvent.setLimit(50);
model.addAttribute("event", newEvent);
// 자동으로 세션에 넣어줌
return "events/form";
}
@PostMapping("/events")
public String createEvent(@ModelAttribute @Valid Event event, BindingResult bindingResult, SessionStatus sessionStatus){
if(bindingResult.hasErrors()){
return "/events/form";
}
// 데이터베이스에서 save 했다고 가정
sessionStatus.setComplete();
return "redirect:/events/list";
}
@GetMapping("/events/list")
public String getEvents(Model model){
Event event = new Event();
event.setName("spring");
event.setLimit(10);
// 데이터베이스에서 읽어왔다고 가정한다.
List<Event> eventList = new ArrayList<>();
eventList.add(event);
model.addAttribute("eventList", eventList);
return "/events/list";
}
}
참고