JasperReport 和struts2的整合

    技术2022-06-25  41

    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.

    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

    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

    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

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

    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:

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

    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

    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

    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.

    常见问题:

    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和struts的整合。


    最新回复(0)