0
Sponsored Links


Ad by Google
Sometimes you need to configure multiple view resolver in Spring MVC, instead of returning simple jsp or jstl as a view. We may need to return jsp,jstl,excel etc from the same/different controller based on the request. So here we are going to show you How to configure multiple view resolver in Spring MVC, we have already seen What is view resolver? and How many types of view resolver in Spring MVC with example in our previous post here.

There are three different implementation of ViewResolver in Spring MVC and those are listed below.

  1. InternalResourceViewResolver [Example]
  2. ResourceBundleViewResolver [Example]
  3. XmlViewResolver [Example]
We have already explained about all three ViewResolvers in our previous post, so here we are not going to dig into the deep, but yes we are going to configure each of them in our this tutorial, and of course at the end of this tutorial, we have also provided download able link of source code, so that you can easily download and import it into your eclipse. Below is the out put of this tutorial.

Let's start implementation by following the below given simple steps.

Tools and Technologies we are using here:

  • JDK 7
  • Spring 4.1.1
  • Eclipse Kepler 4.3
  • Maven 3.2
Overview of the Project Structure:

Main Objects of this project are:
  • pom.xml
  • web.xml
  • views.xml
  • views.properties
  • view-resolver-servlet.xml
  • ViewResolverController.java

Below are the list of dependencies required for this project:
  • spring-core
  • spring-web
  • spring-webmvc
  • jstl
  • javax.servlet-api
  • poi
Step 1. Create Maven project and add above dependencies into pom.xml, to create step by step maven project follow any of our previous post, below is the complete pom.xml entry.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javamakeuse.springmvc</groupId>
 <artifactId>ViewResolverExample</artifactId>
 <version>1.0</version>
 <packaging>war</packaging>
 <name>ViewResolverExample</name>
 <description>View Resolver Example</description>

 <!-- Dependencies -->
 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
  </dependency>
  <!-- poi library for excel generation -->
  <dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.7</version>
  </dependency>
 </dependencies>

 <build>
  <finalName>view-resolver-example</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Step 2. Create web.xml inside WEB-INF folder.

Here is complete web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>Spring MVC View Resolver</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>view-resolver</servlet-name>    <!-- Servlet name can be anything -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- Known as front controller in spring -->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>view-resolver</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
</web-app>

In web.xml we used DispatcherServlet, DispatcherServlet is known as front controller in Spring MVC. You can read more about front controller from one of our previous post Spring front controller.

Step 3: Create views.properties inside class path.

Here is a complete views.properties
excel.(class)=com.javamakeuse.springmvc.util.ExcelBuilderForResourceBundleViewResolver

Step 4: Create views.xml inside class path.

Here is a complete views.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="home"
        class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/pages/xml-home.jsp" />
    </bean>
 </beans>

Step 5: Create view-resolver-servlet.xml

Here is a complete view-resolver-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 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-4.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 <context:component-scan base-package="com.javamakeuse.springmvc" />

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
   <property name="order" value="2" />
 </bean>
 <bean id="viewResolver2" class="org.springframework.web.servlet.view.XmlViewResolver">
  <property name="location">
   <value>/WEB-INF/views.xml</value>
  </property>
   <property name="order" value="1" />
 </bean>

 <bean id="viewResolver3"
  class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
  <property name="order" value="0" />
  <property name="basename" value="views" />
 </bean>

</beans>  

Step 6: Create index.jsp inside webapp folder.

index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC View Resolver Example</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>
   Click <a href="viewResolver1.htm">here</a> to return another view by using InternalResourceViewResolver.
  </p>
  <p>
   Click <a href="viewResolver2.htm">here</a> to return another view by using XMLViewResolver.
  </p>
  <p>
   Click <a href="viewResolver3.htm">here</a> to download excel file, using ResourceBundleViewResolver.
  </p>
 </div>
</body>
</html>

Step 7. Create home.jsp inside WEB-INF/pages folder

home.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC InternalResourceViewResolver</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>${msg}</p>
 </div>
</body>
</html>

Step 8. Create xml-home.jsp inside WEB-INF/pages folder

xml-home.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC XmlViewResolver</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>${msg}</p>
 </div>
</body>
</html>

Step 9. Create ViewResolverController.java

ViewResolverController.java
package com.javamakeuse.springmvc;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewResolverController {

 @RequestMapping(value = "viewResolver1")
 public ModelAndView getViewForInternalResourceViewResolver() {
  String msg = "This message is @ home.jsp page";
  return new ModelAndView("home", "msg", msg);
 }

 @RequestMapping(value = "viewResolver2")
 public ModelAndView getViewForXmlViewResolver() {
  String msg = "This message is @ xml-home.jsp page";
  return new ModelAndView("xml-home", "msg", msg);
 }

 @RequestMapping(value = "viewResolver3")
 public ModelAndView getViewForResourceBundle(HttpServletResponse response) {
  response.setHeader("Content-Disposition",
    "attachment; filename=\"MyReport.xls\"");
  return new ModelAndView("excel");
 }
}

Step 10. Create ExcelBuilderForResourceBundleViewResolver.java to generate excel file

ExcelBuilderForResourceBundleViewResolver.java
package com.javamakeuse.springmvc.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class ExcelBuilderForResourceBundleViewResolver extends AbstractExcelView {


 @Override
 protected void buildExcelDocument(Map<String, Object> model,
   HSSFWorkbook workbook, HttpServletRequest request,
   HttpServletResponse response) throws Exception {

  try {
   HSSFSheet sheet = workbook.createSheet("sheet 1");
   sheet.setDisplayGridlines(false);

   HSSFRow row = sheet.createRow(1);
   HSSFCell cell = row.createCell(1);
   cell.setCellValue("Using ResourceBundleViewResolver");

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

Finally Right click on Project and select Run As->Run On Server

URL: http://localhost:8080/view-resolver-example/

Download the complete example from here Source Code

Sponsored Links

0 comments:

Post a Comment