Spring Mvc Download Excel

Spring Mvc Download Excel Average ratng: 7,5/10 6922 reviews
  • Spring Core Basics
  • Spring Questions and Answers

Spring MVC 4 File Download Example. Download a file in Spring MVC Application by writing it's content to HttpServletResponse output stream, setting required headers. The following example shows how to generate Excel using the Spring Web MVC Framework. To begin with, let us have a working Eclipse IDE in place and stick to the following steps to develop a Dynamic Form based Web Application using the Spring Web Framework. Aug 15, 2015  Spring MVC 4 File Download Example. Download a file in Spring MVC Application by writing it's content to HttpServletResponse output stream, setting required headers. Spring MVC offers seamless integration with different view technologies, including Excel document view. When configured properly, a Spring’s view resolver can generate an Excel document from model data and send it to the client for downloading or opening by a spreadsheet program like Microsoft Excel. In this tutorial we demonstrate how to create an Excel view using Spring MVC. By utilizing Spring MVC Content negotiation we can have multiple views of the same resource. This example shows how to create an excel view, either in.xls and.xlsx format. When working with large excel documents it is profitable to use the streaming xlsx view.

  • Spring Useful Resources
  • Selected Reading

The Spring Web MVC framework provides Model-View-Controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.

  • The Model encapsulates the application data and in general they will consist of POJO.

  • The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.

  • The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

The DispatcherServlet

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses. The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram −

Following is the sequence of events corresponding to an incoming HTTP request to DispatcherServlet

The program is also used as. Simple GIF animations may also be produced on MSWLogo version 6.5 with the command gifsave. It is, with available, in.MSWLogo supports multiple, and allows input from ports. It also supports a windows interface, so (I/O) is available through this GUI, and keyboard and mouse events can trigger interrupts. Microsoft turtle logo.

  • After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.

  • The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.

  • The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request.

  • Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser.

All the above-mentioned components, i.e. HandlerMapping, Controller, and ViewResolver are parts of WebApplicationContext w which is an extension of the plainApplicationContext with some extra features necessary for web applications.

Required Configuration

You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the web.xml file. The following is an example to show declaration and mapping for HelloWebDispatcherServlet example −

The web.xml file will be kept in the WebContent/WEB-INF directory of your web application. Upon initialization of HelloWeb DispatcherServlet, the framework will try to load the application context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INFdirectory. In this case, our file will be HelloWebservlet.xml.

Next, <servlet-mapping> tag indicates what URLs will be handled by which DispatcherServlet. Here all the HTTP requests ending with .jsp will be handled by the HelloWeb DispatcherServlet.

If you do not want to go with default filename as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF, you can customize this file name and location by adding the servlet listener ContextLoaderListener in your web.xml file as follows −

Now, let us check the required configuration for HelloWeb-servlet.xml file, placed in your web application's WebContent/WEB-INF directory −

Following are the important points about HelloWeb-servlet.xml file −

  • The [servlet-name]-servlet.xml file will be used to create the beans defined, overriding the definitions of any beans defined with the same name in the global scope.

  • The <context:component-scan..> tag will be use to activate Spring MVC annotation scanning capability which allows to make use of annotations like @Controller and @RequestMapping etc.

  • The InternalResourceViewResolver will have rules defined to resolve the view names. As per the above defined rule, a logical view named hello is delegated to a view implementation located at /WEB-INF/jsp/hello.jsp .

The following section will show you how to create your actual components, i.e., Controller, Model, and View.

Defining a Controller

The DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controllerannotation indicates that a particular class serves the role of a controller. The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

The @Controller annotation defines the class as a Spring MVC controller. Here, the first usage of @RequestMapping indicates that all handling methods on this controller are relative to the /hello path. Next annotation@RequestMapping(method = RequestMethod.GET) is used to declare theprintHello() method as the controller's default service method to handle HTTP GET request. You can define another method to handle any POST request at the same URL.

You can write the above controller in another form where you can add additional attributes in @RequestMapping as follows −

The value attribute indicates the URL to which the handler method is mapped and the method attribute defines the service method to handle HTTP GET request. The following important points are to be noted about the controller defined above −

  • You will define required business logic inside a service method. You can call another method inside this method as per requirement.

  • Based on the business logic defined, you will create a model within this method. You can use setter different model attributes and these attributes will be accessed by the view to present the final result. This example creates a model with its attribute 'message'.

  • A defined service method can return a String, which contains the name of the view to be used to render the model. This example returns 'hello' as logical view name.

Creating JSP Views

Spring MVC supports many types of views for different presentation technologies. These include - JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, Atom and RSS feeds, JasperReports, etc. But most commonly we use JSP templates written with JSTL.

Let us write a simple hello view in /WEB-INF/hello/hello.jsp −

Spring Mvc Ajax Download Excel

Here ${message} is the attribute which we have set up inside the Controller. You can have multiple attributes to be displayed inside your view.

Spring Web MVC Framework Examples

Based on the above concepts, let us check few important examples which will help you in building your Spring Web Applications −

Sr.No.Example & Description
1Spring MVC Hello World Example

This example will explain how to write a simple Spring Web Hello World application.

2Spring MVC Form Handling Example

This example will explain how to write a Spring Web application using HTML forms to submit the data to the controller and display a processed result.

3Spring Page Redirection Example

Learn how to use page redirection functionality in Spring MVC Framework. Sap business objects license cost.

4Spring Static Pages Example

Learn how to access static pages along with dynamic pages in Spring MVC Framework.

5Spring Exception Handling Example

Learn how to handle exceptions in Spring MVC Framework.

In this tutorial we demonstrate how to create an Excel view using Spring MVC. By utilizing Spring MVC Content negotiation we can have multiple views of the same resource. This example shows how to create an excel view, either in .xls and .xlsx format. When working with large excel documents it is profitable to use the streaming xlsx view. The streaming view uses less memory and can improve performance of large excel documents.

Maven Dependencies

We use Apache Maven to manage our project. Add the following dependencies to your project and Maven will resolve them automatically. To create excel documents we are using apache POI. For creating .xls excel documents, you need the org.apache.poi:poi dependency. When you want to create .xlsx excel documents, you need the org.apache.poi:poi-ooxml dependency. Make sure these are on the classpath.

Spring MVC Excel View Configuration

The configureViewResolvers method registers the views we are using in this example. We’ll create these views later in this tutorial. Things to notice is we registered both the XlsxView and XlsxStreamingView, buth only one of these can be active because both are mapped to the same file extension, request parameter and HTTP Accept header.

This is the equivalent Spring XML configuration as above.

We need to register the DispatcherServlet to serve the request to the correct controller methods.

Create the Controller Endpoint

This Course POJO is used to add data to the Excell document.

The CourseController adds a list of Course instances to the Model, which will be used to display on the Excel document.

Excel Views

There are 2 file formats in which we can create an Excel document. The .xls is the old format, the .xlsx is the new format which is XML based. We are using apache POI to create excel files, when creating .xls documents make sure the org.apache.poi:poi dependency is on the classpath. When working with .xlsx files, you need the org.apache.poi:poi-ooxml dependency.

Changing the name of the excel document

When you want to change the name of the downloaded excel document, you need to set the Content-Disposition header using the HttpServletResponse#setHeader('Content-Disposition', 'attachment; filename='filename.xls'); method.

Xls Excel using AbstractXlsView

The first view we create is the XlsView which extends from AbstractXlsView. You create the excel document by overriding the buildExcelDocument, the rest is self explanatory.

Xlsx Excel using AbstractXlsxView

The second view is 99% the same code. But instead of extending the AbstractXlsView, we extend from the AbstractXlsxView. This class will take care of the generation of the Excel document in .xlsx format.

XLSX Streaming View

Spring Mvc Excel Download Example

The last Excel view we create is the XlsxStreamingView which extends from the AbstractXlsxStreamingView. This can improve the performance of large excel documents. But when you look at the javadoc of the SXSSFWorkbook, notice that some clients are incompatible with this style of streaming. That being said, when you need to process large excel documents, this is the way to go.

Web View

The default view we registered is the text/html. This view will be displayed using the same URI. Using either HTTP Accept header, a specific request parameter or correct file extension will change the view that’ll be displayed.

Demo

URL: http://localhost:8081/spring-mvc-excel-view/courses

URL: http://localhost:8081/spring-mvc-excel-view/courses.xls or http://localhost:8081/spring-mvc-excel-view/courses?type=xls

Mime type of .xls: application/vnd.ms-excel

URL: http://localhost:8081/spring-mvc-excel-view/courses.xlsx or http://localhost:8081/spring-mvc-excel-view/courses?type=xlsx

Mime type of .xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

References

Microsoft Mvc Download

Download

Spring Mvc Download Excel 2018

Download it – spring mvc excel view example

Spring Mvc Download Excel Free

Share