【问题标题】:Show jasper report HTML page (web app) in Spring roo aplication在 Spring Boot 应用程序中显示 jasper 报告 HTML 页面(Web 应用程序)
【发布时间】:2014-08-18 18:58:20
【问题描述】:

我是 Spring Roo 的新手。

我使用 Spring Roo 创建了一个应用程序。 当我在 Spring Roo 中使用 Webapp 示例时,我在 PDF、Word、.... 中得到了很好的结果。 但是 HTML 和“HTML Page By Page Viewer”不起作用。 当我将 HTML 和“HTML Page By Page Viewer”更改为 Hello World 时,它们看起来是正确的。因此正确声明了 JSP 页面。 此外,我没有收到任何错误。

使用 Spring Roo 应用程序之外的 JasperReports 库的 Webapp 示例代码,我得到了想要的结果,但是当我在 Spring Roo 应用程序中使用相同的 JSP 文件时,我得到一个空白屏幕。

在 JSP 中,我检查 JasperPrint 对象是否为空,我可以看到它有 10 页。 System.out.println("Aantal pagina's = " + jasperPrint.getPages().size());

在 FileUploadController 中,我设置了会话属性。 然后我启动 message.jsp 文件。 这个 message.jsp 文件类似于 JasperReports Webapp 示例中的导出页面。 message.jsp 文件确实显示正确。 此外,所有 PDF、Word 等链接都可以正常工作,而且我确实看到了 Jasper 报告的正确 PDF 表示。 只有前 2 个链接、HTML 和 HTML 查看器逐页显示不正确。 HTML 页面为空。当我使用“Hello World”更改页面时,它会正确显示。

在 Spring Roo 中,我有一个 FileUpload 实体。 在 FileUploadController 我添加了:

 @RequestMapping(method = RequestMethod.POST, produces = "text/html")
 public String create(@Valid FileUpload fileUpload, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) throws URLNotFoundException {
    if (bindingResult.hasErrors()) {
        populateEditForm(uiModel, fileUpload);
        return "fileuploads/create";
    }

    File file = null;
    uiModel.asMap().clear();
    CommonsMultipartFile image = fileUpload.getImage();
    if (image != null) {
        file = new File(image.getOriginalFilename());
        System.out.println("Original filename = " + image.getOriginalFilename());
        try {
            image.transferTo(file);
            fileUpload.setContentType(image.getContentType());
            fileUpload.setPath(file.getAbsolutePath());
            fileUpload.setRemoteUrl(httpServletRequest.getRemoteAddr());
        } catch (Exception e) {
            e.printStackTrace();
            return "fileuploads/create";
        }
    }

    File jrPrintFile = file;

    JasperPrint jasperPrint = null;
    try {
        jasperPrint = (JasperPrint)JRLoader.loadObject(jrPrintFile);
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    httpServletRequest.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
    httpServletRequest.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

    httpServletRequest.setAttribute("filePath", file.getAbsolutePath());
    httpServletRequest.setAttribute("message", "The report " + image.getOriginalFilename() + "has been done successfully!");

    fileUpload.persist();
    return "fileuploads/message";
    //  return "redirect:/fileuploads/" + encodeUrlPathSegment(fileUpload.getId().toString(), httpServletRequest);
 }
 }

我的 Message.jsp 文件是(就像 JasperReports Webapp 示例中的导出页面)

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1" import="java.util.Date"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
      <html>
            <h3>HTML viewers</h3>
            <ul>
               <li><a href="jasperHtmlView" target="_blank"><span class="desc">HTML
                  viewer</span></a>
               </li>
               <li><a href="jasperViewer" target="_blank"><span class="desc">HTML
                  Page By Page Viewer</span></a>
               </li>
            </ul>
            <h3>PDF viewer</h3>
            <h3>
               <span class="desc"></span>
            </h3>
            <ul>
               <li><a href="servlets/pdf" target="_blank"><span class="desc">PDF
                  export</span></a>
               </li>
            </ul>
            <h3>Office&nbsp; viewers</h3>
            <ul>
               <li><a href="servlets/docx" target="_blank"><span
                  class="desc">Word (DOCX)</span></a></li>
               <li><a href="servlets/xlsx" target="_blank"><span
                  class="desc">Excel (XLSX)</span></a></li>
               <li><a href="servlets/xls" target="_blank"><span class="desc">Excel
                  2003(XLS)</span></a>
               </li>
               <li><a href="servlets/pptx" target="_blank"><span
                  class="desc">PowerPoint (PPTX)</span></a></li>
               <li><a href="servlets/odt" target="_blank"><span class="desc">penDocument
                  Text (ODT)</span></a>
               </li>
               <li><a href="servlets/ods" target="_blank"><span class="desc">OpenDocument
                  Spreadsheet (ODS)</span></a>
               </li>
            </ul>
            <%
               Date created = new Date(session.getCreationTime());
               Date accessed = new Date(session.getLastAccessedTime());
               Date expiry = new Date(session.getLastAccessedTime()
               + session.getMaxInactiveInterval() * 1000);
               %>
            <footer>
               <hr>
               <table style="width: 100%" border="0">
                  <tbody>
                     <tr>
                        <td>
                           <pre>Session ID</pre>
                        </td>
                        <td><%=session.getId()%></td>
                        <td>Method</td>
                        <td><%=request.getMethod()%></td>
                     </tr>
                     <tr>
                        <td>Created</td>
                        <td><%=created%></td>
                        <td>Request URI</td>
                        <td><%=request.getRequestURI()%></td>
                     </tr>
                     <tr>
                        <td>Last Accessed</td>
                        <td><%=accessed%></td>
                        <td>Path Info</td>
                        <td><%=request.getPathInfo()%></td>
                     </tr>
                     <tr>
                        <td>Next expired time</td>
                        <td><%=expiry%></td>
                        <td>Remote Address</td>
                        <td><%=request.getRemoteAddr()%></td>
                     </tr>
                  </tbody>
               </table>
               <hr>
            </footer>
         </body>
      </html>

我在 html.jsp 中的代码如下所示:

 <%@ page errorPage="error.jsp" %>
 <%@ page import="net.sf.jasperreports.engine.*" %>
 <%@ page import="net.sf.jasperreports.engine.*" %>
 <%@ page import="net.sf.jasperreports.engine.util.*" %>
 <%@ page import="net.sf.jasperreports.engine.export.*" %>
 <%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
 <%@ page import="java.util.*" %>
 <%@ page import="java.io.*" %>

 <%
    JasperPrint jasperPrint = (JasperPrint) session.getAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE);

    if (jasperPrint == null) {
        throw new JRRuntimeException("File WebappReport.jrprint not found.");
    }
    System.out.println("Jasper print object is NOT null");
    System.out.println("Aantal pagina's = " + jasperPrint.getPages().size());

    session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

    HtmlExporter exporter = new HtmlExporter();

    //  session.setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
    //  exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");

    exporter.exportReport();
 %>

【问题讨论】:

    标签: java spring servlets jasper-reports spring-roo


    【解决方案1】:

    您可以尝试使用 gvNIX 框架(基于 Spring-roo),该框架实现了与 jasper 报告和其他有趣功能轻松配合的功能。

    访问以下链接以了解有关 gvNIX 框架的更多信息:

    http://github.com/disid/gvnix http://code.google.com/p/gvnix/

    这里有一个示例,说明如何使用 gvNIX 实现带有 jasper 报告的应用程序:

    http://github.com/DISID/gvnix-samples/tree/master/quickstart-app#reports

    最好的问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-11
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2020-08-30
      相关资源
      最近更新 更多