我想使用jasper-reports生成报告,但我无法确定控制器应包含哪些信息,因为当我创建报告时,我将其用作数据源,即查询的结果集。

report.jsp

<%@ include file="/WEB-INF/template/taglibs.jsp"%>
<div class="container">
    <%@ include file="menu.jsp"%>
    <div class="budgetTable">
        <div class="form-group ">
            <a class="btn btn-warning buttons generate"
                href="http://localhost:8080/Catering/index/reports/fullreport/pdf">Generate
                full report </a>
        </div>
        <div class="form-group">
            <a class="btn btn-warning buttons generate"
                href="http://localhost:8080/Catering/index/reports/partialreport/pdf">Generate
                partial report </a>
        </div>
        <div class="form-group">
            <a class="btn btn-warning buttons generate"
                href="http://localhost:8080/Catering/index/reports/anotherreport/pdf">Generate
                another report </a>
        </div>
    </div>
</div>


ReportsController.java

package catering.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ReportsController {

    protected static Logger logger = Logger.getLogger("controller");

    @RequestMapping(value="reports", method = RequestMethod.GET)
    public String reportsGet(Model model, Authentication authentication){

        logger.debug("Received request to show reports page(GET)");

        model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
        return "reports";
    }

    @RequestMapping(value = "/reports/fullreport/pdf", method = RequestMethod.GET)
    public ModelAndView fullReport(ModelAndView model){

        logger.debug("Received request to download PDF report");

        //List<UserModelSummary> users = UserSummaryDataAccess.getUsersSummary();
        //JRDataSource ds = new JRBeanCollectionDataSource(users);
        //Map<String, Object> parameterMap = new HashMap<String, Object>();
        //parameterMap.put("datasource", ds);
        //model = new ModelAndView("pdfReport",parameterMap);

        return model;
    }

}


jasper-views.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean id="pdfReport" class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView"
    p:url="classpath:fullReport.jrxml"
    p:reportDataKey="datasource">

    </bean>

</beans>


fullReport.jrxml

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<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="fullReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2a2f56d0-72e3-4ccf-942d-bf77c76956aa">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="cateringTest"/>
    <queryString>
        <![CDATA[select u.iduser, u.firstname, u.lastname, u.username from user u]]>
    </queryString>
    <field name="iduser" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="firstname" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="lastname" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="username" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="60" width="80" height="20" uuid="62c1f51a-4917-456f-8c14-e8df054a02ea"/>
                <text><![CDATA[Detailed Report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="50" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="29" width="80" height="20" uuid="5cbcc29e-c1fd-40e2-82b7-4faa4929cedb"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="80" y="30" width="100" height="20" uuid="c6e2fa3b-a346-4a48-b0fd-5381ab527640"/>
                <text><![CDATA[First name]]></text>
            </staticText>
            <staticText>
                <reportElement x="180" y="29" width="100" height="20" uuid="2fe1f184-f577-4f47-bdf4-661b4d297738"/>
                <text><![CDATA[Last name]]></text>
            </staticText>
            <staticText>
                <reportElement x="280" y="29" width="100" height="20" uuid="e4187042-e081-4cba-bdd3-ffd54ef3594b"/>
                <text><![CDATA[Username]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="24b51b0e-37f5-4636-910b-ce01fc427ce5"/>
                <textFieldExpression><![CDATA[$F{iduser}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="0" width="100" height="20" uuid="d60cbff1-5d4e-45e3-bd0f-ccb833f7165d"/>
                <textFieldExpression><![CDATA[$F{firstname}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="180" y="0" width="100" height="20" uuid="e6e56077-f6a1-4f23-923e-6eda1937d019"/>
                <textFieldExpression><![CDATA[$F{lastname}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="280" y="0" width="100" height="20" uuid="697ab4a6-70a3-4eeb-b0fe-842d1b51f754"/>
                <textFieldExpression><![CDATA[$F{username}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

最佳答案

应该是这样的。它应在响应中返回报告流。

@RequestMapping(value = "/reports/fullreport/pdf", method = RequestMethod.GET)
@ResponseBody
public Object fullReport(HttpServletResponse response){

        JRPdfExporter exporter = JasperFillManager.fillReport(jasperReport, parameters, customDataSource; //the function prepares the PDF repport

        response.setContentType("application/x-pdf");
        response.setHeader("Content-disposition", "attachment; filename=" + filename + ".pdf");

        final OutputStream outStream = response.getOutputStream();
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream);
        exporter.exportReport();

        return null;

09-05 19:26