从控制器到JSP的Jasper Sub报告数据源

时间:2018-06-07 05:59:58

标签: jasper-reports subreport

Employees.java(模型类)

@Entity
@Table(name = "EMPLOYEES")
public class Employees implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -8885410195177112037L;


    @Column(name = "empname",nullable = false)
    private String empname;

    @Column(name = "father_name",nullable = true)
    private String father_name;

    @Column(name = "date_of_birth",nullable = true)
    //@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private String date_of_birth;

    @ManyToOne(optional = false,fetch=FetchType.LAZY)
    @JoinColumn(name = "job_npfk")
    private Jobs jobs;

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public String getFather_name() {
        return father_name;
    }

    public void setFather_name(String father_name) {
        this.father_name = father_name;
    }

    public String getDate_of_birth() {
        return date_of_birth;
    }

    public void setDate_of_birth(String date_of_birth) {
        this.date_of_birth = date_of_birth;
    }

    public Jobs getJobs() {
        return jobs;
    }

    public void setJobs(Jobs jobs) {
        this.jobs = jobs;
    }
}

Jobs.java(模特类)

@Entity
@Table(name = "JOBS")
public class Jobs {

    @Id
    @Column(name = "jid")
    @GeneratedValue (strategy = GenerationType.AUTO)
    private Integer jid;

    @Column(name = "jobno")
    private String jobno;

    @Column(name = "jobname")
    private String jobname;

    @Column(name = "job_full_name")
    private String job_full_name;

    @OneToMany(mappedBy = "jobs",fetch=FetchType.LAZY, targetEntity=NespakEmployees.class, cascade=CascadeType.PERSIST)
    private List<Employees> npemp;

    public Jobs() {

    }

    public Integer getJid() {
        return jid;
    }

    public void setJid(int jid) {
        this.jid = jid;
    }

    public String getJobno() {
        return jobno;
    }

    public void setJobno(String jobno) {
        this.jobno = jobno;
    }

    public String getJobname() {
        return jobname;
    }

    public void setJobname(String jobname) {
        this.jobname = jobname;
    }

    public String getJob_full_name() {
        return job_full_name;
    }

    public void setJob_full_name(String job_full_name) {
        this.job_full_name = job_full_name;
    }

    public List<Employees> getNpemp() {
        return npemp;
    }

    public void setNpemp(List<Employees> npemp) {
        this.npemp = npemp;
    }

    public void setJid(Integer jid) {
        this.jid = jid;
    }

}

EmployeeDaoImpl.java

@Repository("npEmployeeDao")
public class EmployeeDaoImpl extends AbstractDao<Integer,Employees> implements EmployeeDao{

@SuppressWarnings("unchecked")
@Override
public List<NespakEmployees> listAllNpReport() {
    Session sessionHb = getSession();

    String sql = "select nespakemployees.file_no,nespakemployees.empname,nespakemployees.emp_code,nespakemployees.email,"
            + "nespakemployees.father_name,nespakemployees.gender,grade.gname,nespakemployees.address,jobs.jobname,nespakemployees.date_of_birth "
            + "from nespakemployees,grade,jobs "
            + "where nespakemployees.grade_npfk = grade.gid "
            + "And nespakemployees.job_npfk = jobs.jid "
            + "And TIMESTAMPDIFF(YEAR,date_of_birth,CURDATE())>= 60;";
    SQLQuery query = sessionHb.createSQLQuery(sql);
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

    //List<NespakEmployees> nList = (List<NespakEmployees>)query.list();

    //nList = query.list();

    return query.list();
}   

}

JobsDaoImpl.java

@Repository("jobsDao")
public class JobsDaoImpl extends AbstractDao<Integer,Jobs> implements JobsDao{

@Override
public List<Jobs> allJobs() {

    Criteria criteria = createEntityCriteria();

    return (List<Jobs>) criteria.list();
}
}

EmployeesServiceImpl.java

@Service("EmployeesService")
@Transactional
public class EmployeeeServiceImpl implements EmployeesService{

   @Autowired 
   private EmployeeDao dao;

   @Autowired
   private JobsDao jobsDao;

    @Override
    public List<NespakEmployees> listNpreport() {

        return dao.listAllNpReport();
    }
}

JobsServiceImpl.java

@Service("jobsService")
@Transactional
public class jobsServicesImpl implements jobsService{

    @Autowired
    private JobsDao dao;

    @Override
    public String jobName() {

        return dao.jobName();
    }
 }

Controller.java

@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {


     private SessionFactory sessionFactory;

    @Autowired
    jobsService JobsService;

    @Autowired
    EmployeesService npEmployeesService;

     @RequestMapping( value = {"nptestreport"}, method = RequestMethod.GET)


                 public String empReportNp(ModelMap modelMap,Model model){

                     Map model1 = new HashMap();

//List of Employee having Name, Father's Name and DOB (1st DataSource for Table)
                     List ell = npEmployeesService.listNpreport();
                     modelMap.put("dts", ell);
                     JRBeanCollectionDataSource dts = new JRBeanCollectionDataSource(ell);

//Job Name (2nd Data Source) for Subreport

                     List rpjobList1 = JobsService.allJobs();
                     modelMap.put("jobdts", rpjobList1);
                     JRBeanCollectionDataSource jobdts = new JRBeanCollectionDataSource(rpjobList1);


                      Map<String, Object> parameters = new HashMap<String, Object>();
                        parameters.put("dts1", dts);
                        parameters.put("rpjob", jobdts);
                     return "npEmpReport";

                 }
}

npEmpReport.jsp

<%@page contentType = "application/pdf"%>
<%@page trimDirectiveWhitespaces = "true"%>
<%@page import = "net.sf.jasperreports.engine.*"%>
<%@page import = "net.sf.jasperreports.engine.data.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.*"%>
<%
    try {

       /*List for Table Data Source*/
        List<Map<String,?>> dts1 = (List<Map<String,?>>) request.getAttribute("dts");

       /*List for Sub Report Data Source*/
        List<Map<String,?>> rpjob1 = (List<Map<String,?>>) request.getAttribute("rpjob");

        // Parameters for Table and Subreport
         Map reportParams = new HashMap(); 
         reportParams.put("dts", new JRBeanCollectionDataSource(dts1));
         reportParams.put("jobdts", rpjob1);

        String jrxmlFile = session.getServletContext().getRealPath("npReports/tBlank_A4.jrxml");
        InputStream input = new FileInputStream(new File(jrxmlFile));
        JasperReport jasperReport = JasperCompileManager.compileReport(input);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,reportParams,new JREmptyDataSource());
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        response.getOutputStream().flush();
        response.getOutputStream().close();
    }catch (Exception e){
        e.printStackTrace();
    }

%>

tBlank_A4.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 3.7.2  -->
<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="tBlank_A4" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF" lineSpacing="Single">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF" lineSpacing="Single">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF" lineSpacing="Single">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1">
        <parameter name="waheeb" class=""/>
        <queryString>
            <![CDATA[]]>
        </queryString>
    </subDataset>
    <subDataset name="Dataset2">
        <parameter name="subPara" class=""/>
        <parameter name="jobSubPara" class=""/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="empname" class="java.lang.String"/>
        <field name="email" class="java.lang.String"/>
        <field name="file_no" class="java.lang.Integer"/>
        <field name="father_name" class="java.lang.String"/>
        <field name="emp_code" class="java.lang.String"/>
        <field name="date_of_birth" class="java.sql.Date"/>
        <field name="gender" class="java.lang.String"/>
        <field name="gname" class="java.lang.String"/>
        <field name="address" class="java.lang.String"/>
        <field name="jobname" class="java.lang.String"/>
        <field name="Field_1" class="java.sql.Date"/>
    </subDataset>
    <subDataset name="Dataset3">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="jobname" class="java.lang.String"/>
    </subDataset>
    <subDataset name="Dataset4">
        <parameter name="jobSubPara1" class=""/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="jobname" class="java.lang.String"/>
    </subDataset>
    <parameter name="dts" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <parameter name="jobdts" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="empname" class="java.lang.String"/>
    <field name="email" class="java.lang.String"/>
    <field name="jobname" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="407" splitType="Stretch">
            <componentElement>
                <reportElement x="30" y="41" width="340" height="150">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Dataset2">
                        <datasetParameter name="subPara">
                            <datasetParameterExpression><![CDATA[$P{dts}]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[$P{dts}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="140">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="140" height="30"/>
                                <textElement lineSpacing="Single"/>
                                <textFieldExpression class="java.lang.String"><![CDATA[$F{empname}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField isBlankWhenNull="true">
                                <reportElement x="0" y="0" width="100" height="30"/>
                                <textElement lineSpacing="Single"/>
                                <textFieldExpression class="java.lang.String"><![CDATA[$F{father_name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30"/>
                                <textElement lineSpacing="Single"/>
                                <textFieldExpression class="java.lang.String"><![CDATA[new SimpleDateFormat("dd-MM-yyyy").format($F{date_of_birth})]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
            <subreport>
                <reportElement x="86" y="207" width="200" height="200"/>
                <dataSourceExpression><![CDATA[$P{jobdts}]]></dataSourceExpression>
                <subreportExpression class="java.lang.String"><![CDATA["src/main/webapp/npReports/ssBlank_A4_1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <pageFooter>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="220" y="0" width="100" height="20"/>
                <textElement textAlignment="Right" lineSpacing="Single"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="320" y="0" width="100" height="20"/>
                <textElement textAlignment="Left" lineSpacing="Single"/>
                <textFieldExpression class="java.lang.String"><![CDATA[" of " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>

ssBlank_A4.jrxml(子报告)

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 3.7.2  -->
<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="ssBlank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
    <property name="com.jaspersoft.studio.report.description" value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <parameter name="jobdts" class="java.lang.String"/>
    <queryString language="SQL">
        <![CDATA[select nespak.jobs.jobname from nespak.jobs where nespak.jobs.jobname = '3206']]>
    </queryString>
    <field name="jobname" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="jobname"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="jobs"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="30">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="f8a66ffb-5c8e-47c8-95f9-ee5783c71752"/>
                </reportElement>
                <textElement lineSpacing="Single"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{jobname}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
  

net.sf.jasperreports.engine.JRException:找不到资源:src / main / webapp / npReports / ssBlank_A4_1.jasper       at net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:145)       at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:107)       at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:383)       at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:350)       at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:408)       at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:326)       at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)       at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:455)       at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2024)       at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:754)       at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:264)       at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:124)       at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:540)       在net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)       at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:116)       at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:668)       at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:984)       at org.apache.jsp.WEB_002dINF.views.nespak.npEmpReport_jsp._jspService(npEmpReport_jsp.java:88)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

我使用Table生成报告,主报告中的表现很好。当我添加子报表时,它显示上面的错误。我很难将参数从主报表传递到子报表或在子报表上显示详细信息。

0 个答案:

没有答案