【问题标题】:JasperReports generated in a Servlet (GWT) does not appear没有出现在 Servlet (GWT) 中生成的 JasperReports
【发布时间】:2014-10-02 03:40:02
【问题描述】:

我在 GWT 中开发一个应用程序,我正在使用 api 生成 JasperReports 报告,最初尝试通过 RPC 生成,它返回给客户端一个带有 pdf 路径的字符串已创建,但不起作用,现在我正在尝试通过普通 servlet 生成报告,但报告已生成,屏幕上没有任何内容,浏览器控制台中也没有发现错误。

详情:

  • 开发模式完美运行。
  • 在本地主机上:8080 完美运行。

错误是应用程序发布在外部Tomcat

这是我的代码

Servlet:

public class RelatorioPacienteServiceImpl extends HttpServlet {

private static final long serialVersionUID = 1L;
private ServletContext sc;

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    sc = config.getServletContext();
}

@SuppressWarnings({ "unused", "unchecked", "rawtypes" })
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String id = req.getParameter("id");
    Map m = req.getParameterMap();

    Paciente paciente = new Paciente();
    File reportFile = null;
    String dir = sc.getRealPath(sc.getContextPath().replaceAll("\\\\", "/"));

    Map parameters = new LinkedHashMap();

    String path = dir + "/../reports/";// tomcat
    path = path.replaceAll("\\\\", "/");

    try {
        paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));

        List<Paciente> list = new ArrayList<>();
        list.add(paciente);

          HashMap parametros = new HashMap<String, Boolean>();  
        parametros.put("cpf", NumberMask.formatCpf(paciente.getCpf()));
        parametros.put("telefone1",NumberMask.formatPhone(paciente.getTelefone1()));
        parametros.put("telefone2",NumberMask.formatPhone(paciente.getTelefone2()));
        parametros.put("telefoneResponsavel",NumberMask.formatPhone(paciente.getTelefoneResponsavel()));
        parametros.put("dataNascimento",StringUtil.formatDate(paciente.getDataNascimento()));
        switch (paciente.getEtnia()) {
        case EtniaProps.BRANCA:
            parametros.put("etnia","Branco");
            break;
        case EtniaProps.INDIGENA:
            parametros.put("etnia","Indigena");
            break;
        case EtniaProps.PARDA:
            parametros.put("etnia","Parda");
            break;
        case EtniaProps.PRETA:
            parametros.put("etnia","Preta");
            break;

        default:
            break;
        }

        reportFile = new File(path + "report_paciente.jasper");

        byte[] bytes = null;

        JRDataSource jrds = new JRBeanCollectionDataSource(list);

        try {
            bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parametros, jrds);
        } catch (JRException ex) {
            ex.printStackTrace();
            System.out.println("Erro ao gerar o relatório " + ex.getMessage());
        }

        if (!list.isEmpty()) {
            if (bytes != null && bytes.length > 0) {
                resp.setContentType("application/pdf");
                resp.setContentLength(bytes.length);
                ServletOutputStream outputStream = resp.getOutputStream();
                outputStream.write(bytes, 0, bytes.length);
                outputStream.flush();
                outputStream.close();

            }
        } else {
            resp.setContentType("text/html");
            ServletOutputStream outputStream = resp.getOutputStream();
            String mensagem = "<html>" + "<head>" + "<meta http-equiv=\"content-type\" charset=\"UTF-8\" content=\"text/html\">"
                    + "<title>Incor lages</title>" + "</head>" + "<body>"
                    + "<br><br><br><br><h1>Documento sem paginas" + "</body>" + "</html>";
            outputStream.write(mensagem.getBytes(), 0, mensagem.getBytes().length);
            resp.setContentLength(mensagem.getBytes().length);
            outputStream.flush();
            outputStream.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Erro ao execura a query " + e.getMessage());
    }
}

调用servlet:

String url = GWT.getModuleBaseURL() + "relatorioPacienteService?id=" + paciente.getId();
Window.open(url, "_blank", "");

任何帮助将不胜感激

【问题讨论】:

    标签: java servlets gwt jasper-reports


    【解决方案1】:

    你能打印reportFile.getPath()吗?我怀疑 .jasper 文件的路径不正确。

    【讨论】:

      【解决方案2】:

      首先,如果您可以发布您的.jrxml 文件,那就更好了。

      根据可用信息(报告已生成,但为空白),我认为以下是关注领域:

       paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
      
       List<Paciente> list = new ArrayList<>();
       list.add(paciente);
      

      确保PacienteDAO.getPacientePorId(Integer.parseInt(id)); 实际上返回一个bean。因为如果它不返回任何内容或返回 null,则您使用的数据源,即JRBeanCollectionDataSource,将没有数据,因此不会显示任何内容。

      【讨论】:

      • PacienteDAO.getPacientePorId (Integer.parseInt (id));是的,正在返回一个我在报告路径上犯错的对象,但仍然无法击中他。并感谢回复
      • 尝试打印您的报告路径,看看它是否提供了所需的路径。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多