从我的servlet调用我的jasper报告时出错

时间:2015-10-30 18:07:27

标签: servlets jasper-reports subreport

我是报道概念的新手,我无法从我的servlet调用报告。我创建了一个显示sdudents标记的报告和一个显示每个学生平均值的子报告。我的报告代码是:

<?xml version="1.0" encoding="UTF-8"?>
 <!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1  -->
  <!-- 2015-10-30T20:01:50 -->
 <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="MarksReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c94cf32b-b8af-4c52-b787-3e35457c3400">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="reg data"/>
<queryString>
    <![CDATA[SELECT name,physics,chemistry,maths,biology from marks;]]>
</queryString>
<field name="name" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="physics" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="chemistry" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="maths" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="biology" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="51" splitType="Stretch">
        <staticText>
            <reportElement x="80" y="20" width="300" height="30" uuid="424d7727-b45d-4417-9a98-9d5f0757ab36"/>
            <textElement textAlignment="Justified">
                <font size="14" isBold="true" isUnderline="true"/>
            </textElement>
            <text><![CDATA[MARKS OF STUDENTS]]></text>
        </staticText>
    </band>
</title>
<columnHeader>
    <band height="63" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="30" width="80" height="30" uuid="a73606af-a42b-403b-8a29-fac31fbdfa7c"/>
            <text><![CDATA[name]]></text>
        </staticText>
        <staticText>
            <reportElement x="80" y="30" width="80" height="30" uuid="dbb8e810-b883-4cbd-bc57-259e7076cf39"/>
            <text><![CDATA[physics]]></text>
        </staticText>
        <staticText>
            <reportElement x="160" y="30" width="70" height="30" uuid="18b70e45-9cdf-45d2-bd2a-81b6f6863237"/>
            <text><![CDATA[chemistry]]></text>
        </staticText>
        <staticText>
            <reportElement x="230" y="30" width="70" height="30" uuid="0cca7051-fa1e-4e63-8ac7-116dedbf97a3"/>
            <text><![CDATA[maths]]></text>
        </staticText>
        <staticText>
            <reportElement x="300" y="30" width="70" height="30" uuid="b88ece10-c334-4a95-9495-2f044782c095"/>
            <text><![CDATA[biology]]></text>
        </staticText>
        <staticText>
            <reportElement x="370" y="30" width="100" height="30" uuid="7f575c71-202f-41f9-87cd-13264574defe"/>
            <text><![CDATA[average]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch">
        <textField>
            <reportElement x="0" y="11" width="80" height="30" uuid="9bc76bda-7b64-4bda-b484-a0ca301702ad"/>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="80" y="10" width="80" height="30" uuid="42eafc88-0e65-423f-9b38-e25ea4eb775e"/>
            <textFieldExpression><![CDATA[$F{physics}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="160" y="10" width="70" height="30" uuid="7656ef9f-2e99-4b57-a87b-6412d3ab5bb9"/>
            <textFieldExpression><![CDATA[$F{chemistry}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="230" y="10" width="70" height="30" uuid="f171900c-8342-443a-9a26-5049b8840134"/>
            <textFieldExpression><![CDATA[$F{maths}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="300" y="10" width="70" height="30" uuid="426d26eb-9d85-4ba2-836a-339928967353"/>
            <textFieldExpression><![CDATA[$F{biology}]]></textFieldExpression>
        </textField>
        <subreport>
            <reportElement x="370" y="0" width="95" height="20" uuid="75acf702-90cb-44c9-b819-270b0e2c128a">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
            </reportElement>
            <subreportParameter name="name">
                <subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA["AverageSub.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>

我的子报告的代码是:

<?xml version="1.0" encoding="UTF-8"?>
 <!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1  -->
    !-- 2015-10-30T20:01:50 -->
   <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="AverageSub" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2c961d35-c2da-4173-960a-f447fdaa2252">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<parameter name="name" class="java.lang.String"/>
<queryString>
    <![CDATA[SELECT physics,chemistry,maths,biology from marks WHERE name=$P{name}]]>
</queryString>
<field name="physics" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="chemistry" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="maths" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="biology" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<variable name="Average" class="java.lang.Double" calculation="Average">
    <variableExpression><![CDATA[Integer.parseInt($F{physics})+ Integer.parseInt($F{chemistry})+ Integer.parseInt($F{maths})+ Integer.parseInt($F{biology})]]></variableExpression>
</variable>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="33" splitType="Stretch">
        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
        <textField>
            <reportElement x="20" y="0" width="80" height="20" uuid="0b1bef5d-3e79-4c1b-92ad-3afc0a28fc13"/>
            <textFieldExpression><![CDATA[$V{Average}]]></textFieldExpression>
        </textField>
    </band>
</detail>
 </jasperReport>

我的servlet的代码是:

    package org.akash.java;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.util.HashMap;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import net.sf.jasperreports.engine.JasperCompileManager;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.JasperReport;



     @WebServlet(name = "MarksReport", urlPatterns = {"/MarksReport"})
     public class MarksReport extends HttpServlet {


     private static Connection conn = ConnectionManager.getInstance().getConnection();
     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HashMap hm= null;
       //String sname=request.getParameter("name");
    try {


       String jrxmlFileName = "C:/Users/Sony/workspace/StudentRegiForm/MarksReport.jrxml";
       //String jasperFileName = "C:/Users/Sony/workspace/StudentRegiForm/WebContent/RegReport.jrxml";
       //String pdfFileName = "C:/Users/Sony/workspace/StudentRegiForm/WebContent/RegReport.jrxml";

      //JasperCompileManager.compileReportToFile(jrxmlFileName, jasperFileName);
          JasperReport jasperReport=        JasperCompileManager.compileReport(jrxmlFileName);
       // String dbUrl = props.getProperty("jdbc.url");
       // String dbUrl = "jdbc:oracle:thin:@localhost:1521:mydbname";
       // String dbDriver = props.getProperty("jdbc.driver");
          String dbDriver = "com.mysql.jdbc.Driver";
       // String dbUname = props.getProperty("db.username");
       //String dbUname = "mydb";
       // String dbPwd = props.getProperty("db.password");
       //String dbPwd = "mydbpw";

       // Load the JDBC driver
       Class.forName(dbDriver);
       // Get the connection
       //Connection conn = DriverManager
         //.getConnection(dbUrl, dbUname, dbPwd);

       // Create arguments
       // Map params = new HashMap();
              // hm= new HashMap();
                   //hm.put("name",sname);




       // Generate jasper print
       JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperReport,hm, conn);

       // Export pdf file
       JasperExportManager.exportReportToPdfStream(jprint, response.getOutputStream());

       System.out.println("Done exporting reports to pdf");

      } catch (Exception e) {
       System.out.print("Exceptiion" + e);
      } 
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

并且我不断得到的错误是:Exceptiionnet.sf.jasperreports.engine.JRException:找不到资源:AverageSub.jasper其中AverageSub.jasper是我的子报告名称。

请伙计们,我需要你的帮助,我明天要把它呈现给老师。谢谢!!

1 个答案:

答案 0 :(得分:0)

假设存在AverageSub.jasper

如果没有,你可能忘记编译了AverageSub.jrxml

在jasper报告中,您应该使用图像&子报告的绝对路径,因此您需要通过其绝对路径(完整路径)调用子报告。

ES。

<subreportExpression><![CDATA["C:/Users/Sony/workspace/StudentRegiForm/AverageSub.jasper"]]></subreportExpression>

未来;通常是一条基道。 C:/ Users / Sony / workspace / StudentRegiForm /在参数map hm中传递,然后用于访问图像和子报告..

在java代码中添加到参数map。

hm.put("basePath","C:/Users/Sony/workspace/StudentRegiForm/")

在jrxml中添加参数

<parameter name="basePath" class="java.lang.String"></parameter>

在子报告调用中

<subreportExpression><![CDATA[$P{basePath}+"AverageSub.jasper"]]></subreportExpression>