Chen Kozhan
1. Design the report
Website address: http://community.jaspersoft.com/
Before we begin to design the report, we have to have tools to design the report, then we can find and download in the official website address
When you finish installing the Report Designer, start the tool
Data source: There are two types of data sources, the JDBC data source and the list data source.
First, we are using a list data source, that is, after querying the value through a background SQL statement, encapsulate into the list set, and then pass the data from the list set to the report as the data source.
The benefit of using a list set as a data source is that the data for the report design is not directly associated with the SQL statement, or it can be said to be more secure.
A. Configuring the fields to be displayed
fields-> right-click->.., you can refer to
B. Receiving the parameters passed over
Parameters-> Right-click Add, configure the relevant properties, you can!
C. Adding a subreport
Note:
1. You can right-click to set the relevant parameters (properties) of the subreport;
2. If you want to pass in a list set as a data source for a subreport, select the sub-report, expand Properties window,
Set Connection Type:use a DataSource expression,
Set Data Source Expression:newnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource ($P {sub1ds}).
Where sub1ds is the list set that receives the pass-through.
D. Add picture Display
Palette->image, then configure picture-related properties
For example, the property to display the path to the picture image expression set to $p{imageurl}
2. Integrate into the Web project and load the display
@Controller Layer-entrybillcontroller.java
/** * Print PDF report * Ckz * @param modelandview * @return * @throws Exception * /@RequestMapping ("/dopdf") public void Dorepor Tpdf (String billcode,string where,httpservletrequest Req,httpservletresponse resp) throws exception{ Entrybillservice.doreport (BILLCODE,WHERE,REQ,RESP);}
@Service Layer-entrybillservice.java
/** * Print Report * ckz * * @param billcode * @param req * @param resp * @throws Exception * * @SuppressWarnings ({"Rawtypes", "un Checked "}) @Transactional (readonly=true) public void DoReport (String billcode,string where, httpservletrequest req, HttpServletResponse resp) {try{list data = null;//= Entrybilldao.getprintentrybillds (Billcode); List list_sub1 = null;//entrybilldao.getprintsub1ds (Billcode); List list_sub2 = null;//entrybilldao.getprintsub2ds (Billcode);//Gets the project path, String Root_path=req.getsession (). Getservletcontext (). Getrealpath ("");//Get. jasper File path String reportfilepath = root_path;//+ "\\webresource\\reports\\ Report_entrybill_print_look_all_cn.jasper ";//Report logo picture path string imageurl=root_path+" \\webresource\\reports\\ Xxx.png ";//Set the report parameter map params = new HashMap (); String username = (string) req.getsession (). getattribute ("EmployeeName");p arams.put ("username", username);//+++++++ +++data = Entrybilldao.getprintentrybillds (billcode); list_sub1 = entrybilldao.getprintsub1ds (billcode); list_sub2 = EntrybILLBINDAO.GETPRINTSUB2DS (Billcode); reportfilepath+= "\\webresource\\reports\\entrybill\\report_entrybill_print_ Look_all_cn.jasper ";p arams.put (" sub1ds ", List_sub1);p arams.put (" Sub2ds ", List_sub2);p arams.put (" Entrybillmastitle "," Storage order Detail table ");p arams.put (" Subreport_dir ", root_path+" \\webresource\\reports\\entrybill\\ ");//+++ +++++++//Get data source Jrdatasource DataSource = new Jrbeancollectiondatasource (data);p arams.put ("ImageUrl", IMAGEURL); map<string,object> map = (map) data.get (0), if ("1". Equals (Map.get ("Billstate"). ToString ())) {Params.put (" Billstateimage ", root_path+" \\webresource\\reports\\audit-yes.png ");} if ("1". Equals (Map.get ("Disusestate"). ToString ())) {Params.put ("Billstateimage", root_path+ "\\webresource\\ Reports\\disuse-yes.png ");} Get Jasperprint object Jasperprint jasperprint = Reportuitl.getjasperprint (Reportfilepath, params, dataSource); Reportuitl.exportpdf (req, resp, jasperprint);} catch (Exception ex) {printwriter out = null;try {resp.setcharacterencoding ("UTF-8"), out = Resp.getwriter (); OUT.WRite ("Among them, Exportpdf (req, resp, jasperprint) and Getjasperprint (Reportfilepath, params, DataSource) methods of the class, Reportuitl.java
Package Com.wms.common;import Java.io.file;import Java.io.ioexception;import java.util.map;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Net.sf.jasperreports.engine.jrdatasource;import Net.sf.jasperreports.engine.jrexporterparameter;import Net.sf.jasperreports.engine.jasperfillmanager;import Net.sf.jasperreports.engine.jasperprint;import Net.sf.jasperreports.engine.jasperreport;import Net.sf.jasperreports.engine.export.jrpdfexporter;import net.sf.jasperreports.engine.util.jrloader;/** * Common methods for printing reports * @author CKZ * * * * * * * @SuppressWarnings ("deprecation") public Class Reportuitl {/** * get jasperprint Object * Ckz * * @return * @throws Exception */public static Jasperprint Getjasperpri NT (String reportfilename,map<string, object> params,jrdatasource dataSource) throws Exception{file file = new file (Reportfilename), if (!file.exists ()) throw new Exception ("System not found file" + reportfilename); Jasperreport report = (Jasperreport) jrloader.loadobjectfromfIle (File.getpath ()); Jasperprint print = Jasperfillmanager.fillreport (report, params, dataSource); return print;} /** * Print PDF file * Ckz * * @param req * @param resp * @param jasperprint * @throws ioexception * @throws jrexception */pub Lic static void Exportpdf (HttpServletRequest req,httpservletresponse resp,jasperprint jasperprint) throws Exception{// Get Jasperprint Stream object Jasperprint print = jasperprint;//use PDF exporter jrpdfexporter exporter =new jrpdfexporter ();// Set the parameters of the exporter exporter.setparameter (Jrexporterparameter.jasper_print, PRINT); Exporter.setparameter ( Jrexporterparameter.output_stream, Resp.getoutputstream ()); Exporter.setparameter (Jrexporterparameter.character_ ENCODING, "UTF-8");//execute Exporterexporter.exportreport ();}}
@Repository Layer-xxx.javaHere, you retrieve a list of data from the database that you set fields in the report to.
Foreground call:
You only need to access the @controller layer-entrybillcontroller.java class method
Doreportpdf (String billcode,string where,httpservletrequest req,httpservletresponse resp) to display your design report content!
Note: The *.jasper file is generated by the Report tool, click the Preview option to generate the *. jasper file located in the same directory (relative to the currently compiled *.jrxml file)
IReport Report-Graphic detailed