2009-05-29 9 views
1

Im Schreiben meines Jaspis-Bericht Problem mit Struts2. Im Anschluss ist der Code, den ich auszuführen bin versucht:JasperReport Problem mit Struts2 - Null in endgültigen PDF-Datei

struts.xml enthält:

<action name="myJasperTest1" class="temp.JasperAction1"> 
    <result name="success" type="jasper"> 
     <param name="location">/jasper/our_compiled_template.jasper</param> 
     <param name="dataSource">myList</param> 
     <param name="format">PDF</param> 
    </result> 
</action> 

Mein JasperAction1 enthält:

import java.util.ArrayList; 
import java.util.List; 
import java.io.FileInputStream; 
import com.opensymphony.xwork2.ActionSupport; 
import com.sufalam.business.model.util.LegacyJasperInputStream; 
import com.sufalam.business.finance.model.bean.Account; 
import net.sf.jasperreports.engine.JasperCompileManager; 
import net.sf.jasperreports.engine.design.JasperDesign; 
import net.sf.jasperreports.engine.xml.JRXmlLoader; 

public class JasperAction1 extends ActionSupport { 

    /** List to use as our JasperReports dataSource. */ 
    private List<Account> myList; 

    public String execute() throws Exception { 

     // Create some imaginary persons. 
     Account a1 = new Account(); 
     Account a2 = new Account(); 

     a1.setId(77); 
     a1.setName("aaa"); 
     a2.setId(88); 
     a2.setName("bbb"); 
     // Store people in our dataSource list (normally would come from database). 
     myList = new ArrayList<Account>(); 
     myList.add(a1); 
     myList.add(a2); 

     // Normally we would provide a pre-compiled .jrxml file 
     // or check to make sure we don't compile on every request. 
     try { 
      JasperDesign design = JRXmlLoader.load(
        new LegacyJasperInputStream(new FileInputStream("F://backup//report2.jrxml"))); 

      JasperCompileManager.compileReportToFile(design, "F://backup//our_compiled_template1.jasper"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return ERROR; 
     } 

     return SUCCESS; 
    } 

    public List<Account> getMyList() { 
     return myList; 
    } 
} 

I iReport Plugin von Netbeans bin mit zur Generierung der .jrxml-Datei. Nach dem Entwurf meiner Seite iReport Assistenten meiner our_jasper_template.jrxml Datei enthält folgenden Code:

<?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="null" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> 
    <queryString language="SQL"> 
      <![CDATA[SELECT 
     account_master."name" AS account_master_name, 
     account_master."id" AS account_master_id 
    FROM 
     "public"."account_master" account_master]]> 
    </queryString> 
    <field name="account_master_name" class="java.lang.String"/> 
    <field name="account_master_id" class="java.lang.Integer"/> 
    <title> 
     <band height="58"> 
      <line> 
       <reportElement x="0" y="8" width="555" height="1"/> 
      </line> 
      <line> 
       <reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555" height="1"/> 
      </line> 
      <staticText> 
       <reportElement x="65" y="13" width="424" height="35"/> 
       <textElement textAlignment="Center"> 
        <font size="26" isBold="true"/> 
       </textElement> 
       <text><![CDATA[Classic template]]> 
       </text> 
      </staticText> 
     </band> 
    </title> 
    <detail> 
     <band height="40"> 
      <staticText> 
       <reportElement x="0" y="0" width="139" height="20"/> 
       <textElement> 
        <font size="12"/> 
       </textElement> 
       <text><![CDATA[account_master_name]]> 
       </text> 
      </staticText> 
      <textField> 
       <reportElement x="139" y="0" width="416" height="20"/> 
       <textElement> 
        <font size="12"/> 
       </textElement> 
       <textFieldExpression class="java.lang.String"><![CDATA[$F{account_master_name}]]> 
       </textFieldExpression> 
      </textField> 
      <staticText> 
       <reportElement x="0" y="20" width="139" height="20"/> 
       <textElement> 
        <font size="12"/> 
       </textElement> 
       <text><![CDATA[account_master_id]]> 
       </text> 
      </staticText> 
      <textField> 
       <reportElement x="139" y="20" width="416" height="20"/> 
       <textElement> 
        <font size="12"/> 
       </textElement> 
       <textFieldExpression class="java.lang.Integer"><![CDATA[$F{account_master_id}]]> 
       </textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
    <pageFooter> 
     <band height="26"> 
      <textField evaluationTime="Report" pattern="" isBlankWhenNull="false"> 
       <reportElement key="textField" x="516" y="6" width="36" height="19" forecolor="#000000" backcolor="#FFFFFF"/> 
       <box> 
        <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement> 
        <font size="10"/> 
       </textElement> 
       <textFieldExpression class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER}]]> 
       </textFieldExpression> 
      </textField> 
      <textField pattern="" isBlankWhenNull="false"> 
       <reportElement key="textField" x="342" y="6" width="170" height="19" forecolor="#000000" backcolor="#FFFFFF"/> 
       <box> 
        <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement textAlignment="Right"> 
        <font size="10"/> 
       </textElement> 
       <textFieldExpression class="java.lang.String"><![CDATA["Page " + $V{PAGE_NUMBER} + " of "]]> 
       </textFieldExpression> 
      </textField> 
      <textField pattern="" isBlankWhenNull="false"> 
       <reportElement key="textField" x="1" y="6" width="209" height="19" forecolor="#000000" backcolor="#FFFFFF"/> 
       <box> 
        <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement> 
        <font size="10"/> 
       </textElement> 
       <textFieldExpression class="java.util.Date"><![CDATA[new Date()]]> 
       </textFieldExpression> 
      </textField> 
     </band> 
    </pageFooter> 
</jasperReport> 

nun das Problem, das ich bin vor, wenn ich diese Aktion Klasse führen Sie es mir als PDF-Format folgende Ausgabe gibt: alt text

Antwort

1

Entfernen Sie einfach Abfrage-String-Teil aus Ihrer .jrxml-Datei und ändern Sie Ihren Feldnamen mit der Variablen Name der Account-Klasse.

0

Obwohl ich möglicherweise nicht die endgültige Lösung haben, gibt es ein Detail, das mir seltsam aussieht: Ihre Felder account_master_name und account_master_id scheinen nicht wirklich mit den Eigenschaftsnamen Ihrer Account-Klasse übereinstimmen. Sollte es nicht id und name sein? Was komisch ist für mich ist, dass es einen Fehler beim Ausfüllen des Berichts erzeugen sollte ...

Ich bin nicht vertraut mit Struts, aber ich sehe nicht, wie es sich auswirken kann.

+0

ya aber ich habe versucht, ID und Name als Feldname zu ändern. Aber ich bekomme immer noch Null-Wert als Ergebnis kein Fehler beim Ausfüllen des Berichts. DEBUG zeigt mir die Füllung: null nicht der Fehler ... Irgendwelche Vorschläge von deiner Seite? – Nirmal