Today we gone make Jasper report's using Java Spring MVC application
3- Now we have jasper or jrxml file how to use it in our code, But first where we should get Jar file for Jasper Reports
Jar Address in your system if iReport is installed in you system then go to the folder of iReport which is in default path is
C:\Program Files (x86)\Jaspersoft\iReport-5.0.4\ireport\modules\ext \
4- Now make a Simple spring mvc project in eclipse or IDE suit's you
In that project make a POJO class like this
for example --
class person
{
String name;
String address;
String dob;
Long phone;
****************************
Getter and setter for same
****************************
}
5- Intilize these Person POJO object's with content from database or hardcoded and assign to
List<person> object
6-this list object act as and Datasource for your reports, Now how we provide this datasource to or reports
this we done in our java code so hold your breath.
7- Now code for you controller is
{ Inside your controller method which is called on your click of any link
******************************************************************************* List<person> personlist=datasource // Told earlier in point 5
try {
// 1. Add report parameters
HashMap<String, Object> parametrs= new HashMap<String, Object>();
parametrs.put("Title", "User Report");
// 2. Retrieve template
InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATEPATH.jrxml);
// 3. Convert template to JasperDesign
JasperDesign jd = JRXmlLoader.load(reportStream);
// 4. Compile design to JasperReport
JasperReport jr = JasperCompileManager.compileReport(jd);
// 5. Create the JasperPrint object
// Make sure to pass the JasperReport, report parameters, and data source
JasperPrint jp = JasperFillManager.fillReport(jr, parametrs, personlist);
// Set our response properties
// Here you can declare a custom filename
String fileName = "UserReport.pdf";
response.setHeader("Content-Disposition", "inline; filename="+ fileName);
// Set content type
response.setContentType(MEDIA_TYPE_PDF);
The Steps to make report are as follows -:
1- Design a Report with empty datasource using iReport as described in previuos post check on following link - starting-with-netbeans-ireport.html
In Same select empty datasource and make feild with same name as in your application bean class(if don't get this line you will get it later just follow the steps)
2- Make Jasper generated by compiling(preview button on iReport) or jrxml that obtail before compiling the design on design in iReport.
check and try to understand this image given
3- Now we have jasper or jrxml file how to use it in our code, But first where we should get Jar file for Jasper Reports
Jar Address in your system if iReport is installed in you system then go to the folder of iReport which is in default path is
C:\Program Files (x86)\Jaspersoft\iReport-5.0.4\ireport\modules\ext \
4- Now make a Simple spring mvc project in eclipse or IDE suit's you
In that project make a POJO class like this
for example --
class person
{
String name;
String address;
String dob;
Long phone;
****************************
Getter and setter for same
****************************
}
5- Intilize these Person POJO object's with content from database or hardcoded and assign to
List<person> object
6-this list object act as and Datasource for your reports, Now how we provide this datasource to or reports
this we done in our java code so hold your breath.
7- Now code for you controller is
{ Inside your controller method which is called on your click of any link
******************************************************************************* List<person> personlist=datasource // Told earlier in point 5
try {
// 1. Add report parameters
HashMap<String, Object> parametrs= new HashMap<String, Object>();
parametrs.put("Title", "User Report");
// 2. Retrieve template
InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATEPATH.jrxml);
// 3. Convert template to JasperDesign
JasperDesign jd = JRXmlLoader.load(reportStream);
// 4. Compile design to JasperReport
JasperReport jr = JasperCompileManager.compileReport(jd);
// 5. Create the JasperPrint object
// Make sure to pass the JasperReport, report parameters, and data source
JasperPrint jp = JasperFillManager.fillReport(jr, parametrs, personlist);
// Set our response properties
// Here you can declare a custom filename
String fileName = "UserReport.pdf";
response.setHeader("Content-Disposition", "inline; filename="+ fileName);
// Set content type
response.setContentType(MEDIA_TYPE_PDF);
// Export is most important part of reports
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.outputstream());
exporter.exportReport();
return null;
}
*********************************************************************************
We can directly use Jasper file in reportstream to skip compilation step as it can also be done by iReport designer
code is ---
InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATEPATH.jasper);
JasperPrint jp = JasperFillManager.fillReport(reportStream , parametrs, personlist);down steps of code are same as in above
********************************************************************************
NOTE - check in you report design that fields name are same as in POJO class
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.outputstream());
exporter.exportReport();
return null;
}
*********************************************************************************
We can directly use Jasper file in reportstream to skip compilation step as it can also be done by iReport designer
code is ---
InputStream reportStream = this.getClass().getResourceAsStream(TEMPLATEPATH.jasper);
JasperPrint jp = JasperFillManager.fillReport(reportStream , parametrs, personlist);down steps of code are same as in above
********************************************************************************
NOTE - check in you report design that fields name are same as in POJO class
Have great programming :)
No comments:
Post a Comment