如何使用Spring MVC在jasper报表中显示来自MySQL DB的数据

时间:2018-05-15 10:42:57

标签: java spring-mvc jasper-reports

报告未显示表格。

Employee.java

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

    private static final long serialVersionUID = -8885410195177112037L;

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

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


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

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

EmployeeDaoImpl.java

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

@SuppressWarnings("unchecked")
public List<Employees> listAllNpEmp() {

    Session sessionHb = getSession();
    String sql = "select * from employees";
    SQLQuery query = sessionHb.createSQLQuery(sql);
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

    return query.list();
}
}

EmployeeService.java

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

@Autowired 
private EmployeeDao dao;

public List<NespakEmployees> listNpEMp() {

    return dao.listAllNpEmp();
}   
}

Controller.java

@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {
private SessionFactory sessionFactory;
@Autowired
EmployeesService npEmployeesService;
@RequestMapping( value = {"nptestreport"}, method = RequestMethod.GET)


             public String empReportNp(ModelMap modelMap){

                 List elist = npEmployeesService.listNpEMp();
                 modelMap.put("npEmpList", elist);
                 JRBeanCollectionDataSource dts = new JRBeanCollectionDataSource(elist, false);
                 Map<String,Object> parameter = new HashMap<String,Object>();
                    modelMap.put("dts", dts);

                 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<Map<String,?>> dataSource = (List<Map<String,?>>) request.getAttribute("npEmpList");
    JRDataSource jrDataSource = new JRBeanCollectionDataSource(dataSource);

//  Map<String,Object> parameters = new HashMap<String,Object>();
    String jrxmlFile = session.getServletContext().getRealPath("npReports/empReport.jrxml");
    InputStream input = new FileInputStream(new File(jrxmlFile));
    JasperReport jasperReport = JasperCompileManager.compileReport(input);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,jrDataSource);

     // JasperPrint = JasperFillManager.fillReport(jasperReport,null, new JREmptyDataSource());
    //OutputStream output = new FileOutputStream(new File("c:/output/JasperReport.pdf"));
    JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

    //response.setHeader("Content-Disposition", "attachment;filename=report.pdf");
    // JasperExportManager.exportReportToHtmlFile(jasperPrint, response.getOutputStream());

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

%>

empReport.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="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <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">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
    <queryString language="SQL">
        <![CDATA[select empname from employees]]>
    </queryString>
    <field name="empname" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="empname"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="employees"/>
    </field>
    </subDataset>
    <parameter name="dts" class="net.sf.jasperreports.engine.data.JRCollectionDataSource"/>
    <queryString>
    <![CDATA[]]>
    </queryString>
        <field name="empname" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
    <   band height="31">
            <rectangle>
                <reportElement x="0" y="0" width="111" height="31"/>
            </rectangle>
        </band>
    </columnHeader>
    <detail>
    <band height="210" splitType="Stretch">
        <rectangle>
            <reportElement x="0" y="0" width="111" height="31"/>
        </rectangle>
        <textField>
            <reportElement x="8" y="0" width="100" height="30"/>
            <textElement lineSpacing="Single"/>
            <textFieldExpression><![CDATA[$F{empname}]]></textFieldExpression>
        </textField>
        <componentElement>
            <reportElement x="337" y="10" width="103" height="200">
                <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="Dataset1">
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                </datasetRun>
                <jr:column width="100">
                    <jr:tableHeader style="Table_TH" height="30"/>
                    <jr:tableFooter style="Table_TH" height="30"/>
                    <jr:columnHeader style="Table_CH" height="30"/>
                    <jr:columnFooter style="Table_CH" height="30"/>
                    <jr:detailCell style="Table_TD" height="30">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30"/>
                            <textElement lineSpacing="Single"/>
                            <textFieldExpression><![CDATA[$F{empname}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

报告未显示表格。请帮忙

  

at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:982)           在org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:934)           在org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:581)           在org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)           at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5122)           在org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5786)           在org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)           在org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1027)           在org.apache.catalina.startup.HostConfig.undeploy(HostConfig.java:1565)           在org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1491)           在org.apache.catalina.startup.HostConfig.check(HostConfig.java:1713)           在org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:333)           在org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)           在org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)           在org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1373)

0 个答案:

没有答案