Spring mvc-Integration (integration)-Generate PDF sample (reprint practice)

Source: Internet
Author: User
Tags tomcat server tutorialspoint

The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_pdf.htm

Description: The sample is based on spring MVC 4.1.6.

The following example shows how to generate a PDF using the Spring WEB MVC framework. First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:

Step Description
1 Create a project named TestWeb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section.
2 Create a Java class Userpdfview,pdfcontroller under the Com.tutorialspoint package.
3 Download the Itext library itext from the MAVEN repository page. Put it in your classpath.
4 The final step is to create the contents of all the source and configuration files and export the application as described below.

Pdfcontroller.java

 PackageCom.tutorialspoint;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController; Public classPdfcontrollerextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {//User DataMap<string,string> UserData =NewHashmap<string,string>(); Userdata.put ("1", "Mahesh"); Userdata.put ("2", "Suresh"); Userdata.put ("3", "Ramesh"); Userdata.put ("4", "Naresh"); return NewModelandview ("Usersummary", "UserData", UserData); }}

Userexcelview.java

 PackageCom.tutorialspoint;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.view.document.AbstractPdfView;Importcom.lowagie.text.Document;Importcom.lowagie.text.Table;ImportCom.lowagie.text.pdf.PdfWriter; Public classUserpdfviewextendsAbstractpdfview {protected voidBuildpdfdocument (map<string, object>model, document document, PDFWriter PDFWriter, httpservletrequest request, httpservletresponse response)throwsException {Map<String,String> UserData = (map<string,string>) model.get ("UserData"); Table Table=NewTable (2); Table.addcell ("Roll No"); Table.addcell ("Name");  for(Map.entry<string, string>Entry:userData.entrySet ())         {Table.addcell (Entry.getkey ());      Table.addcell (Entry.getvalue ());   } document.add (table); }}

Testweb-servlet.xml

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd ">   <Beanclass= "Org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />   <Beanclass= "Com.tutorialspoint.PDFController" />   <Beanclass= "Org.springframework.web.servlet.view.XmlViewResolver">      < Propertyname= "Location">         <value>/web-inf/views.xml</value>      </ Property>   </Bean></Beans>

Views.xml

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"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 Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ">   <BeanID= "Usersummary"class= "Com.tutorialspoint.UserPDFView"></Bean></Beans>

Here we have created a pdfcontroller and a userpdfview. The Itext library processes the PDF file format and converts the data into PDF documents.

After you finish creating the source files and profiles, export the application. Right-click the application and use the Export > WAR file option and save your testweb.war file in the Tomcat WebApps folder.

Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try the URL http://localhost:8080/TestWeb/pdf, you should see the following results.

Maven Example:

Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test31

Spring mvc-Integration (integration)-Generate PDF sample (reprint practice)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.