简体   繁体   English

JasperReports总是打开相同的报告吗?

[英]JasperReports always opening the same report?

I'm trying create a report using JasperReports to my application. 我正在尝试使用JasperReports向我的应用程序创建报告。 What happens is when I do some change at JasperReports does not update in my application. 当我在JasperReports进行一些更改时,我的应用程序中没有更新时,会发生什么。
I tried refresh my project, refresh my browser and always displayed report without changes 我尝试刷新我的项目,刷新我的浏览器,并且始终显示报告而没有更改

I'm trying this 我正在尝试

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);       
    }       
}

I posted the images. 我发布了图像。 I did change "Hellooooo" and when I do display doesn't changed. 我确实更改了“ Hellooooo”,并且显示时也没有更改。

Any idea ? 任何想法 ?

在此处输入图片说明

在此处输入图片说明

The problem is seldom with .jasper file coming from the cache. 问题很少来自缓存中的.jasper文件。 A quick solution to this is clear your server's working directory and clean the server. 快速解决方案是清除服务器的工作目录并清理服务器。

If this still does not help, delete the .jasper file maually before you redeploy your application. 如果仍然不能解决问题,请在重新部署应用程序之前先删除.jasper文件。 And than later on refresh the project. 并且比以后刷新项目。

Hope this solves the issue. 希望这能解决问题。

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;
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM