Spring3 Web MVC 集成Jasper Report生成PDF例子

jopen 11年前發布 | 51K 次閱讀 Spring3 Spring MVC Web框架

Spring3 Web MVC 集成JasperReport生成PDF例子

一:環境搭建與配置

1.      安裝JDK6以上版本

2.      安裝STS, 下載地址如下:http://www.springsource.org/downloads/sts-ggts

3.      下載并安裝Tomcat7

4.      創建一個Dynamic Web Project項目,然后選擇創建好的項目右鍵選擇

Configuration->convert to Manve Project.

5.      添加web.xml文件,在WEB-INF目錄下新建reports, pages, classes三個子目錄

6.      新建index.jsp文件在webapp目錄下。

7.      最終的項目目錄結構如下:

Spring3 Web MVC 集成Jasper Report生成PDF例子

二:Spring配置詳解

在web.xml中配置Spring的DispatchServlet與Context Listener,配置如下:

Spring3 Web MVC 集成Jasper Report生成PDF例子

在express-servlet.xml中配置spring view解析器

Spring3 Web MVC 集成Jasper Report生成PDF例子

三:Jasper Report配置詳解

在jasper-views.xml添加如下配置

Spring3 Web MVC 集成Jasper Report生成PDF例子

四:Report內容與數據源

兩個報表,演示了子報表的用法,同時還演示了如何想子報表傳遞數據源,以及參

數傳遞在報表中顯示圖像等技巧。需要特別說明的是如果要在報表中使用圖像路徑

圖像必須位于WEB-INF/classes下面,因為JasperReport解析是按找類路徑尋找。關

于報表的詳細內容建議查看下載以后的源文件,此處不再細說。

五:Controller與注解

Spring3 Controller支持注解(annotation)方式,使用非常方便,生成PDF報表的

Controller代碼如下:

package com.gloomyfish.express.controllers;

import java.util.HashMap; import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JREmptyDataSource;

import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;

import com.gloomyfish.report.dao.MockDataFactory;

@Controller public class JasperReportController {

protected static Logger logger = Logger.getLogger("controller");

/**
 * Retrieves the PDF report file
 * 
 * @return
 */
@RequestMapping(value = "/getpdfReport", method = RequestMethod.GET)
public ModelAndView doSalesReportPDF(ModelAndView modelAndView) 
     {
    logger.debug("Received request to download PDF report");

    // Retrieve our data from a mock data provider
    MockDataFactory dataprovider = new MockDataFactory();

    // Assign the datasource to an instance of JRDataSource
    // JRDataSource is the datasource that Jasper understands
    // This is basically a wrapper to Java's collection classes
    JRDataSource categoryData  = dataprovider.getCategoriesData();

    // parameterMap is the Model of our application
    Map<string,object> parameterMap = new HashMap<string,object>();

    // must have the empty data source!!!
    JREmptyDataSource emptyData = new JREmptyDataSource();
    parameterMap.put("datasource", emptyData);
    parameterMap.put("JasperfishSubReportDatasource", categoryData);
    parameterMap.put("JasperfishSummaryInfo", dataprovider.getSummaryInfo());

    // pdfReport is the View of our application
    // This is declared inside the /WEB-INF/jasper-views.xml
    modelAndView = new ModelAndView("pdfReport", parameterMap);

    // Return the View and the Model combined
    return modelAndView;
}

}</string,object></string,object></pre> Mock數據工廠代碼如下:

package com.gloomyfish.report.dao;

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List;

import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class MockDataFactory {

public MockDataFactory()
{
    System.out.println("create mock up data");
}

public GloomyFishSummaryInfoBean getSummaryInfo()
{
    GloomyFishSummaryInfoBean summaryBean = new GloomyFishSummaryInfoBean();
    summaryBean.setBlogURL("http://blog.csdn.net/jia20003");
    summaryBean.setMajorDomain("J2SE, J2EE, WEB developer");
    summaryBean.setName("jia20003");
    summaryBean.setNickName("gloomyfish");
    summaryBean.setRegDate("2003-03-02");
    summaryBean.setWorkYears(8);
    return summaryBean;
}

public JRDataSource getCategoriesData()
{
    List<articlescategory> listData = new ArrayList<articlescategory>();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy, hh:mm:ss");
    Date createDte = new Date();
    ArticlesCategory category1 = new ArticlesCategory();
    ArticlesCategory category2 = new ArticlesCategory();
    ArticlesCategory category3 = new ArticlesCategory();
    ArticlesCategory category4 = new ArticlesCategory();
    ArticlesCategory category5 = new ArticlesCategory();
    ArticlesCategory category6 = new ArticlesCategory();

// ArticlesCategory category7 = new ArticlesCategory(); // ArticlesCategory category8 = new ArticlesCategory(); // ArticlesCategory category9 = new ArticlesCategory(); // ArticlesCategory categoryTen = new ArticlesCategory(); category1.setCategoryName("Android Development"); category1.setCount(6); category1.setCreateDate(sdf.format(createDte)); category2.setCategoryName("Swing Desktop Development"); category2.setCount(21); category2.setCreateDate(sdf.format(createDte)); category3.setCategoryName("JAVA 2D Image Process"); category3.setCount(56); category3.setCreateDate(sdf.format(createDte)); category4.setCategoryName("J2EE"); category4.setCount(8); category4.setCreateDate(sdf.format(createDte)); category5.setCategoryName("HTML5"); category5.setCount(4); category5.setCreateDate(sdf.format(createDte)); category6.setCategoryName("Network Protocols Research"); category6.setCount(4); category6.setCreateDate(sdf.format(createDte)); category6.setCategoryName("ExtJS Learning"); category6.setCount(2); category6.setCreateDate(sdf.format(createDte)); listData.add(category1); listData.add(category2); listData.add(category3); listData.add(category4); listData.add(category5); listData.add(category6); JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(listData); return data; }

}</articlescategory></articlescategory></pre> 啟動運行在瀏覽器中訪問地址為:http://localhost:8080/express/hello.html

六:Deploy與運行

全部代碼完成以后,從IDE中運行Maven的clean與install命令,得到war包

將war拷貝到tomcat的webapps下面即可啟動tomcat然后從瀏覽器訪問。

點擊[Get PDF Report]會自動在新窗口中打開生成的PDF報表文件。

程序運行打開主頁面結果如下:

Spring3 Web MVC 集成Jasper Report生成PDF例子

獲取PDF報表在瀏覽器中打開以后效果如下:

Spring3 Web MVC 集成Jasper Report生成PDF例子

七:常見問題

1.      必須在applicationContext.xml中導入jasper-views.xml資源否則報表不會被編譯

          為jasper文件

2.      Web.xml的servlet名必須與spring的xxx-servlet.xml中的xxx一致

3.      Jasper-views.xml中聲明的子報表路徑參數與數據參數必須與報表文件jrxml中保

         持一致

4.      報表中field變量名必須與Java Class中的field變量名一一對應。

 

八:項目文件打包下載,解壓縮作為Maven項目導入以后運行clean與 install命令。


來自:http://blog.csdn.net/jia20003/article/details/8471169

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!