Tải bản đầy đủ (.pdf) (17 trang)

Tài liệu JasperReports 3.5 for Java Developers- P8 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (628.23 KB, 17 trang )

Chapter 11
[
339
]
connection = DriverManager.getConnection("jdbc:mysql://localhost:
3306/flightstats?user=user&password=secret");
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), connection);
connection.close();
servletOutputStream.flush();
servletOutputStream.close();
return mapping.getInputForward();
}
}
All the action classes must extend the
org.apache.struts.action.Action
class.
Typically, the
execute()
method is overridden to implement custom logic for
servicing a request. As can be seen in this code, the
execute()
method takes an
instance of
HttpServletResponse
as one of its parameters. This makes it easy to
write action classes that generate reports.
The technique illustrated in the preceding example is not much different from what
we have seen in various earlier examples throughout the book. In most examples,
we used standard Java servlets to generate web reports, implementing the report
logic in the servlet's


doGet()
method. As both the
HttpServlet.doGet()
and
Action.execute()
methods take an instance of
HttpServletResponse
as one of
their parameters, the technique to generate a report from an action class is virtually
identical to the technique used when employing a servlet.
Let us take a look at the JSP that will invoke the
GenerateReportAction.execute()

method.
<%@ taglib uri=" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Generate Report</title>
</head>
<body>
<p>Click on the button to generate the report.</p>
<html:form action="/generate_report">
<html:submit />
</html:form>
</body>
</html>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Integrating JasperReports with Other Frameworks

[
340
]
This JSP will generate a very simple HTML form with a Submit button as its only
input eld.
Next, let us take a look at the form bean for this JSP.
package net.ensode.jasperbook.struts;
import org.apache.struts.action.ActionForm;
public class GenerateReportForm extends ActionForm
{
}
As the HTML form generated by the preceding JSP has no input elds other than
a Submit button, its corresponding form bean has no elds. We still need to write
it because, when writing Struts applications, each JSP must have a corresponding
form bean.
To wire the action class, the form bean, and the JSP together, we need to create a
struts-config.xml
le and deploy it in the
WEB-INF
directory of the application's
war
le.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
" /><struts-config>
<!-- ==================================== Form Bean Definitions -->
<form-beans>
<form-bean name="generateReportForm"
type="net.ensode.jasperbook.struts.GenerateReportForm">

</form-bean>
</form-beans>
<!-- =============================== Action Mapping Definitions -->
<action-mappings>
<action path="/generate_report"
type="net.ensode.jasperbook.struts.GenerateReportAction"
name="generateReportForm"
scope="request"
input="generate_report.jsp">
</action>
</action-mappings>
</struts-config>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 11
[
341
]
The
<form-bean>
tag denes the
GenerateReportForm
class as a form bean and
assigns the logical name
generateReportForm
to it.
The
<action>
tag maps the
GenerateReportAction

action class to the
/generate_report
path. It also species that the
GenerateReportForm
form bean
will be associated with this action. Finally, it links the
generate_report.jsp
JSP
le through the
input
attribute.
Like all server-side Java web applications, Struts applications must contain a
web.
xml
le in the
WEB-INF
directory inside the application's
war
le.
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
" >
<web-app>
<display-name>Struts JasperReports Application</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>

<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
This
web.xml
le simply denes the Struts
ActionServlet
to process all the URLs
ending in
.do
. The Struts
ActionServlet
calls the appropriate JSP and action class
behind the scenes for the appropriate URL.

This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Integrating JasperReports with Other Frameworks
[
342
]
Following the standard procedure for deploying web applications, we create a
WAR le with the preceding les and required dependencies, deploy it to a servlet
container, and point the browser to the corresponding URL. We should then see a
web page similar to the following:
Clicking on the Submit button generates the report, exports it to PDF, and displays
it in the browser.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 11
[
343
]
Summary
The chapter started with integrating JasperReports with Hibernate by writing
embedded report queries in the HQL. JasperReports with HQL queries is
similar to reports containing SQL queries except that the
language
attribute of
the
<queryString>
element must be set to
hql
. Next, we saw how to integrate
JasperReports with the JPA. As with Hibernate, JPA integration requires that the

language
attribute of the
<queryString>
element be modied. For JPA, the value
of this attribute must be set to
ejbql
. Following our discussion of JPA, we saw
how to integrate JasperReports with the Spring framework by taking advantage
of Spring's built-in support for JasperReports integration.
The chapter also dealt with JSF and JasperReports integration and illustrated how
to write backing beans that ll a report and display it in the browser. Finally, the
chapter illustrated the integration of JasperReports with Struts by explaining how to
write action classes that ll a report and display it in the browser.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×