Java beancollection is used as datasource.
JSP pageCode:
<% @ Page errorpage = "error. jsp" %>
<% @ Page Import = "Reportdatasource. *" %>
<% @ Page Import = "Net. SF. jasperreports. Engine. *" %>
<% @ Page Import = "Net. SF. jasperreports. Engine. util. *" %>
<% @ Page Import = "Net. SF. jasperreports. Engine. Export. *" %>
<% @ Page Import = "Net. SF. jasperreports. J2EE. servlets. *" %>
<% @ Page Import = "Java. util. *" %> <% @ page Import = "Java. Io. *" %>
<% @ Page Import = "Java.net. urlencoder" %>
<% @ Page Import = "Javax. servlet. *" %>
<% @ Page Language = "Java" contenttype = "application/X-msdownload" pageencoding = "UTF-8" %>
<%
String requestfilename = "linereport"; // report name
String reportfilename = requestfilename + ". Jasper ";
String filedownname = "XXX Report ";
Map parameters = New Hashmap ();
// Parameters. Put ("reporttitle", "address Report"); // pass parameters to the report.
Jasperprint = javabeanlistdatasourefactory. createjasperprintbyjavabeanlist (reportfilename, parameters, locationvaluebeanfactory. getpageloadtimehistorylinereportbeancollection ());
// Output
Pathclasspath classpath = New Pathclasspath ();
String stringpath = application. getrealpath ("/reports/" + reportfilename );
System. Out. Print ("reportfilename =" + stringpath );
File sourcefile = New File (stringpath );
Long Start = system. currenttimemillis ();
System. Err. println ("PDF creation time:" + (system. currenttimemillis ()-Start ));
//Download
Response. Reset ();//
Response. setcontenttype ("application/X-download ");
String filedisplay = filedownname + ". pdf ";
Filedisplay = urlencoder. encode (filedisplay, "UTF-8 ");
Response. addheader ("content-disposition", "attachment; filename =" + filedisplay );
Outputstream outp = Null ;
Try
{
Outp = response. getoutputstream ();
Jasperexportmanager. exportreporttow.stream (jasperprint, outp );
Outp. Flush ();
}
Catch (Exception E)
{
System. Out. println ("error! ");
E. printstacktrace ();
}
Finally
{
If (Outp! = Null )
{
Outp. Close ();
Outp = Null ;
}
// The following is an exception in getoutputstream () has already been called for this response:
Out. Clear ();
Out = pagecontext. pushbody ();
}
%>
Where:
Locationvaluebeanfactory. getpageloadtimehistorylinereportbeancollection ()
Returns the list of arrays of an object:
Arrays. aslist (locationvaluebean [] data );
Where:
Javabeanlistdatasourefactory. createjasperprintbyjavabeanlist()
Code:
// Packages that may be used in this class:
//
Import Java. Io. file;
Import Java. util. arraylist;
Import Java. util. arrays;
Import Java. util. collection;
Import Java. util. List;
Import Java. util. Map;
Import Java. util. properties;
Import Net. SF. jasperreports. Engine. jrexception;
Import Net. SF. jasperreports. Engine. jrexporterparameter;
Import Net. SF. jasperreports. Engine. jrruntimeexception;
Import Net. SF. jasperreports. Engine. jasperfillmanager;
Import Net. SF. jasperreports. Engine. jasperprint;
Import Net. SF. jasperreports. Engine. Data. jrbeancollectiondatasource;
Import Net. SF. jasperreports. Engine. Export. jrhtmlexporter;
Import Net. SF. jasperreports. Engine. Export. jrhtmlexporterparameter;
Import Net. SF. jasperreports. J2EE. servlets .*;
ImportJava. Io. filenotfoundexception;
ImportJava. Io. ioexception;
ImportJava. Io. inputstream;
//
//
Public Static Jasperprint createjasperprintbyjavabeanlist (string reportfilename, map parameters, collection)
{
String Path = "";
Properties prop = New Properties ();
Try {
Inputstream in = javabeanlistdatasourefactory. Class . Getclassloader (). getresourceasstream ("report. properties ");
Prop. Load (in );
Path = prop. getproperty ("site. reportjasper. Path ");
In. Close ();
} Catch (Filenotfoundexception e ){
E. printstacktrace ();
} Catch (Ioexception e ){
E. printstacktrace ();
}
Jrhtmlexporter exporter = New Jrhtmlexporter ();
Jasperprint = Null ;
// Pathclasspath classpath = new pathclasspath ();
// Readconfiginfo conf = new readconfiginfo ();
String reportfilenameresult = path + reportfilename;
File reportfile = New File (reportfilenameresult );
If (! Reportfile. exists ())
Throw New Jrruntimeexception ("file" + reportfilename + "Not found. The report design must be compiled first .");
Parameters. Put ("reporttitle", "address Report ");
Parameters. Put ("basedir", reportfile. getparentfile ());
Try {
Jasperprint =
Jasperfillmanager. fillreport (
Reportfilenameresult,
Parameters,
New Jrbeancollectiondatasource (Collection)
);
} Catch (Jrexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return Jasperprint;
}
PS: there is still a problem with the inserted code in the blog garden. Sometimes the top line will run out of the code block, and sometimes the bottom line will run out. My environment is win7 x64 + chrome 21.01180.89.