Wednesday 23 January 2019

Spring MVC View Resolver Example-xml based

https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-view-resolver-example/


6. InternalResourceViewResolver

The InternalResourceViewResolver maps the jsp and html files in the WebContent/WEB-INF/ folder. It allows us to set properties such as prefix or suffix to the view name to generate the final view page URL. It is configured as shown below in mvc-dispatcher-servlet.xml.
mvc-dispatcher-servlet.xml
02
03    <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />
04      
05   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
06    
07 <bean
08   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
09    
10    <bean
11        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
12        <property name="prefix">
13            <value>/WEB-INF/</value>
14        </property>
15        <property name="suffix">
16            <value>.jsp</value>
17        </property>
18    </bean>
19
20</beans>
When the Controller returns the "helloworld" view, the InternalResourceViewResolver will create the url of the view making use of the prefix and suffix properties that are set to it, and will map the "helloworld" view name to the correct "helloworld" view.

7. XmlViewResolver

XmlViewResolver is an implementation of ViewResolver that accepts a configuration file written in XML, where the view implementation and the url of the jsp file are set. Below is the configuration file, views.xml.
views.xml
03    xsi:schemaLocation="http://www.springframework.org/schema/beans
05  
06    <bean id="helloWorld"
07        class="org.springframework.web.servlet.view.JstlView">
08        <property name="url" value="/WEB-INF/helloWorld.jsp" />
09    </bean>
10  
11</beans>
The resolver is defined in mvc-dispatcher-servlet.xml. It provides a property to configure, which is the location property, and there the path of the configuration file is set.
mvc-dispatcher-servlet.xml
02
03    <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />
04      
05   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
06    
07 <bean
08   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
09   
10    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
11        <property name="location">
12            <value>/WEB-INF/views.xml</value>
13        </property>
14    </bean>
15
16</beans>
Now, when the Controller returns the "helloworld" view, the XmlViewResolver will make use of the views.xml file to get the view class and the url of the view that will be mapped to the name "helloworld".

8. ResourceBundleViewResolver

The ResourceBundleViewResolver uses bean definitions in a ResourceBundle, that is specified by the bundle basename. The bundle is typically defined in a properties file, located in the classpath. Below is the views.properties file.
views.properties
1helloworld.(class)=org.springframework.web.servlet.view.JstlView
2helloworld.url=/WEB-INF/helloworld.jsp
The ResourceBundleViewResolver is defined in mvc-dispatcher-servlet.xml, and in its definition the basename property is set to view.properties file.
mvc-dispatcher-servlet.xml
02
03    <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />
04      
05   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
06    
07 <bean
08   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
09   
10    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
11        <property name="basename" value="views" />
12    </bean>
13
14</beans>
So, in this case, when the Controller returns the "helloworld" view, the ResourceBundleViewResolver will make use of the views.properties file to get the view class and the url of the view that will be mapped to the name "helloworld".

9. Configure multiple View Resolvers together

In order to set multiple Resolvers together in the same configuration file, you can set the order property in all definitions, so that the order that they are used will be defined, as shown below:
mvc-dispatcher-servlet.xml
02
03    <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" />
04      
05   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
06    
07 <bean
08   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
09    
10    <bean
11        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
12        <property name="prefix">
13            <value>/WEB-INF/</value>
14        </property>
15        <property name="suffix">
16            <value>.jsp</value>
17        </property>
18        <property name="order" value="2" />
19    </bean>
20
21    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
22        <property name="location">
23            <value>/WEB-INF/views.xml</value>
24        </property>
25        <property name="order" value="1" />
26    </bean>
27
28    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
29        <property name="basename" value="views" />
30        <property name="order" value="0" />
31    </bean>
32
33</beans>
Note that the InternalResourceViewResolver has the lowest priority, because it can map any request to the correct view, so if set before other resolvers the other resolvers will never be used.
In any of the four steps above, you can run your application, using a tomcat server and the result will be the one below:
multipleresolvers

This was an example of Spring MVC View Resolvers.
Download the Eclipse project of this tutorial: SpringMVCViewResolversExample

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...