JasperReports总是打开同一份报告?

时间:2014-08-28 00:18:27

标签: java jasper-reports vaadin7

我正在尝试使用 JasperReports 创建一个报告给我的应用程序。当我在 JasperReports 进行一些更改时,我的应用程序中没有更新。
我尝试刷新我的项目,刷新浏览器并始终显示报告而不做任何更改

我正在尝试这个

public class ImprimeListaChamadaReport {
    private Turma turma;    
    private final String pathJasper = "br/com/webapp/reports/jasper/ListaDeChamadas.jasper";

    public ImprimeListaChamadaReport(Turma t){
        this.turma = t;
        createReport();
    }

    private void createReport(){
        final Map map = new HashMap();                
        final JRDataSource ds = new JRBeanCollectionDataSource(turma.getMatriculas());

        StreamResource.StreamSource source = new StreamResource.StreamSource() {
            public InputStream getStream() {
                byte[] b = null;

                try {                   
                    b = JasperRunManager.runReportToPdf(getClass().getClassLoader().getResourceAsStream(pathJasper), map, ds);
                } catch (JRException ex) {
                   ex.printStackTrace();
                }
                return new ByteArrayInputStream(b);
            }
        };

        StreamResource resource = new StreamResource(source, "ListaDeChamadas.pdf");
        resource.setMIMEType("application/pdf");              

        Embedded e = new Embedded("", resource);
        e.setSizeFull();
        e.setMimeType("application/pdf");
        e.setType(Embedded.TYPE_BROWSER);       

        ReportWindow w = new ReportWindow("Lista de chamada", e);
        UI.getCurrent().addWindow(w);       
    }       
}

我发布了图片。我做了改变" Hellooooo"当我做显示时,没有改变。

有什么想法吗?

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

问题很少来自缓存的.jasper文件。快速解决此问题的方法是清除服务器的工作目录并清理服务器。

如果仍然没有帮助,请在重新部署应用程序之前手动删除.jasper文件。而不是以后刷新项目。

希望这可以解决问题。

答案 1 :(得分:-2)

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

public class JasperReport {

    Logger log = GmLogger.getInstance(this.getClass().getName());

    private String strJasperReportName = "";
    private HashMap hmReportParameters = null;
    private List reportDataList = null;
    private HttpServletRequest request = null;
    private HttpServletResponse response = null;
    private String strJasperRealLocation = "";
    private int intpageHeight = 970;

    public void setJasperReportName(String strReportName) {
        this.strJasperReportName = strReportName;
    }

    public String getJasperReportName() {
        return strJasperReportName;
    }

    public void setHmReportParameters(HashMap hmParameters) {
        this.hmReportParameters = hmParameters;
    }

    public void setPageHeight(int intPHeight) {
        this.intpageHeight = intPHeight;
    }

    public HashMap getHmReportParameters() {
        return hmReportParameters;
    }

    public void setReportDataList(List dataList) {
        this.reportDataList = dataList;
    }

    public List getReportDataList() {
        return reportDataList;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * in the future, when we use another jasper location, "path", and set it in the
     * method:setJasperRealLocation(String path), instead of returns this
     * default strJasperRealLocation, it will return the value of "path";
     */
    public String getJasperRealLocation() {
        if (strJasperRealLocation == null || strJasperRealLocation.equals("")) {
            strJasperRealLocation = request.getSession().getServletContext().getRealPath(GmCommonClass.getString("GMJASPERLOCATION"));
        }
        return strJasperRealLocation;
    }

    public void setJasperRealLocation(String path) {
        this.strJasperRealLocation = path;
    }

    public void createJasperReport() throws Exception {
        String strJasperPath = GmCommonClass.getString("GMJASPERLOCATION");
        JasperPrint jp = null;
        JRExporter exporter = null;


        InputStream reportStream;
        ServletOutputStream servletOutputStream = response.getOutputStream();
        reportStream = request.getSession().getServletContext().getResourceAsStream(strJasperPath + strJasperReportName);
        if (reportDataList != null) {
            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JRBeanCollectionDataSource(reportDataList));
        } else {
            /*
             * if there is/are subreport(s) in detail section and we don't want
             * to print out duplicated sub reports, set this value as "new
             * JREmptyDataSource()" when use reportDataList in the detail
             * section.
             */

            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JREmptyDataSource());
        }

        jp.setPageHeight(intpageHeight);

        exporter = new JRHtmlExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);

        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
        exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, new HashMap());

        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/GmImageServlet?image=");
        exporter.exportReport();

        servletOutputStream.close();
    }

    public String getHtmlReport() throws JRException, FileNotFoundException, IOException {
        String strJasperPath = GmCommonClass.getString("GMJASPERLOCATION");
        JasperPrint jp = null;
        JRExporter exporter = null;
        StringBuffer strBuffer = new StringBuffer();

        InputStream reportStream;

        if(request == null)
        {
            String jasperPath = GmCommonClass.getString("GMJASPERTEMPLATEPATH");
            reportStream = new FileInputStream(jasperPath + strJasperReportName);   
            log.debug("Jasper location = " + jasperPath + strJasperReportName);
        }
        else
        {
            reportStream = request.getSession().getServletContext().getResourceAsStream(strJasperPath + strJasperReportName);
            log.debug("Jasper location = " + strJasperPath + strJasperReportName);
        }
        if (reportDataList != null) {
            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JRBeanCollectionDataSource(reportDataList));
        } else {
            /*
             * if there is/are subreport(s) in detail section and we don't want
             * to print out duplicated sub reports, set this value as "new
             * JREmptyDataSource()" when use reportDataList in the detail
             * section.
             */

            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JREmptyDataSource());
        }

        jp.setPageHeight(intpageHeight);


        exporter = new JRHtmlExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, strBuffer);

        //request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, new HashMap());

        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/GmImageServlet?image=");
        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, getImageURI());
        //
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, new Boolean(false));
        exporter.exportReport();
        return strBuffer.toString();
    }

    public void exportJasperReportToHtml(String strDestFileName) throws Exception {
        String strJasperPath = GmCommonClass.getString("GMJASPERLOCATION");
        JasperPrint jp = null;

        InputStream reportStream;
        ServletOutputStream servletOutputStream = response.getOutputStream();
        reportStream = request.getSession().getServletContext().getResourceAsStream(strJasperPath + strJasperReportName);
        if (reportDataList != null) {
            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JRBeanCollectionDataSource(reportDataList));
        } else {
            /*
             * if there is/are subreport(s) in detail section and we don't want
             * to print out duplicated sub reports, set this value as "new
             * JREmptyDataSource()" when use reportDataList in the detail
             * section.
             */
            jp = JasperFillManager.fillReport(reportStream, hmReportParameters, new JREmptyDataSource());
        }

        jp.setPageHeight(intpageHeight);        
        JasperExportManager.exportReportToHtmlFile(jp,strDestFileName);     
        servletOutputStream.close();
    }

    public HashMap sendJasperMail(String strReportName, 
             HashMap hmParams, 
             ArrayList  alReportData, 
             GmEmailProperties emailProperties, HttpServletRequest request) throws Exception
    {
        HashMap hmReturn = new HashMap();
        try
        {
            String strMessageDesc = "Original Mail Not Found.";
            setJasperReportName(strReportName);
            setHmReportParameters(hmParams);
            setReportDataList(alReportData);
            setRequest(request);
            strMessageDesc = getHtmlReport();

            hmReturn.put("ORIGINALMAIL", strMessageDesc);

            emailProperties.setMessage(strMessageDesc);

            log.debug("Sending jasper mail to : " + emailProperties);
            GmCommonClass.sendMail(emailProperties);

        }
        catch (Exception e)
        {
            e.printStackTrace();
            hmReturn.put("EXCEPTION", e);
        }
        return hmReturn;
    }


}