This tutorial shows steps for creating Spring MVC Project in Eclipse Using Maven. As you know that Maven and Eclipse are using widely in java programming. The example will show step by step with the illustrated pictures. This tutorial explains about generating PDF and excel documents in Spring web mvc using iText PDF library. It uses the content negotiation view resolver provided by spring to generate PDF and excel views. Here we will be creating a simple spring mvc web application using annotations and java configs.
- Jsp Servlet Tutorial Using Eclipse Pdf CLICK HEREexample Servlets will be used to create a sample servlet in Run the servlet in Eclipse on Tomcat and view the results. Export a WAR from determine which Servlet (or JSP page) is. Maven EJB3 and JPA Example. This article (updated for Eclipse Luna).
- Free eBook “Maven by Example” is an introduction to Apache Maven which uses a set of examples to demonstrate core concepts.This book introduces Maven by developing some real examples and walking you through the structure of those examples providing motivation and explanation along the way.
- In our example, we will use our iTextHelloWorld.pdf file generated previously. Once we load the file using PdfReader, we need to create a PdfStamper which is used to apply additional content to file like metadata. Full examples can be found in the Maven based project over on GitHub.
- Maven is an automation and management tool developed by Apache Software Foundation. It was initially released on 13 July 2004. In Yiddish language the meaning of Maven is 'accumulator of knowledge'. It is written in Java Language and used to build and manage projects written in C#, Ruby, Scala,.
- Maven by Example by Tim O'Brien, at al. Publisher: Sonatype, Inc. 2010 Number of pages: 176. Description: This work is the first attempt at a comprehensive title on Maven. It builds upon the combined experience and work of the authors of all previous Maven titles, and you should view it not as a finished work but as the first edition in a long line of updates to follow.
This tutorial demonstrates how to create a PDF view using Spring MVC and itext. Out of the box spring only supports the older com.lowagie:itext
library. The newest com.itextpdf:itext
library is not supported by default. But there is a work around, which we’ll show you in the following example.
Maven Dependencies
We manage our application using Apache Maven. Add the following dependencies to your pom.xml
and maven will automatically manage and resolve the dependencies. Note: we added two dependencies for creating PDF documents. The first com.lowagie:itext
is the older open source library, which is by default supported by Spring. The second library com.itextpdf:itext
is not supported by default and you’ll need a licence to run this in production.
Spring MVC PDF View Configuration
The ServletInitializer
is a replacement for the web.xml
servlet descriptor. This class extends from the AbstractAnnotationConfigDispatcherServletInitializer
. By overriding the getServletConfigClasses()
we instruct where the spring configuration files are located.
Next, we show how to configure spring using Java Configuration. Annotate the class with @Configuration
to mark this class as a configuration file. We can override the configureContentNegotiation(..)
method and use the ContentNegotiationConfigurer
to configure the content negotiation preferences.
The configureViewResolvers(..)
method is where we register the views for our application. You can either enable the ItextPdfView()
or the LowagiePdfView
which we’ll create later on in this tutorial.
The equivalent Spring MVC XML Configuration is located below:
Creating the controller
The CourseController
adds some data to the Model
which we’ll display on the views.
Spring MVC PDF Lowagie View
The older com.lowagie:itext
library is supported by default. Create a new class and extend from the AbstractPdfView
. We can create the PDF Document using the buildPdfDocument(..)
method.
Spring MVC PDF IText Example
The new com.itextpdf:itextpdf
library is not supported by default. We can resolve this by first creating an abstract class and extending from the AbstractView
. In this class we set the correct content type for our document. Next, we create an abstract method that’ll be used for creating the PDF document. Finally, we override and implement the renderMergedOutputModel(..)
method that’ll write the pdf document to the response.
Create a new class and extend from the previously created AbstractItextPdfView
and override the buildPdfDocument(..)
method to create your PDF document.
HTML View
To demonstrate the content negotiation we also created a simple HTML view to display the courses
attributes which we added to the Model
.
Demo
URL: http://localhost:8081/spring-mvc-pdf/
URL: http://localhost:8081/spring-mvc-pdf/index.pdf
References
Download
Free eBook “Maven by Example” is an introduction to Apache Maven which uses a set of examples to demonstrate core concepts.This book introduces Maven by developing some real examples and walking you through the structure of those examples providing motivation and explanation along the way.
Description
Starting with a simple Maven project which contains a single class and a single unit test, this book slowly develops an enterprise multi-module project which interacts with a database, interacts with a remote API, and presents a simple web application.
Table of Contents
- Introducing Apache Maven
- Installing Maven
- A Simple Maven Project
- Customizing a Maven Project
- A Simple Web Application
- A Multi-module Project
- Multi-module Enterprise Project
- Optimizing and Refactoring POMs
Book Details
Maven By Example Pdf
Format(s): PDF
File size: 1.30 MB
Number of pages: 164
Link: Download