Resolving views in Spring MVC by R4R Team

Resolving views in Spring MVC:-

All MVC frameworks for web applications provide a way to address views. 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 that 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.

Resolving views with the ViewResolver interface:

After completion of request processing by a controller, we need to redirect it to a view. all handler methods in the Spring Web MVC controllers must resolve to a logical view name, either explicitly (e.g., by returning a String, View, or ModelAndView) or implicitly (i.e., based on conventions). Views in Spring are addressed by a logical view name and are resolved by a view resolver. We can have multiple view resolvers chained in the configuration files.

1 AbstractCachingViewResolver: Extending this resolver provides ability to cache the views before actually calling the views.

2 XMLViewResolver: Takes view configuration in xml format compliant with DTD of Spring’s bean factory. Default configuration is searched in WEB-INF/views.xml.

3 ResourceBundleViewResolver: Definitions are searched in resource bundle i.e. property files. Default classpath property file is searched with name views.properties.

4 UrlBasedViewResolver: Straightforward url symbol mapping to view.

5 InternalResourceViewResolver: Subclass of UrlBasedViewResolver that supports JSTL and Tiles view resolving.

6 VelocityViewReolver: Subclass of UrlBasedViewResolver used to resolve velocity views.

7 FreeMarkerViewResolver: Subclass of UrlBasedViewResolver used to resolve FreeMarker views.

As an example, with JSP as 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>

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!