Spring MVC View Resolvers

Spring Interview Questions and Answers Q1 - Q14 are FAQs

Q1 - Q4 Overview & DIP Q5 - Q8 DI & IoC Q9 - Q10 Bean Scopes Q11 Packages Q12 Principle OCP Q14 AOP and interceptors
Q15 - Q16 Hibernate & Transaction Manager Q17 - Q20 Hibernate & JNDI Q21 - Q22 read properties Q23 - Q24 JMS & JNDI Q25 JDBC Q26 Spring MVC Q27 - Spring MVC Resolvers

Q. What is a Spring MVC ViewResolver?
A. In Spring MVC,  ViewResolver is a strategy and factory object used to map logical view names to actual view resources. The interface for ViewResolver is shown below. The single method resolveViewName(..) maps a logical view name together with the request locale to a View instance.The interface defines the following single method.

public interface ViewResolver {
  View resolveViewName(String name, Locale loc);
}


The ViewResolvers can be defined in your Spring context file as shown below.

<beans ... >
...
  <bean class="org.springframework.web.servlet.view.
               InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
  </bean>

</beans>


Q. Can you write custom resolvers?
A. Yes, you can. Spring provides the following resolvers.

  • 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) or FreeMarkerView respectively and custom subclasses of them.

If you have specific requirements, you can define your own.Here is an example where a custom view resolver is defined to use both .jsp and .html files.

-->
Step 1: The Spring context file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

 <mvc:annotation-driven />

 <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="classpath:/applicationPortal.properties" />
  <property name="placeholderPrefix" value="$ap{" />
  <property name="placeholderSuffix" value="}" />
 </bean>

 <context:annotation-config />

 <context:component-scan base-package="com.myapp.portal.controller" />

 <bean class="com.myapp.portal.resolver.AppPortalViewResolver">
  <property name="htmlResolver" ref="htmlViewResolver" />
  <property name="jspResolver" ref="jspViewResolver" />
 </bean>

 <bean id="htmlViewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".html"></property>
 </bean>

 <bean id="jspViewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView"></property>
  <property name="prefix" value="/WEB-INF"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>


</beans>



Step 2: The custom resolver Java class.

package com.myapp.portal.resolver;

import java.util.Locale;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class AppPortalViewResolver implements ViewResolver {
 
 
 private ViewResolver htmlResolver;
 private ViewResolver jspResolver;
 
 

 public void setHtmlResolver(ViewResolver htmlResolver) {
  this.htmlResolver = htmlResolver;
 }

 public void setJspResolver(ViewResolver jspResolver) {
  this.jspResolver = jspResolver;
 }


 @Override
 public View resolveViewName(String viewName, Locale locale) throws Exception {
  if (viewName.startsWith("/pages")) {
            return jspResolver.resolveViewName(viewName, locale);
        } else {
            return htmlResolver.resolveViewName(viewName, locale);
        }
 }

}


-->

No comments:

Post a Comment