Spring MVC XML View Resolver provides a way to configure the view URL’s in a single XML file. The location of the file is defined in the dispatcher servlet.xml. If we do not define any location in the dispatcher servlet.xml, by default the file is picked from the WEB-INF/views.xml
Lets see a simple example of the Spring MVC XML View Resolver.
1. my-views.xml file
Create a my-views.xml and store all the view urls in that file. You can use any name for the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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/myHome.jsp" /> </bean> <bean id="news" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsp/news/myNews.jsp" /> </bean> </beans> |
2. Configure the views file in dispatcher servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>/WEB-INF/my-views.xml</value> </property> </bean> </beans> |
3. Add the JSP files
Add the 2 jsp files that you configured in the my-views.xml file.
As you may have noticed I have the 2 jsps in different folders. This was done for you to understand that we can give any valid URL for the views.
myHome.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - XML View Resolver Sample</title> </head> <body> <h2>This is My Home Page</h2> <h3>Thanks for visiting my home page !!!!!!!!! </h3> </body> </html> |
myNews.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - XML View Resolver Sample</title> </head> <body> <h2>This is My News Page</h2> <h3>No News for Today !!! </h3> </body> </html> |
4. Add a Controller to handle the requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class XMLViewResolverController { @RequestMapping(value = "/showHome") public String showHome() { return "home"; } @RequestMapping(value = "/showNews") public String showNews() { return "news"; } } |
Output
When you call http://localhost:8080/sampleproject/showHome , the Controller method showHome() is called which returns “home”.
Now the XML View Resolver checks for the bean with id =”home” and then returns the associated url with it.
Same applies when we use the http://localhost:8080/sampleproject/showNews