日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

jasperreports_JasperReports JSF插件用例–简单列表报告

發(fā)布時(shí)間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jasperreports_JasperReports JSF插件用例–简单列表报告 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

jasperreports

這是JasperReports JSF插件系列的第一篇“用例文章” ,我將專注于一個(gè)簡(jiǎn)單的需求,并且我將進(jìn)一步深入。 起點(diǎn)是我們已經(jīng)為圖書商店完成的項(xiàng)目設(shè)置,我將向其中添加一個(gè)列表,其中包含在數(shù)據(jù)庫(kù)中注冊(cè)的其他圖書,該列表也將作為報(bào)告提供。

當(dāng)向用戶顯示報(bào)告時(shí),我們的應(yīng)用程序?qū)⑻峁┮韵鹿δ?#xff1a;

  • 用戶將能夠選擇報(bào)告的輸出格式。
  • 報(bào)告視圖將是可本地化的,并將以與應(yīng)用程序用戶相同的語(yǔ)言環(huán)境輸出。

最初設(shè)定

為了避免從功能上創(chuàng)建所有CRUD,為簡(jiǎn)單起見,我將初始化包含將使用SQL腳本顯示的值的表。 僅幾條記錄就??足以滿足本文的目的,因此,我將使用以下腳本來(lái)提供書本表:

insert into book(title, author, published_year, genre, price)values('The Futurogical Congress', 'Stanislaw Lem', '1978', 'SciFi', 2.5); insert into book(title, author, published_year, genre, price)values('Brave New World', 'H. G. Wells', '1975', 'SciFi', 3.99); insert into book(title, author, published_year, genre, price)values('Treasure Island', 'Robert Lewis Stevenson', '1948', 'Adventures', 4.45); insert into book(title, author, published_year, genre, price)values('The Lord of the Rings', 'J. R. Tolkien', '1945', 'Fantasy', 12.23); insert into book(title, author, published_year, genre, price)values('In Cold Blood', 'Truman Capote', '1966', 'Nonfiction', 9.50);

我將創(chuàng)建一個(gè)資源包,其中將包含視圖和報(bào)告的本地化文本。 該文件的名稱為Messages.properties ,我將其添加到資源根文件夾下的包net.sf.jasperreports.jsf.sample.usecases中 。 文件內(nèi)容如下:

report.format.select=Select report formatbookList.pageTitle=Browsing Books bookList.id=Reference ID bookList.title=Title bookList.author=Author bookList.genre=Genre bookList.year=Year bookList.price=Price

我添加了與上一個(gè)文件類似的另一個(gè)文件,但名為Messages_es.properties并保留了西班牙語(yǔ)翻譯,如果您對(duì)支持多個(gè)以上的語(yǔ)言感興趣,可以添加其他首選語(yǔ)言。 必須在faces-config.xml文件中配置先前的資源文件:

<faces-config ...><application><locale-config><default-locale>en</default-locale><supported-locale>es</supported-locale></locale-config><resource-bundle><var>Messages</var><base-name>net.sf.jasperreports.jsf.sample.usecases.Messages</base-name></resource-bundle></application> </faces-config>

由于我使用的是Maven,因此我將在項(xiàng)目結(jié)構(gòu)內(nèi)配置一個(gè)文件夾來(lái)保存報(bào)告的.jrxml文件,并配置POM以對(duì)該文件夾運(yùn)行JasperReports編譯器。 我選擇的文件夾名稱是src / main / jasperreports , JasperReports Maven插件的POM配置如下:

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>jasperreports-maven-plugin</artifactId><version>1.0-beta-2</version><executions><execution><goals><goal>compile-reports</goal></goals></execution></executions><configuration><outputDirectory>target/jasperreports/jasper</outputDirectory></configuration><dependencies><dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>4.5.1</version></dependency></dependencies> </plugin>

我還將在Maven WAR插件中添加一些額外的配置,以便它可以收集JasperReports編譯器生成的所有.jasper文件并將它們打包在應(yīng)用程序WAR文件中:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.2</version><configuration><webResources><resource><directory>src/main/jasperreports</directory><includes><include>**/*.jrxml</include></includes><targetPath>resources/reports/sources</targetPath></resource><resource><directory>target/jasperreports/jasper</directory><includes><include>**/*.jasper</include></includes><targetPath>resources/reports/jasper</targetPath></resource></webResources></configuration> </plugin>

您可能會(huì)注意到,我將JasperReports的.jrxml文件打包在Web應(yīng)用程序歸檔文件中。 當(dāng)使用插件時(shí),我們可以將其作為報(bào)告模板資源進(jìn)行引用,插件引擎足夠智能,可以識(shí)別所引用文件的類型,而當(dāng)使用對(duì)報(bào)告源文件的引用時(shí),插件將即時(shí)編譯報(bào)告。 但是,首選方法應(yīng)該始終是使用.jasper文件,因?yàn)樗绕渌募哂懈玫男阅堋??

設(shè)計(jì)報(bào)告模板

如本系列簡(jiǎn)介中所述,我將使用的JasperReports視覺設(shè)計(jì)工具是iReport,那里有很多關(guān)于如何使用iReport的教程,因此我將跳過(guò)這一部分。 但是,由于我要設(shè)計(jì)一個(gè)可識(shí)別語(yǔ)言環(huán)境的報(bào)告,因此必須注意JasperReports網(wǎng)站上提供的I18n Sample 。

因此,我們啟動(dòng)iReport并選擇一個(gè)空白報(bào)告模板。 由于我們的報(bào)告將使用基于SQL的數(shù)據(jù)源,因此可視設(shè)計(jì)器完成加載后,我們必須配置SQL語(yǔ)句以從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)。 為此,我們必須在設(shè)計(jì)器視圖中單擊縮放選項(xiàng)左側(cè)的按鈕,然后選擇“報(bào)告查詢”標(biāo)簽:

我將用來(lái)獲取數(shù)據(jù)SQL查詢是我們可以做的最簡(jiǎn)單的查詢。 輸入該SQL語(yǔ)句后,iReport將在窗口下部顯示所有可用字段。 如果它可以加載所有字段而沒有任何錯(cuò)誤,則單擊“確定”按鈕以保存它們以進(jìn)行報(bào)告:

select * from book;

我的最終報(bào)告中將輸出的所有文本都將來(lái)自資源包或數(shù)據(jù)庫(kù)本身。 因此,正如JasperReports i18n示例所指出的,無(wú)論何時(shí)要輸出一些文本,都應(yīng)使用文本字段。 報(bào)表設(shè)計(jì)視圖將如下圖所示:


報(bào)告模板文件的XML視圖將類似于以下內(nèi)容:

<?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"name="booklist" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"topMargin="20" bottomMargin="20"><property name="ireport.zoom" value="1.5"/><property name="ireport.x" value="0"/><property name="ireport.y" value="0"/><queryString><![CDATA[select * from book]]></queryString><field name="BOOK_ID" class="java.lang.Integer"/><field name="TITLE" class="java.lang.String"/><field name="AUTHOR" class="java.lang.String"/><field name="PUBLISHED_YEAR" class="java.lang.String"/><field name="GENRE" class="java.lang.String"/><field name="PRICE" class="java.math.BigDecimal"/><background><band splitType="Stretch"/></background><title><band height="38" splitType="Stretch"><textField><reportElement x="0" y="0" width="227" height="29"/><textElement><font size="18" isBold="true"/></textElement><textFieldExpression><![CDATA[$R{bookList.pageTitle}]]></textFieldExpression></textField></band></title><columnHeader><band height="27" splitType="Stretch"><textField><reportElement x="0" y="0" width="100" height="20"/><textElement><font isBold="true"/></textElement><textFieldExpression><![CDATA[$R{bookList.id}]]></textFieldExpression></textField><textField><reportElement x="113" y="2" width="155" height="20"/><textElement><font isBold="true"/></textElement><textFieldExpression><![CDATA[$R{bookList.title}]]></textFieldExpression></textField><textField><reportElement x="285" y="2" width="100" height="20"/><textElement><font isBold="true"/></textElement><textFieldExpression><![CDATA[$R{bookList.author}]]></textFieldExpression></textField><textField><reportElement x="398" y="2" width="100" height="20"/><textElement><font isBold="true"/></textElement><textFieldExpression><![CDATA[$R{bookList.year}]]></textFieldExpression></textField></band></columnHeader><detail><band height="21" splitType="Stretch"><textField><reportElement x="0" y="0" width="100" height="20"/><textElement/><textFieldExpression class="java.lang.Integer"><![CDATA[$F{BOOK_ID}]]></textFieldExpression></textField><textField><reportElement x="113" y="0" width="155" height="20"/><textElement/><textFieldExpression><![CDATA[$F{TITLE}]]></textFieldExpression></textField><textField><reportElement x="285" y="0" width="100" height="20"/><textElement/><textFieldExpression><![CDATA[$F{AUTHOR}]]></textFieldExpression></textField><textField><reportElement x="398" y="0" width="112" height="20"/><textElement/><textFieldExpression><![CDATA[$F{PUBLISHED_YEAR}]]></textFieldExpression></textField></band></detail><columnFooter><band height="45" splitType="Stretch"/></columnFooter><pageFooter><band height="25" splitType="Stretch"><textField><reportElement x="227" y="0" width="100" height="20"/><textElement textAlignment="Center"/><textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression></textField></band></pageFooter><summary><band height="42" splitType="Stretch"/></summary> </jasperReport>

將此文件保存在先前創(chuàng)建的src / main / jasperreports文件夾中,您可以開始下一節(jié)了。

設(shè)計(jì)JSF視圖

我們的JSF View將基于Facelets ,因此它將被編寫為XHTML文件,在其中我們需要聲明將要使用的不同庫(kù)名稱空間。 有關(guān)如何設(shè)置Facelets的說(shuō)明,請(qǐng)直接轉(zhuǎn)到其文檔 (JSF 2.x應(yīng)用程序無(wú)需額外配置即可直接支持Facelets)。

以下代碼片段顯示了Facelets模板的內(nèi)容,我將使用它們來(lái)呈現(xiàn)圖書清單的用戶界面。 復(fù)制它并將其保存在Web應(yīng)用程序內(nèi)容文件中的文件中。 我將使用的文件名為/book/bookList.xhtml :

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:jr="http://jasperreportjsf.sf.net/tld/jasperreports-jsf-1_3.tld"> <head><title>Book List - JasperReports JSF Use Cases</title> </head> <body> <h1><h:outputText value="#{Messages['bookList.pageTitle']}"/></h1><div><h:form id="bookListForm"><jr:source id="bookSource" type="jndi" value="java:comp/env/jdbc/BookStoreDB"/><h:panelGrid columns="3"><h:outputLabel for="reportFormat" value="#{Messages['report.format.select']}" /><h:selectOneMenu id="reportFormat" value="#{bookList.reportFormat}"onchange="document.bookListForm.submit();"><f:selectItems value="#{bookList.reportFormats}" /></h:selectOneMenu><jr:reportLink id="reportLink" format="#{bookList.reportFormat}"target="blank" source="bookSource"value="/resources/reports/jasper/booklist.jasper"resourceBundle="#{Messages}"><h:outputText value="#{Messages['bookList.action.show']}"/></jr:reportLink></h:panelGrid><h:dataTable value="#{bookList.allBooks}" var="book"><h:column><f:facet name="header"><h:outputText value="#{Messages['bookList.title']}"/></f:facet><h:outputText value="#{book.title}"/></h:column><h:column><f:facet name="header"><h:outputText value="#{Messages['bookList.author']}"/></f:facet><h:outputText value="#{book.author}"/></h:column><h:column><f:facet name="header"><h:outputText value="#{Messages['bookList.year']}"/></f:facet><h:outputText value="#{book.publishedYear}"/></h:column><h:column><f:facet name="header"><h:outputText value="#{Messages['bookList.genre']}"/></f:facet><h:outputText value="#{book.genre}"/></h:column></h:dataTable></h:form> </div> </body> </html>

突出顯示的行是我們添加JasperReports JSF插件組件的位置:

  • 首先,在第14行,我們通過(guò)<jr:source>標(biāo)記定義了一個(gè)報(bào)告源組件。 該組件不會(huì)將任何數(shù)據(jù)輸出到呈現(xiàn)HTML,但是可以由庫(kù)的可視組件引用,以告訴他們?nèi)绾潍@取呈現(xiàn)報(bào)表內(nèi)容所需的數(shù)據(jù)。
  • 從第21行到第26行,我們通過(guò)<jr:reportLink>標(biāo)記定義一個(gè)報(bào)告鏈接組件。 該組件將輸出一個(gè)標(biāo)準(zhǔn)的 HTML元素,該元素指向由插件引擎生成的特殊URL。 當(dāng)單擊它時(shí),插件的引擎將攔截該URL并替換一些JSF生命周期階段來(lái)處理報(bào)告數(shù)據(jù)并生成結(jié)果。 注意,報(bào)表組件具有一個(gè)屬性resourceBundle,它指向我們?cè)诘谝徊糠种袆?chuàng)建的消息包。

注意 : resourceBundle屬性已在JasperReports JSF插件的主干版本(在編寫此行時(shí))中可用,該版本使用標(biāo)記庫(kù)的1.3版本。 對(duì)于仍在使用1.2版標(biāo)記庫(kù)的用戶,他們可以獲得類似的結(jié)果,將參數(shù)添加到報(bào)表鏈接中:

<jr:reportLink id="reportLink" format="#{bookList.reportFormat}"target="blank" source="bookSource"value="/resources/reports/jasper/booklist.jasper"><f:param name="RESOURCE_BUNDLE" value="#{Messages}" /><h:outputText value="#{Messages['bookList.action.show']}"/> </jr:reportLink>

除了突出顯示的行之外,我還添加了代碼以呈現(xiàn)一個(gè)組合框,其中包含JasperReports JSF插件支持的所有導(dǎo)出選項(xiàng)以及一個(gè)數(shù)據(jù)表,該數(shù)據(jù)表將為表簿布置不同的記錄。

注意 :組合框包含一個(gè)onchange屬性,該屬性旨在使用報(bào)表導(dǎo)出選項(xiàng)的新值提交表單。 我必須這樣做,才能使報(bào)告鏈接在版本2.x之前的JSF中正常工作,因?yàn)樵摪姹緵]有任何AJAX支持。

上面顯示的JSF視圖需要一個(gè)托管bean才能工作,該托管bean將向用戶界面提供值:

public class BookListView {private BookManager bookManager;private List allBooks;private String reportFormat = &quot;pdf&quot;;public void setBookManager(BookManager bookManager) {this.bookManager = bookManager;}public List getAllBooks() {if (allBooks == null) {allBooks = bookManager.getAllBooks();}return allBooks;}public String getReportFormat() {return reportFormat;}public void setReportFormat(String reportFormat) {this.reportFormat = reportFormat;}public List getReportFormats() {FacesContext context = FacesContext.getCurrentInstance();JRFacesContext jrContext = JRFacesContext.getInstance(context);List list = new ArrayList();for (String format : jrContext.getAvailableExportFormats()) {list.add(new SelectItem(format, format));}return list;}}

bookManager字段是對(duì)接口的引用,該接口將提供從數(shù)據(jù)庫(kù)中獲取所有書籍的邏輯:

public interface BookManager {public List getAllBooks(); }

如本系列的介紹文章中所述,我將不介紹實(shí)現(xiàn)用例所需的其他類或接口的細(xì)節(jié),因?yàn)橛擅總€(gè)特定用戶來(lái)決定哪種框架將提供應(yīng)用程序的框架。 ?

一起測(cè)試

現(xiàn)在該檢查所有功能是否正常運(yùn)行了。 因此,讓我們打包我們的Web應(yīng)用程序,將其部署到我們的Tomcat服務(wù)器上,然后在您喜歡的瀏覽器中輸入以下URL: http:// localhost:8080 / jrjsf-usecases / book / bookList.jsf 。

注意 :在執(zhí)行部署操作之前,Derby(數(shù)據(jù)庫(kù))和Tomcat(應(yīng)用程序服務(wù)器)必須正在運(yùn)行。

因此,這是我們的瀏覽器輸出前面提到的URL的內(nèi)容時(shí)的外觀:


現(xiàn)在,單擊“顯示報(bào)告”鏈接以查看報(bào)告的輸出,其中填充了我們資源包中的字符串和數(shù)據(jù)庫(kù)中的數(shù)據(jù):


這是從報(bào)告的PDF視圖中截取的示例屏幕截圖。 使用用戶界面中的組合框可以更改報(bào)告導(dǎo)出格式。

結(jié)論

因此,總的來(lái)說(shuō),這是向JavaServer Faces應(yīng)用程序添加報(bào)告的最簡(jiǎn)單方法。 我從一個(gè)簡(jiǎn)單的示例開始,我將不時(shí)為其添加更復(fù)雜的功能。 希望您喜歡這篇文章并回來(lái)以后的出版物,好東西還沒有出現(xiàn)!

參考: JasperReports JSF插件用例–來(lái)自Code Nibbles博客的JCG合作伙伴 Alonso Dominguez的簡(jiǎn)單列表報(bào)告 。


翻譯自: https://www.javacodegeeks.com/2012/07/jasperreports-jsf-plugin-use-cases-2.html

jasperreports

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的jasperreports_JasperReports JSF插件用例–简单列表报告的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。