Deprecaiation Warning:
The XmlViewResolver class has been deprecated as of 5.3, in favor of Spring’s common view resolver variants and/ or custom resolver implementations such as InternalResourceViewResolver, ContentNegotiatingViewResolver and BeanNameViewResolver.
In Spring MVC-based applications, the last step of request processing is to return the logical view name. Here DispatcherServlet has to delegate control to a view template so the information is rendered. This view template decides which view should be rendered based on the returned logical view name.
These view templates are one or more view resolver beans declared in the web application context. These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them. Spring MVC comes with several ViewResolver implementations. In this example, we will look at such a view resolver template i.e. XmlViewResolver.
Contrary to InternalResourceViewResolver where each logical view name is mapped to the physical location of the view directly, in the case of XmlViewResolver, views are declared as Spring beans. You can declare the view beans in the same configuration file as the web application context, but it’s better to isolate them in a separate configuration file.
1. XML Configuration
By default,
XmlViewResolver loads view beans from the file ‘/WEB-INF/views.xml
‘, but this location can be overridden through the location property.
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/admin-views.xml</value>
</property>
</bean>
In the views.xml
configuration file, you can declare each view as a normal Spring bean by setting the class name and properties. e.g.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="home" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/jsp/home.jsp" />
</bean>
<bean id="admin/home" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/jsp/admin/home.jsp" />
</bean>
<bean id="logOffRedirect" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="home" />
</bean>
</beans>
In the above configuration, the first two beans are pretty obvious. Logical view name “home” is mapped to “/WEB-INF/jsp/home.jsp” and “admin/home” is mapped to “/WEB-INF/jsp/admin/home.jsp“.
The third bean does not map any physical view file, rather it redirects the request to URL “home” which is handled by the controller of URL “/home”. Whatever logical name that controller will return, that view will be looked up into bean mappings and then the actual view file will be obtained.
2. Java Configuration
If we are using the Java configurations, we can define the XmlViewResolver bean as follows:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.XmlViewResolver;
@Configuration
@ComponentScan(basePackages = "com.example.springmvc")
public class SpringMvcConfig {
@Bean
public ViewResolver xmlViewResolver() {
XmlViewResolver resolver = new XmlViewResolver();
resolver.setLocation("/WEB-INF/views.xml");
return resolver;
}
}
Just make that a views.xml
file is present in the specified location (/WEB-INF/views.xml
) to define the application views.
Drop me your queries if something needs more explanation.
Happy Learning !!
Comments