I was checking the following question JasperReports fillReport too slow and resource consuming and I tried to apply the verfied answer but couldn't notice any change in the generation of my reports.
Basicaly what I'm trying to do is to change the xpath executer factory for JasperFillManager to use Jaxen instead of Xalan but I can't seem to know where I should place the following line in my code
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
My code block looks as follows
private JasperPrint getJasperPrintInstance(JasperReport report) throws JRException, NamingException, SQLException {
JasperPrint jasperPrint = null;
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
if (dataSource == null) {
jasperPrint = JasperFillManager.fillReport(report, this.reportParams, connection);
} else {
jasperPrint =
JasperFillManager.fillReport(report, this.reportParams,
new JRBeanCollectionDataSource(getDataSource()));
}
return jasperPrint;
}
Even when I change "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory" to anything else nothing changes.
Can you please let me know what I may be doing wrong?
This happens because you are not using the newly created context. You need to create a JasperFillManager instance with it:
JasperFillManager jasperFillManager = JasperFillManager.getInstance(context);
and use that with its instance methods instead of the class ones like so:
jasperPrint = jasperFillManager.fill(report, this.reportParams, connection);
Related
So, I already have the report made, but the problem is that I'm using GetMapping to display, and it opens on another page alone, but I need it to open on a div, a table, a card, or even a modal. It just can't change from the previous page.
I have no idea how to open it with out redirecting the page. Any suggestion is appreciated
Method on the controller
#GetMapping("/seguro")
public void export(HttpServletResponse response) throws IOException, JRException, SQLException {
response.setContentType("text/html");
JasperPrint jasperPrint = null;
jasperPrint = seguroReportService.generatePromissoria(1L);
HtmlExporter htmlExporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
htmlExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
htmlExporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
htmlExporter.exportReport();
}
Method to get the report file
public JasperPrint generatePromissoria(Long id) throws SQLException, JRException, IOException {
Connection conn = jdbcTemplate.getDataSource().getConnection();
String path = resourceLoader.getResource("classpath:/reports/SeguroReport.jrxml").getURI().getPath();
JasperReport jasperReport = JasperCompileManager.compileReport(path);
// Parameters for report
Map<String, Object> parameters = new HashMap<>();
parameters.put("titulo", "Relatório de Seguros");
parameters.put("ID", id);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);
return print;
}
page where is supposed to open the report
Report opening in another page
I resolve the problem using the Iframe, but instead of passing the HTML on the src="", I made a onClick function on the button from my form targeting the name of the iframe.
<iframe name="my_iframe" src=""></iframe>
<button onclick="this.form.target='my_iframe'">My Button</button>
I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.
I'm having trouble to load a report through a button.This is my code.When I click the button it shows the down image error.
try {
InputStream in =
getClass().getResourceAsStream("C:/Users/RaMiNdU/Documents/NetBeansProjects/TimeTable
Generator/src/timetable/generator/sem1.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(in);
Map<String, Object> param = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param );
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println(e.getMessage());
}
add commons-digester.jar to your classpath and you should be through
commons-digester is not available in the Classpath.
Any start-up script is overriding Classpath environment variable.
Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due
to failure of static initialization is quite common.
Your class may not be visible while using multiple class loaders.
I have a Collection of type List that I want to pass it to a report using JasperReports and iReport on NetBeans, but I can't manage to show the content of the list on the Report.
I don't know how to show the data in the Details section of the jrxml. What parameter should I use?
The list has Objects of type Proveedor (Provider in Spanish) and has variables like name, code, etc.
Here is the code of the method who call to JasperReports:
public void generateReport(List<Proveedor> aList){
String reportName = System.getProperty("user.dir") + "\\src\\reports\\lista-proveedores.jrxml";
JRBeanCollectionDataSource dataSource;
JasperReport jasperReport;
JasperPrint jasperPrint;
try{
//1-I fill the dataSource with the Collection
dataSource = new JRBeanCollectionDataSource(aList);
//2-Compile the XML
jasperReport = JasperCompileManager.compileReport(reportName);
//3-Fill the report with the datasource
jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
//4-Export to PDF and save to disk
JasperExportManager.exportReportToPdfFile(jasperPrint, "Proveedores.pdf");
System.out.println("Finished!");
}catch (Exception e){
System.out.println(e);
//e.printStackTrace();
}
}
I have design my project in iReport and I have compiled successfully. Now I want to add the compiled jasper file to add in my java project. May any one help me?
Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().
Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.
Make sure that you include the necessary jar file to your path.
try {
String shopName = jComboBox1.getSelectedItem().toString();
String actionCode = jComboBox2.getSelectedItem().toString();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("storeName", shopName);
map.put("actionCode", actionCode);
URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
File reportFile = new File(reportFileURL.toURI());
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperViewer jv = new JasperViewer(jasperPrint);
JDialog viewer = new JDialog(this, "Batch Report", true);
viewer.setBounds(jv.getBounds());
viewer.getContentPane().add(jv.getContentPane());
viewer.setResizable(true);
viewer.setIconImage(jv.getIconImage());
viewer.setVisible(true);
} catch (JRException exc) {
System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
System.out.println(exs.getMessage());
}