Spring MVC XML View Resolver Example

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.

2. Configure the views file in dispatcher servlet

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.



4. Add a Controller to handle the requests


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
Best Spring MVC Tutorial for Beginners with Examples

Spring MVC XML View Resolver | Spring MVC Sample

Download Full Code – SpringMVC-XMLViewResolver.zip