我有一个简单的jasper报告,它显示了数据库中的一些数据。碧玉报告在ireport设计师中表现良好。问题是当我尝试从servlet运行报告时。我试图从servlet运行时将报告导出为pdf。 我包含了像
这样的依赖项- jasperreports-4.6.0.jar
- itextpdf-5.1.1.jar
- commons-logging-1.1.1.jar
- commons-collections-3.2.1.jar
- commons-beanutils-1.8.3.jar
- commons-digester3-3.2.jar
- cglib-2.2.jar
我已将report1.jasper文件放在/web/report1.jasper中 这是我的servlet文件:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection connection;
ServletOutputStream servletOutputStream =response.getOutputStream();
InputStream reportStream =getServletConfig().getServletContext().getResourceAsStream("/report1.jasper");
HashMap parameterMap = new HashMap();
String stdID="510BCT2010";
String semester="1";
parameterMap.put("stdID", stdID);
parameterMap.put("semester", semester);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection ("jdbc:odbc:ACEM","","");
JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, parameterMap, connection);
connection.close();
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (Exception e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
当我第一次运行servlet时,我收到以下错误:
java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule
net.sf.jasperreports.components.ComponentsExtensionsRegistryFactory.<clinit>(ComponentsExtensionsRegistryFactory.java:100)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:247)
net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)
net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)
net.sf.jasperreports.engine.util.ClassUtils.instantiateClass(ClassUtils.java:53)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:237)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:214)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:162)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:133)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:105)
net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:83)
net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:121)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:407)
net.sf.jasperreports.engine.JasperRunManager.runToPdfStream(JasperRunManager.java:201)
net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:720)
com.reports.reportGenerator.doGet(reportGenerator.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
当我运行servlet之后,我得到以下错误
java.lang.NoClassDefFoundError: Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser
net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:121)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:407)
net.sf.jasperreports.engine.JasperRunManager.runToPdfStream(JasperRunManager.java:201)
net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:720)
com.reports.reportGenerator.doGet(reportGenerator.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
它说java.lang.NoClassDefFoundError。我错过了一些依赖吗?我的报告没有图表,只从数据库中提取数据
答案 0 :(得分:1)
考虑到堆栈跟踪,似乎apache commons digester在你的依赖项中缺失。
答案 1 :(得分:1)
我弄明白了这个问题。 Jasper报告库依赖性取决于其版本。 jasperreport 4.6需要groovy。我还有新的itext库(5.1),但jasper report 4.6仍然使用iText-2.1.7。