天天看點

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.

JasperReport 和struts2的整合

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.

JasperReport 和struts2的整合

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]

JasperReport 和struts2的整合

Figure 3

JasperReport 和struts2的整合

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:

JasperReport 和struts2的整合

Figure 5

Now, let's create the new report. Select

File > New

.

JasperReport 和struts2的整合

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:

JasperReport 和struts2的整合

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

JasperReport 和struts2的整合

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

JasperReport 和struts2的整合

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.

JasperReport 和struts2的整合

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.

JasperReport 和struts2的整合

常見問題:

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 啟動時的參數,把使用者語言明确為英文,界面就成英文的了。

JasperReport 和struts2的整合

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

JasperReport 和struts2的整合

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

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

繼續閱讀