Java 在Servlet(GWT)中生成的JasperReports不会出现
我正在GWT中开发一个应用程序,我正在使用api生成JasperReports报告,最初尝试通过RPC生成报告,RPC向客户端返回一个带有创建的pdf路径的字符串,但这不起作用,现在我尝试通过普通servlet生成报告,但报告已生成,屏幕上没有显示任何内容,浏览器控制台中也没有发现任何错误。 详情:Java 在Servlet(GWT)中生成的JasperReports不会出现,java,servlets,gwt,jasper-reports,Java,Servlets,Gwt,Jasper Reports,我正在GWT中开发一个应用程序,我正在使用api生成JasperReports报告,最初尝试通过RPC生成报告,RPC向客户端返回一个带有创建的pdf路径的字符串,但这不起作用,现在我尝试通过普通servlet生成报告,但报告已生成,屏幕上没有显示任何内容,浏览器控制台中也没有发现任何错误。 详情: 开发模式工作得很好 在本地主机上:8080工作正常 错误发生在应用程序在外部Tomcat中发布时 这是我的密码 Servlet: public class RelatorioPacienteSe
- 开发模式工作得很好
- 在本地主机上:8080工作正常
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());
}
}
如果您能打印reportFile.getPath(),我们将不胜感激。我怀疑.jasper文件的路径不正确。首先,如果你能发布你的
.jrxml
文件就更好了
根据可用信息(报告已生成,但为空),我认为以下是关注的领域:
paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
List<Paciente> list = new ArrayList<>();
list.add(paciente);
paciente=PacienteDAO.getpacientporid(Integer.parseInt(id));
列表=新的ArrayList();
列表。添加(paciente);
确保
PacienteDAO.getPacientePorId(Integer.parseInt(id))
实际上返回了一个bean。因为如果它不返回任何内容或返回null,则您使用的数据源(即JRBeanCollectionDataSource
)将没有数据,因此不会显示任何内容。PacienteDAO.getPacientePorId(Integer.parseInt(id));“是”正在返回一个对象。我在报告路径上出错,但仍然无法击中他。感谢replyTry打印您的报告路径,看看它是否提供了所需的路径。
paciente = PacienteDAO.getPacientePorId(Integer.parseInt(id));
List<Paciente> list = new ArrayList<>();
list.add(paciente);