본문 바로가기

Spring

ViewResolver

참고: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-viewresolver

 

 

ViewResolver란?

Spring MVC의 DispatcherServlet은 뷰 관련 처리를 ViewResolver에 위임했다.

ViewResolver는 문자열로 반환된 뷰 네임을 확인한 후 적절한 뷰를 찾아준다.

 

View Resolution

DispatcherServlet는 HandlerAdaptor의 handle 메소드를 호출하여 요청을 처리한다. 그리고 결과로 ModelAndView 객체를 전달받는다.

ModelAndView는 말그대로 model과 view를 프로퍼티로 들고있는 객체이다.

이 때 ModelAndView.view는 View객체일 수도, 혹은 String으로 된 뷰 네임일 수도 있다. (개인적으로 주로 후자를 많이 접했다.)

ModelAndView를 받으면 아래와 같이 사전작업을 한다. (소스는 spring mvc 5.1.9를 보면서 적었다)

 

  1. ModelAndView객체는 있는데 내부에 view가 null이면, 디폴트 뷰네임을 지정한다. 이 때 사용하는 빈은 ViewNameTranslator이고, HttpServletRequest를 참고하여 디폴트 뷰네임을 결정한다. 이 빈이 null이면 디폴트 뷰네임은 지정되지 않는다
  2. handler의 인터셉터들의 postHandle 메소드를 호출한다. 이게 view랑 무슨 상관이 있길래 언급한걸까? 바로, 인터셉터 postHandle 메소드에서 view의 clear() 메소드를 호출하여 view rendering을 막을 수도 있기 때문이다.
  3. DispatcherServlet은 processDispatchResult 메소드를 호출하여 view 혹은 exception 처리를 마무리한다. 이 단계에서 viewResolver 빈들이 사용된다. redirect, jsp 등의  뷰네임 처리가 이때 이뤄진다. ViewResolver는 자신이 처리할 수 있으면 View객체를 반환하고, 처리할 수 없다면 null을 반환한다. HandlerMapping 리스트 처럼 처음 View객체를 반환하는 ViewResolver에 의해 뷰가 결정된다.

 

 

ViewResolver 종류

ViewResolver의 종류에 대해서 알아보자.

 

링크로 달아둔 공식문서에서 그냥 긁어왔다.

AbstractCachingViewResolver

Sub-classes of AbstractCachingViewResolver cache view instances that they resolve. Caching improves performance of certain view technologies. You can turn off the cache by setting the cache property to false. Furthermore, if you must refresh a certain view at runtime (for example, when a FreeMarker template is modified), you can use the removeFromCache(String viewName, Locale loc) method.

XmlViewResolver

Implementation of ViewResolver that accepts a configuration file written in XML with the same DTD as Spring’s XML bean factories. The default configuration file is /WEB-INF/views.xml.

ResourceBundleViewResolver

Implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle base name. For each view it is supposed to resolve, it uses the value of the property [viewname].(class) as the view class and the value of the property [viewname].url as the view URL. You can find examples in the chapter on View Technologies.

UrlBasedViewResolver

Simple implementation of the ViewResolver interface that affects the direct resolution of logical view names to URLs without an explicit mapping definition. This is appropriate if your logical names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings.

InternalResourceViewResolver

Convenient subclass of UrlBasedViewResolver that supports InternalResourceView (in effect, Servlets and JSPs) and subclasses such as JstlView and TilesView. You can specify the view class for all views generated by this resolver by using setViewClass(..). See the UrlBasedViewResolver javadoc for details.

FreeMarkerViewResolver

Convenient subclass of UrlBasedViewResolver that supports FreeMarkerView and custom subclasses of them.

ContentNegotiatingViewResolver

Implementation of the ViewResolver interface that resolves a view based on the request file name or Accept header. See Content Negotiation.

 

참고로 DispatcherServlet은 디폴트로 InternalResourceViewResolver 빈을 등록한다.

근거: DispatcherServlet은 초기화를 위한 설정 파일명을 자기자신이 들고 있는데, 파일명은 DispatcherServlet.properties 이다.

프로퍼티명은 DEFAULT_STRATEGIES_PATH 이다.

'Spring' 카테고리의 다른 글

RequestParam  (0) 2019.07.02
ResponseEntity  (0) 2019.06.30
RequestMapping  (0) 2019.06.29
HandlerMapping  (0) 2019.06.27
Resource Handler  (0) 2019.06.24