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

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

[
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">

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>

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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status