JasperReport 和struts2的整合

JasperReport是一個報表製作程序。用戶需要按照它制定的規則編寫一個xml文件,然後得到用戶需要輸出的格式文件。而iReport是一個製作JasperReport的xml文件的可視化開發 工具,該庫完全利用Java編寫。可以用於在各種Java應用程序中,包括J2EE、WEB應用程序中生成動態內容。
1.iReort。

Report Life Cycle
Before we start working with iReport, let's learn about the life cycle of a report. When you design a report using iReport you are creating a JRXML file, which is an XML document that contains the definition of the report layout. The layout is completely designed in a visual way, so you can ignore the real structure of the JRXML file. Before executing a report, the JRXML must be compiled in a binary object called a Jasper file. This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports.
The report execution is performed by passing a Jasper file and a data source to JasperReports. There are plenty of types of data sources, it's possible to fill a Jasper file from an SQL query, an XML file, a csv file, an HQL(Hibernate Query Language) query, a collection of JavaBeans, etc... If you don't find a suitable data source, JasperReports is very flexible and allows you to write your own custom data source. With a Jasper file and a data source, JasperReports is able to generate the final document in the format you prefer.
iReport also lets you configure data sources and use them to test your reports. In many cases, data-driven wizards can help you design your reports much quicker. iReport includes the JasperReports engine itself to let you preview your report output, test, and refine your reports.

compilation_process.png

To create the new connection in iReport, open the datasources/connections dialog by clicking its icon in the tool bar, as shown in Figure 2.

figure_2.png
Figure 2

Click New, choose Database JDBC connection as shown in Figure 3, and use the following settings for the sample HSQL database, as shown in Figure 4 below:

Name:
JasperReports Sample

JDBCDriver:
org.hsqldb.jdbcDriver

Url:
jdbc:hsqldb:hsql://localhost

Username:
sa

Password:
[none]

figure_3.png
Figure 3

figure_4.png
Figure 4

As said above, you can use your favorite database, in that case you have to provide the right URL, the JDBC driver and credentials. iReport proposes a list of well known database drivers. If the one you need is not present, just type the name. If it is present and it is highlighted in red it means that the driver is not available in the classpath and must be added. To do so, select Tools > Options > iReport > Classpath and add the required JARs to the classpath. Also, it's good practice to test the connection before saving it.
When done, the new connection appears in the connections list and is automatically selected as the default, active connection:

figure_5.png
Figure 5

Now, let's create the new report. Select File > New.

figure_6.png
Figure 6

In the template chooser that appears, select a blank report and click Open this Template. Select a name and location to story the new file, click next and then finish. Your blank report now appears in the designer:

figure_7.png

Click the button representing a database query in the designer tool bar:

figure_8.png
Figure 8

This opens the Query dialog to define the query that retrieves data to fill the report. The SQL query is stored inside the report and can accept parameters to filter the data using the syntax $P{} (for standard parameters) and $P!{} (to use a parameter as SQL chunk). Again we want to keep things simple in this tutorial, so we will not use any parameters. Type the following query to select all the fields/columns in the orders table:

select * from orders

figure_9.png
Figure 9

Of course, you can use any table for this example. The orders table is available in the sample database we are using. If you don't know SQL, you can use the visual SQL query builder by clicking the Query designer button.
As you type the query, iReport executes it to extract all the fields (columns) available in the query. The fields are listed on the bottom portion of the window. If there is an error in the SQL, you'll get a message that explains what's wrong. When the query is valid, click OK. iReport lists all the discovered fields in the report inspectory, so they can be used in the report and displayed using text fields and other elements.

To print the value of a field in the report, drag a field object from the report inspector to the design view. For example, drag a field into the detail band, a section of the report that is printed for each record/row in the query results. The title and the summary bands are printed just once in each report, while the page header and footer are printed once at the top and bottom of each page, respectively.

figure_10.png
Figure 10

When the field object is dragged inside the detail band, iReport creates a textfield element and sets the textfield expression for that element to $F{SHIPNAME}. This is a simple expression to print the value of the field SHIPNAME (the syntax $F{}). Repeat the same operation for other fields and format the textfields element by stretching, dragging and aligning them. It is important that the textfields are all placed within the detail band.

To preview the report, switch to the preview mode by clicking Preview in the designer tool bar. The preview compiles the report in the background and fills it with data retrieved by the query through the JDBC connection. As shown in Figure 13, the detail band repeats for every row in the query results, creating a simple table report.

figure_13.png

常見問題:

iReport 是圖形界面編輯 JasperReport 的工具,在簡體中文的操作系統下,它的本地化功能把界面弄得又有中文又有英文,很亂。
以前的舊版可以在選項中方便地修改界面成英文,但是到了 iReport-nb-3.5.2 版在 iReport 安裝路徑下的 etc/ireport.conf 裏增加啓動參數,把這行
default_options="-J-Xms256m -J-Xmx512m -J-Dfile.encoding=UTF-8 -J-Dorg.netbeans.ProxyClassLoader.level=1000"
改成
default_options="-J-Xms256m -J-Xmx512m -J-Dfile.encoding=UTF-8 -J-Dorg.netbeans.ProxyClassLoader.level=1000 -J-Duser.language=en"
再啓動就成英文界面了。
原理在於,通常咱們簡體中文的操作系統下,JVM 會檢測到系統屬性 user.language=zh,而 iReport 啓動時就使用這個系統屬性值,我用 -J-Duser.language=en 指定了 iReport 啓動時的參數,把用戶語言明確爲英文,界面就成英文的了。

image

出現如上錯誤,在下圖配置你預瀏覽的應用程序。

image

至於更深入的自己去網上找吧,網上有很多。

 

下面來進入JasperReport和struts的整合。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章