3

i've created an application which prints out a report using jasper report 5.0, struts2 and hibernate. The jasper report uses hql. The app works fine but the problem is that i'm getting blank report in browser when i triggered the struts action, but when i preview the report in netbeans i'm getting the report with datas.

can anyone please tell me some solution for this.

When report run seperately

enter image description here

Blank report when same report is generated through struts2 + hibernate

enter image description here

My code is as given below

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="default" extends="struts-default,jasperreports-default">
<result-types>
     <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult" />
</result-types>

<action name="getpdf" class="commonpackage.HibernateClass" method="getpdf">
    <result name="success" type="jasper">
        <param name="location">/Reports_HSE/sample.jasper</param>
        <param name="dataSource">myList</param>
        <param name="format">PDF</param>
    </result>
</action>

    </package>
</struts>

HibernateClass.java

public class HibernateClass {

private List<PeopleBean> myList;
Session session;
SessionFactory sessionFactory;

public String getpdf()
{
    String dest = ServletActionContext.getServletContext().getRealPath("/Reports_HSE/sample.jasper");
    session = createSession();

    Map parameterMap = new HashMap();
    parameterMap.put(
        JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);


    try
    {
      System.out.println("Filling report...");
      JasperFillManager.fillReportToFile(dest, parameterMap);
      System.out.println("Done!");
      return "success";
    }
    catch (JRException e)
    {
      System.out.println("There was an error filling the report.");
      e.printStackTrace();
      return "error";
    }      
}
    public List getMyList() {
        return myList;
    }

  private Session createSession()
  {
    SessionFactory sessionFactory = new Configuration().configure()
        .buildSessionFactory();
    return sessionFactory.openSession();
  }

}

sample.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="HibernateQueryDemoReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="398cbfdc-5535-4373-bd84-38d1f6d0e677">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="countryCode" class="java.lang.String"/>
    <queryString language="hql">
        <![CDATA[FROM DepartmentMaster]]>
    </queryString>
    <field name="id" class="java.lang.Integer"/>
    <field name="abcCode1" class="java.lang.String"/>
    <field name="abcCode2" class="java.lang.String"/>
    <field name="deptCode" class="java.lang.String"/>
    <field name="deptName" class="java.lang.String"/>
    <field name="status" class="java.lang.String"/>
    <field name="createdDate" class="java.util.Date"/>
    <field name="modifiedDate" class="java.util.Date"/>
    <title>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement uuid="7c7dd6c2-fb3d-4540-a37d-9c7038a1c5ea" x="0" y="0" width="555" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA["Aircraft Registered in Country Code: " ]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement uuid="5bd34faa-d13d-4ac0-ab94-c86bc8950920" x="0" y="0" width="100" height="30"/>
                <textElement/>
                <text><![CDATA[
                    Tail Number
                ]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="456f1a7f-7255-4bcc-90ef-1673152ec19d" x="100" y="0" width="100" height="30"/>
                <textElement/>
                <text><![CDATA[
                    Serial Number
                ]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="84e80378-0e25-4d15-acd5-d368feda4982" x="200" y="0" width="100" height="30"/>
                <textElement/>
                <text><![CDATA[
                    Year Built
                ]]></text>
            </staticText>
        </band>
    </pageHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement uuid="5cef4310-14c8-4b6b-9204-99e2fcbfae15" x="0" y="0" width="100" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="52648e98-f0cd-4f21-9f2d-2f9db2429fb4" x="100" y="0" width="100" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{deptName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="533185fe-64cf-4839-b0f8-05fbeb2b5fe9" x="200" y="0" width="100" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{deptCode}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
  • Have you made database connection in your java code? – user1791574 Oct 14 '13 at 14:05
  • Yes sir , since when the struts action is triggered , in the apache console i'm getting......... `Filling report... Hibernate: select department0_.id as id96_, department0_.dept_code as dept2_96_, department0_.dept_name as dept3_96_, department0_.abc_code1 as abc4_96_, department0_.abc_code2 as abc5_96_, department0_.status as status96_, department0_.created_date as created7_96_, department0_.modified_date as modified8_96_ from hse.department_master department0_ Done!`.......... **that shows that database is okay** –  Oct 14 '13 at 14:13
  • For check can you change your `id` as `id96_`. Then check that your id see in your pdf file. – user1791574 Oct 14 '13 at 14:36
  • do i have to declare any parameter as `id96_` or simply change the field expression `$F{id}` to `$F{id96_}` –  Oct 14 '13 at 14:44
  • not getting anything....still blank report......why this blank report is coming..... –  Oct 14 '13 at 15:21
  • I am not sure but it may be that your field name in and its mapping name with database are not same. – user1791574 Oct 14 '13 at 15:58
  • ok.....but what about static label , it should print right.....even that too were not printing..... –  Oct 14 '13 at 16:03
  • Can you send value of parameterMap . Here is my email id: mabtestflight@gmail.com – user1791574 Oct 14 '13 at 16:09
  • ok i'll send u the project.....so that you can do check wat's the prob...... –  Oct 14 '13 at 16:15
  • Thanks for sharing... I will give you update ASAP after check it. – user1791574 Oct 14 '13 at 18:00
  • sir did you get the problem...... –  Oct 15 '13 at 05:27
  • I havn't check yet, I will inform you ASAP after debugging solution. – user1791574 Oct 15 '13 at 05:58
  • in which IDE you made this project? – user1791574 Oct 15 '13 at 06:14
  • You are not populating your `myList` list. – Aleksandr M Oct 15 '13 at 08:01
  • @AleksandrM but sir.....i'm not using that list right......i'm using hql query direclty in report........... –  Oct 15 '13 at 08:44
  • Try to use `connection` parameter instead of `dataSource`. – Aleksandr M Oct 16 '13 at 18:13
  • but i'm using hibernate right.....in the case if we are using mysql we can pass the connection.....but how abt hibernate –  Oct 17 '13 at 02:21

3 Answers3

0

The result of

JasperFillManager.fillReportToFile(dest, parameterMap);

is String with filename. This file contain jrPrint object.

How you can to see result of executing report if you dont't work with this file?

sanBez
  • 933
  • 1
  • 8
  • 18
  • I reccomend use method fillReport. The result of fillReport is jrPrint object. After that you must make export jrPrint to PDF. See for example [link](http://stackoverflow.com/questions/3746649/jasperreports-how-to-call-the-report-in-jsp-page/3746704#3746704) – sanBez Oct 15 '13 at 11:59
0

I just recommend that you should go for

JasperPrint  fill(java.lang.String sourceFileName, java.util.Map<java.lang.String,java.lang.Object> params)

then, work on the return reference of JasperPrint Object. You will also be able to check whether all your data are present in it.As per documentation, JasperPrint - An instance of this class represents a page-oriented document that can be viewed, printed or exported to other formats.When filling report designs with data, the engine produces instances of this class and these can be transferred over the network, stored in a serialized form on disk or exported to various other formats like PDF, HTML, XLS, CSV or XML.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

I faced the same problem, so I removed the Struts2-JasperReports-Plugin jar and wrote the exporting code myself. It is very simple!

public void exportReport() {

    try {    

        ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();

        Map<string, Object> parameters = new HashMap<>();
        parameters.put("id","1");

        //Get the parameters and report according to your application   
        byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, myList);


        //Setting the response as a downloadable attachment
        ServletActionContext.getResponse().setContentType("application/pdf");
        ServletActionContext.getResponse().setHeader("Content-Disposition: attachment",  "inline; filename=report.pdf");

        outputStream.write(bytes, 0, bytes.length);
        outputStream.flush();
        outputStream.close();

    } catch(Exception e) {
        addActionError("Unable to generate report");
    }

}

Exporting to XLS works in a very similar manner.

Edit: Changed from connection object to datasource

Neel Borooah
  • 91
  • 1
  • 1
  • 11