Spring MVC XmlViewResolver Example with JstlView and RedirectView
April 04, 2015
In this page we will provide Spring MVC XmlViewResolver example with JstlView and RedirectView. XmlViewResolver handles views defined in XML. This helps to achieve view definition in a separate XML file that we can put at our required location. We need to refer this location in XmlViewResolver using setLocation() method. In our example, we using java configuration file to create the bean of XmlViewResolver . Find the complete example.
Project Structure in Eclipse
Find the project structure in eclipse.Create XML View with JstlView and RedirectView
We need to create an XML file at our required location and define the URL using JstlView and RedirectView.JstlView : The view which uses JSP standard tag library (JSTL).
RedirectView : The view that redirects the absolute or context relative URL.
Find the XML view used in our demo.
user-view.xml
<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="success" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/views/success.jsp" /> </bean> <bean id="home" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://concretepage.com" /> </bean> </beans>
Java Configuration for XmlViewResolver
XmlViewResolver uses an XML file as view definition in which we define inner and external URLs with the help of JstlView and RedirectView. Default XML file location is "/WEB-INF/views.xml". But we can assign any location. To configure view location, XmlViewResolver provides XmlViewResolver.setLocation() method. Find the definition of XmlViewResolver.AppConfig.java
package com.concretepage.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.servlet.view.XmlViewResolver; @Configuration @ComponentScan("com.concretepage") public class AppConfig { @Bean public XmlViewResolver xmlViewResolver() { XmlViewResolver resolver = new XmlViewResolver(); Resource resource = new ClassPathResource("user/user-view.xml"); resolver.setLocation(resource); return resolver; } }
Create Controller
Create a controller class.WelcomeController.java
package com.concretepage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/myworld") public class WelcomeController { @RequestMapping("/welcome") public String hello(Model model) { model.addAttribute("msg", "XmlViewResolver Demo"); return "success"; } @RequestMapping("/home") public String getInfo() { return "home"; } }
Create JSP
Find the JSP used in our demo.success.jsp
<html> <head> <title>Spring MVC</title> </head> <body> <h1>${msg}</h1> </body> </html>
Output
To test the application, download code and run gradle as gradle clean build. Deploy war file in tomcat.1. Find the URL to test JstlView.
http://localhost:8080/concretepage-1/myworld/welcome
http://localhost:8080/concretepage-1/myworld/home
As an output http://www.concretepage.com/ will open.