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
03 | <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" /> |
05 | <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" /> |
08 | class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> |
11 | class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
12 | <property name="prefix"> |
13 | <value>/WEB-INF/</value> |
15 | <property name="suffix"> |
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
07 | class="org.springframework.web.servlet.view.JstlView"> |
08 | <property name="url" value="/WEB-INF/helloWorld.jsp" /> |
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
03 | <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" /> |
05 | <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" /> |
08 | class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> |
10 | <bean class="org.springframework.web.servlet.view.XmlViewResolver"> |
11 | <property name="location"> |
12 | <value>/WEB-INF/views.xml</value> |
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
1 | helloworld.(class)=org.springframework.web.servlet.view.JstlView |
2 | helloworld.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
03 | <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" /> |
05 | <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" /> |
08 | class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> |
10 | <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> |
11 | <property name="basename" value="views" /> |
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
03 | <context:component-scan base-package="com.javacodegeeks.snippets.enterprise" /> |
05 | <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" /> |
08 | class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> |
11 | class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
12 | <property name="prefix"> |
13 | <value>/WEB-INF/</value> |
15 | <property name="suffix"> |
18 | <property name="order" value="2" /> |
21 | <bean class="org.springframework.web.servlet.view.XmlViewResolver"> |
22 | <property name="location"> |
23 | <value>/WEB-INF/views.xml</value> |
25 | <property name="order" value="1" /> |
28 | <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> |
29 | <property name="basename" value="views" /> |
30 | <property name="order" value="0" /> |
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:
This was an example of Spring MVC View Resolvers.
No comments:
Post a Comment