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 !!
Hi Lokesh,
How are you? I hope well!
I have a question about a problem related with xmlViewResolver.
The question is about changing the location of the view.xml file to another folder, instead WEB-INF could be src/main/resources/. This is because I’m changing to spring boot in a jar, so there will be no WEB-INF folder when I deploy this jar. The thing is that I created config java class with the bean xmlViewResolver but when I set the location with a resource loader and I try to use this way of mapping to generate a excel file via location.href it says error 404.
Thank you in advance for anything you could think of.
Fabio
I have a question. for the id of bean and the jsp page can be defferent or not?
for example with the bean id home and /WEB-INF/jsp/home.jsp. the bean id home replace or rename by home1 or not?
I am developing a spring rest service. I would like to remove the fields with null values on the fly using spring configuration while returning response. Please let me know how can we achieve this, but no use.
I added the below config in context xml:
In comment box, please put your code inside [java] … [/java] OR [xml] … [/xml] tags otherwise it may not appear as intended.