ModelAndView객체는 Model객체와 데이터를 넘길 페이지값을 가진 return을 합친 것입니다.
즉, 데이터와 넘길 페이지의 값을 모두 가지고 있는 객체입니다.
@Controller
public class TestModelAndView {
@RequestMapping("models/modelAndView")
public ModelAndView testModelAndView(){
ModelAndView mv = new ModelAndView();
mv.addObject("name", "이름");
mv.setViewName("/sample/modelAndView");
return mv;
}
}
혹은 생성자에서 ModelAndView("/sample/modelAndView") 해주어도 됩니다.
return mv하게되면 ViewResolver를 거쳐서 View를 보여주게됩니다.
ViewResolver는
Controller가 넘긴 View이름을 통해 알잡은 view를 찾는 역할을 합니다.
Controller는 ModelAndView객체에 응답할 view이름을 넣어 넘기면
DispatchServlet은 ViewResolver에게 응답한 view를 요청한다
ViewResolver는 View이름을 이용해 알맞은 viwe객체를 찾아서 DispatchResolver에게 전달한다.
viewResolver를 context에 선언해주어야한다.
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
'Programming > Spring' 카테고리의 다른 글
Spring 진행과정 (0) | 2018.02.14 |
---|---|
root-context & servlet-context (0) | 2018.02.14 |
파일 업로드/ 다운로드 (0) | 2018.02.09 |
MariaDB Auto_Increment (0) | 2018.02.08 |
DAO, DTO, VO (0) | 2018.02.08 |