[Spring] viewResolver return types...

ITWeb/개발일반 2020. 7. 21. 10:45

[참고문서]

https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#mvc-ann-return-types

https://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s03.html

 

Spring mvc framework 에서 Controller 에서 사용하는 return type 에 따른 viewResolver 적용방법이 다릅니다.

위 문서를 보시면 이해 하실 수 있습니다.

 

가장 많이 사용 하는 몇 개만 뽑아 왔습니다.

 

  • A ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A View object, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • A String value that is interpreted as the logical view name, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).
  • If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters. See the section called “Mapping the response body with the @ResponseBody annotation”.

제가 실수한 부분은 void 로 선언을 해 놓고 template 수정 후 반영이 되지 않아 cache 를 의심 했었는데 역시 원인은 제가 선언을 잘 못 했기 때문 이였습니다.

 

void 로 선언 시 해석은 

@GetMapping("/hello")
public void helloworld() {...}

hello.html 을 template 으로 찾게 됩니다.

 

: