Sponsored Links
Ad by Google
In this tutorial we are going to show you the usage of ResourceBundleViewResolver, In our previous post we have seen a simple step by step Spring MVC Hello world application here. The ResourceBundleViewResolver is an implementation of View Resolver interface and uses bean definitions in ResourceBundle, specified by the bundle basename. The default location of bundle is in Class Path and the default name of the bundle is view.properties file. The ResourceBundleViewResolver will also helps you to achieved the internationalization.
In this tutorial we are going to show you a simple implementation example of ResourceBundleViewResolver, where we will return excel file as a view instead of jsp,jstl.
Below is the out put of the application, It will download excel file, when you complete this example by following the step by step implementation given below.
Main Objects of this project are:
Below are the list of dependencies required for this project:
Step 1. Create a Maven Project:
Step A: Go to File->New->Other..
Step B: Select Maven Project from the select wizard.
Step C: Select project name and location from New Maven Project wizard.
Step D: Configure project, provide GroupId, artifactId, project name etc. See the screenshot we have selecting packaging type as war because we are going to develop web project.
After completion of all the above steps now your project will look like this screen shot.
2. Add project dependencies into pom.xml file:
Double click on your project's pom.xml file, it will open pom.xml file having very limited information. Add required Spring dependencies into pom.xml below is the complete entry of pom.xml file
pom.xml
Step 3: Create WEB-INF folder inside webapp, and create web.xml inside WEB-INF folder.
Here is a complete web.xml
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 4: Create views.properties file inside src/main/resources folder and add the below entry inside views.properties.
views.properties
view-resolver-servlet.xml
Here is a complete index.jsp code place this code in your index.jsp page.
Step 8: Create ExcelBuilderForResourceBundleViewResolver.java, This class will generate excel file.
Finally Right click on Project and select Run As->Run On Server
URL: http://localhost:8080/ResourceBundleExample/
That's it :)
In this tutorial we are going to show you a simple implementation example of ResourceBundleViewResolver, where we will return excel file as a view instead of jsp,jstl.
Below is the out put of the application, It will download excel file, when you complete this example by following the step by step implementation given below.
Tools and Technologies we are using here:
- JDK 7
- Spring 4.1.1
- Eclipse Kepler 4.3
- Maven 3.2
Main Objects of this project are:
- pom.xml
- web.xml
- 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 For excel file generation
Step 1. Create a Maven Project:
Step A: Go to File->New->Other..
Step B: Select Maven Project from the select wizard.
Step C: Select project name and location from New Maven Project wizard.
Step D: Configure project, provide GroupId, artifactId, project name etc. See the screenshot we have selecting packaging type as war because we are going to develop web project.
After completion of all the above steps now your project will look like this screen shot.
2. Add project dependencies into pom.xml file:
Double click on your project's pom.xml file, it will open pom.xml file having very limited information. Add required Spring dependencies into pom.xml below is the complete entry of pom.xml file
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>ResourceBundleExample</artifactId> <version>1.0</version> <packaging>war</packaging> <name>ResourceBundleExample</name> <description>ResourceBundle 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>ResourceBundleExample</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 3: Create WEB-INF folder inside webapp, and create web.xml inside WEB-INF folder.
Here is a complete web.xml
<?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 ResourceBundle 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 4: Create views.properties file inside src/main/resources folder and add the below entry inside views.properties.
views.properties
excel.(class)=com.javamakeuse.springmvc.util.ExcelBuilderForResourceBundleViewResolverStep 5: Create view-resolver-servlet.xml parallel to web.xml and place the below code.
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.ResourceBundleViewResolver"> <property name="basename" value="views" /> </bean> </beans>Step 6: Create index.jsp Right click on webapp folder then New->JSP File and provide name as index.jsp
Here is a complete index.jsp code place this code in your index.jsp page.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 4 MVC ResourceBundleViewResolver Example</title> </head> <body> <div style="width: 200px; margin: auto;"> <p> Click <a href="downloadExcel.htm">here</a> to download excel file, using ResourceBundleViewResolver. </p> </div> </body> </html>Step 7: Create 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="downloadExcel") public ModelAndView getMessageForResourceBundle(HttpServletResponse response){ response.setHeader("Content-Disposition", "attachment; filename=\"MyReport.xls\""); return new ModelAndView("excel"); } }
Step 8: Create ExcelBuilderForResourceBundleViewResolver.java, This class will generate excel file.
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/ResourceBundleExample/
That's it :)
Download the complete example from here Source Code
Sponsored Links
0 comments:
Post a Comment