Manish
Manish

Reputation: 1517

Usage of different ViewResolver in Spring Mvc

I am a beginner in spring-mvc. While going through the view resolvers, I am able to understand that how to use following view resolvers:

BeanNameViewResolver,InternalResourceViewResolver and UrlBasedViewResolver

I have already gone through the google to understand but still i am not clear about their pros and cons over each other.

How one should decide when to use which view Resolver.

If someone can help me in understanding that,it would be a great help.

Thanks,

Upvotes: 4

Views: 3674

Answers (2)

Ralph
Ralph

Reputation: 120831

InternalResourceViewResolver is a subclass of UrlBasedViewResolver.

UrlBasedViewResolver and InternalResourceViewResolver are often used in MVC application where the controller return the name of the view that should been rendered. The controller return an logical name of the view, and the resolver made it a file name (of the jsp), by adding some pre - and postfix. For example: logical view name return by the controller: main/example, prefix: /WEB-INF/pages/, postfix: .jsp -> /WEB-INF/pages/main/example.jsp gets rendered with the model-data provided by the controller

  • The UrlBasedViewResolver needs a View class (like the most other ViewResolvers too). (very brif: The view class is responsible for rendering, while the resolver is responsible to pick the right template/...) The view used in UrlBasedViewResolver has to be an subclass of AbstractUrlBasedView.

  • The InternalResourceViewResolver is convenient subclass of UrlBasedViewResolver that has be default already a configured view: InternalResourceView (or JstlView when Jstl is present). So it is the right resolver when JSPs are used as template engine.

There are other AbstractUrlBasedView implementations, for example for JasperReports, Freemaker, Velocity, Tiles, .... Most of them has a convenient subclass of UrlBasedViewResolver too.

  • BeanNameViewResolver very very old resolvers, from the old Spring 2.0 time. At this time each controller was for handling one URL. At this time there was no @RequestMapping annotation, and one has to tell Spring which controller was for which url. One could list them all, or have this Resolver, that was able to map url->controller by the controller name. Since Spring 3.0 (more exact since 2.5) and Springs annotation support, this resolver is used very rarely.

Upvotes: 7

Aniket Thakur
Aniket Thakur

Reputation: 68975

When your Spring application loads there is default ViewResolver that gets loaded - InternalResourceViewResolver. You can refer to the default beans that gets initialized in DisplacherServlet.properties in spring-webmvc jar In case of View Resolvers it is -

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

which essentially returns a JSTL view.

There are lot of URLBasedViewResolvers like -

  1. InternalResourceViewResolver
  2. VelocityViewReolver
  3. FreeMarkerViewReolver
  4. ThymeleafViewResolver
  5. XsltViewReolver

Yup you read it right. InternalResourceViewResolver is a convenient subclass of UrlBasedViewResolver.

If you want to know more about these View Resolvers see - http://www.studytrails.com/frameworks/spring/spring-mvc-view-resolver.jsp

NOTE : Important point to note in case of chaining of view resolvers. In the chain Spring moves on to next View resolver only if current View resolver returns null. Some URLBasedViewResolvers (Tiles, Velocity,Freemarker) check if resource exist and return null. So they can be anywhere in the view resolver chain. Others must be last (JSTL/JSP, XSLT, JSON)

Upvotes: 2

Related Questions