我正在GWT中开发一个应用程序,我正在使用api生成 JasperReports 报告,最初尝试通过RPC进行生成,它使用pdf的路径返回给客户端一个字符串已创建,但这不起作用,现在我正在尝试通过普通的servlet生成报告,但生成报告,屏幕上没有任何内容,并且浏览器控制台中没有发现错误。
详细信息:
错误是在外部 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", "");
任何帮助将不胜感激
答案 0 :(得分:0)
可以打印reportFile.getPath()。我怀疑.jasper文件的路径不正确。
答案 1 :(得分:0)
首先,它会更好如果你可以发布你的.jrxml
文件。
根据可用的信息(生成报告但空白),我认为以下是关注的领域:
paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
List<Paciente> list = new ArrayList<>();
list.add(paciente);
确保PacienteDAO.getPacientePorId(Integer.parseInt(id));
实际上正在返回一个bean。因为如果它没有返回任何内容或返回null,您使用的数据源即JRBeanCollectionDataSource
将没有数据,因此不会显示任何内容。