Friday 25 October 2013

view resolvers in spring mvc

Spring provides view resolvers, which enable you to render models in a browser without tying you to a specific view technology. Out of the box, Spring enables you to use JSPs, Velocity templates and XSLT views

The two interfaces which are important to the way Spring handles views are ViewResolver and View.

The ViewResolver provides a mapping between view names and actual views.

The View interface addresses the preparation of the request and hands the request over to one of the view technologies.

AbstractCachingViewResolver--
An abstract view resolver which takes care of caching views. Often views need preparation before they can be used, extending this view resolver provides caching of views.

XmlViewResolver--
An 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--
An implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle basename. The bundle is typically defined in a properties file, located in the classpath. The default file name is views.properties.

UrlBasedViewResolver--
A simple implementation of the ViewResolver interface that effects the direct resolution of symbolic view names to URLs, without an explicit mapping definition. This is appropriate if your symbolic names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings.

InternalResourceViewResolver--
A convenience subclass of UrlBasedViewResolver that supports InternalResourceView(i.e. Servlets and JSPs), and subclasses such as JstlView and TilesView. The view class for all views generated by this resolver can be specified via setViewClass(..). See the Javadocs for the UrlBasedViewResolver class for details.

VelocityViewResolver/FreeMarkerViewResolver--
A convenience subclass of UrlBasedViewResolver that supports VelocityView (i.e. Velocity templates) orFreeMarkerView respectively and custom subclasses of them.
Ex:

when using JSP for a view technology you can use the UrlBasedViewResolver. This view resolver translates a view name to a URL and hands the request over to the RequestDispatcher to render the view

    <bean id="viewResolver"  
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
When mixing different view technologies in a web application, you can use the ResourceBundleViewResolver


    <bean id="viewResolver"  
class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="views"/>
    <property name="defaultParentView value="parentView"/>
</bean>





 
 
 
 












1 comment:

  1. Nice Collection of view resolver. UrlBasedViewResolver can also be used in java config as
    @Bean
    public UrlBasedViewResolver urlBasedViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/views/");
    resolver.setSuffix(".jsp");
    resolver.setCache(false);
    resolver.setViewClass(JstlView.class);
    return resolver;
    }

    Find the link.

    ReplyDelete