Generating Reports and PDFs From a Web Application

2018-12-17 11:55

Generating Reports and PDFs From a Web Application January 2007 [Revision number: V1-2]

When you need to generate a report, a chart, or a PDF file from a web application, you have many third-party products to help you accomplish your task, including the open source JasperReports framework. JasperReports is a reporting tool that outputs reports in HTML, PDF, XLS, CSV, and XML formats. This tutorial show how to integrate the JasperReports framework into a web application that you build using the Sun Java Studio Creator IDE.

In this tutorial, you create a web application that displays and prints information from a travel database. The web application lets you view a trip report in either HTML format or PDF format.

Contents

- Creating a JasperReports Library

- Setting Up the JRXML Editing Environment

- Creating the Project and Authorizing Access to the JasperReport Classes - Setting Up the Report Resources

- Mapping the JasperReports ImageServlet - Adding the Report Generation Logic - Creating the Database Query - Creating the Trip Report Page

Creating a JasperReports Library

Before you begin building your project, use the IDE's Library Manager to make the JasperReports Java reporting library easily available for any project.

1. Download jasperreports-1.2.7-project.zip from http://www.jasperforge.org and extract the files.

You must register with JasperForge before you can download the zip file. Note: The examples in this tutorial use JasperReports 1.2.7 and the associated JAR files that are provided with that version. If you use a different version, the JAR library names and version numbers might vary. In addition, the list of JAR libraries to add might differ. 2. In the IDE, choose Tools > Library Manager from the main menu.

3. Click New Library, type JasperReports127 in the Library Name text box, and click OK. 4. Click Add JAR/Folder and navigate to the directory into which you extracted the

jasperreports-1.2.7-project.zip files. 5. Navigate to the dist directory, select jasperreports-1.2.7.jar and press Enter.

6. Click Add JAR/Folder again, and navigate into the ../lib directory (go up one directory and down

into the lib directory). 7. Use Ctrl-Click to select the following JAR files and press Enter. If you are using a different version

than 1.2.7, consult the JasperForge web site to determine which JAR files to select.

o commons-digester-1.7.jar o itext-1.3.1.jar

These are the minimum libraries that you need for a web application that uses the

JasperReports framework. You might need to add additional libraries to use more advanced features. For example, if you are using the subreports feature, you might need to add commons-javaflow-20060411.jar.

Note: A web application that uses the JasperReports framework also requires the following JAR files. Because the Sun Application Server provides these libraries, you do not need to add them to the Library Manager or to your projects. If you are deploying to a different server, you might need to add the following JAR files to your project.

o commons-beanutils-1.5.jar o commons-logging-1.0.2.jar o commons-collections-2.1.jar

Also note that although the Sun Application Server provides the commons-digester library, this library does not include the SetNestedPropertiesRule class. This class, which is included in

commons-digester-1.7.jar, is required by the JasperReports compiler. Therefore, you must add it to the Library Manager as shown in this step.

8. Verify that you have added the three libraries, as shown in the following figure.

Figure 1: Library Manager Window Showing the JasperReports127 Library

To make the JavaReports Javadoc available to the Java Editor, click the Javadoc tab, click Add ZIP/Folder, navigate to the directory into which you extracted the jasperreports-1.2.7-project.zip files, select the dist/javadoc subdirectory, and press Enter.

(Optional) If you want to step into JasperReport classes during a debugging session, click the Sources tab, click Add JAR/Folder, navigate to the directory into which you extracted the jasperreports-1.2.7-project.zip files, select the src subdirectory, and press Enter. 9. Click OK to close the Library Manager.

Setting Up the JRXML Editing Environment

You write your JavaReport templates in XML format. To enable the IDE's editor to recognize a

JavaReport XML file (JRXML) as an XML file, you use the Options dialog to add jrxml to the Extensions and MIME Types property for the XML object, as shown in the following figure.

Figure 2: Associating a JRXML file with XML Objects

1. Choose Tools > Options to open the Options dialog box. 2. Select the Advanced radio button.

3. Expand IDE Configuration, expand System, expand Object Types, and select XML Objects (not

XML Schemas). 4. Click the ellipsis (...) button for the Extensions and MIME Types property to open the customizer

editor. 5. Type jrxml in the Item text box and click Add. 6. Click OK and close the Options dialog box.

Creating the Project and Authorizing Access to the JasperReport Classes

The Application Server provides a security feature that prevents the execution of unauthorized classes. Complete the following steps to create the project and to set the necessary permissions for a project that uses the JasperReport framework.

1. If the bundled database is not running, right-click the Bundled Database Server node in the

Servers window, and select Start Bundled Database. 2. From the IDE main menu, choose File > New Project, and create a JSF Web Application named

TravelReport. 3. The project appears with the initial page (Page1) open in the Visual Designer. 4. In the Projects window, right-click TravelReport > Libraries and choose Add Library.

5. In the Add Library window, select JasperReports127 as shown in the following figure, and click

Add Library.

Figure 3: Adding the JasperReports127 Library

6. If the Application Server is running, right-click the Deployment Server node in the Servers

window, select Start/Stop Server, and click Stop Server.

7. From the main menu, choose File > Open File, then navigate to and select the

creator-install-dir/SunAppServer8/domains/creator/config/server.policy file. 8. Add the following entry to the server.policy file to enable execution by the bundled Application

Server. Code Sample 1: JasperReports Permission Entries in the server.policy File grant codeBase \path-to-web-project/TravelReport/build/web/-\ permission java.util.PropertyPermission \ permission java.lang.RuntimePermission \

permission java.lang.reflect.ReflectPermission \ permission java.lang.RuntimePermission \ permission java.lang.RuntimePermission \ };

9. Replace path-to-web-project with the path to the folder that your project is in. For example, you

might use something like

file:/C:/Documents and Settings/Me/My Documents/Creator/Projects/MyApp/build/web/- for the codebase argument.

Note: You use this path because the IDE deploys a web application to the project's build directory by default. Remember to change the path specification if you use a different project name. Also, note that if you export a JasperReports project to a WAR file, you can use the following path for the codeBase:

10. file:${com.sun.aas.installRoot}/domains/creator/applications/j2ee-modules/project-name/- 11. Save and close the server.policy file.

12. Restart the Application Server by right-clicking the Deployment Server node in the Servers

window, selecting Start/Stop Server, and clicking Start Server. Tip: You can right-click the Deployment Server node in the Servers window and choose View Server Log to display the server's log file in the Output window area. After you start the server, check the log to make sure the server started without any problems. If there are syntax errors in the

server.policy file, the server might fail to start. For example, an incorrectly specified permission causes an AccessControlException exception.

Setting Up the Report Resources

In this section, you add an images directory and put the company's logo in this directory. Next, you add a JRXML template for the trip report, and you modify the project's build file to compile the JRXML template into a Jasper binary report template.

The JRXML template generates a report like the one shown in the following figure.


Generating Reports and PDFs From a Web Application.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:学习杨善洲心得体会优秀

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: