【问题标题】:struts2 + hibernate3 + jasper-report 5.0 ----- getting blank reportstruts2 + hibernate3 + jasper-report 5.0 ----- 得到空白报告
【发布时间】:2013-10-14 13:52:01
【问题描述】:

我创建了一个使用 jasper report 5.0、struts2 和 hibernate 打印报告的应用程序。 jasper 报告使用 hql。 该应用程序运行良好,但问题是当我触发 struts 操作时,我在浏览器中得到 空白报告,但是当我在 netbeans 中预览报告时,我得到带有数据的报告。 p>

谁能告诉我一些解决方案。

报表单独运行时

通过struts2 + hibernate生成相同报告时的空白报告

我的代码如下所示

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>

【问题讨论】:

  • 你有没有在你的java代码中建立数据库连接?
  • 是的先生,因为当struts动作被触发时,在apache控制台中我得到......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!............ 这表明数据库没问题
  • 检查您能否将您的id 更改为id96_。然后检查您的 id 是否在您的 pdf 文件中看到。
  • 我必须将任何参数声明为id96_ 还是只需将字段表达式$F{id} 更改为$F{id96_}
  • 没有得到任何东西......仍然是空白报告......为什么这个空白报告来了......

标签: java hibernate struts2 jasper-reports


【解决方案1】:

结果

JasperFillManager.fillReportToFile(dest, parameterMap);

是带有文件名的字符串。该文件包含 jrPrint 对象。

如果不使用此文件,如何查看执行报告的结果?

【讨论】:

  • 我推荐使用方法fillReport。 fillReport 的结果是 jrPrint 对象。之后,您必须将 jrPrint 导出为 PDF。参见例如link
【解决方案2】:

我只是建议你应该去

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

然后,处理 JasperPrint 对象的返回引用。您还可以检查其中是否存在所有数据。根据文档,JasperPrint - 此类的一个实例表示可以查看、打印或导出为其他格式的面向页面的文档。填写报告时使用数据进行设计时,引擎会生成此类的实例,这些实例可以通过网络传输、以序列化形式存储在磁盘上或导出为各种其他格式,如 PDF、HTML、XLS、CSV 或 XML。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,所以我删除了 Struts2-JasperReports-Plugin jar 并自己编写了导出代码。这很简单!

    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");
        }
    
    }
    

    导出到 XLS 的工作方式非常相似。

    编辑:从连接对象更改为数据源

    【讨论】:

    • 在 Struts2 中使用 stream 结果。
    • 我添加了一些代码。您在寻找什么样的样品?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多