Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 378
Manish Avatar asked Jan 07 '23 20:01

Manish


1 Answers

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.
like image 64
Ralph Avatar answered Feb 22 '23 22:02

Ralph