简体   繁体   English

在Spring Roo Aplication中显示Jasper Report HTML页面(Web应用程序)

[英]Show jasper report HTML page (web app) in Spring roo aplication

I'm novice with Spring Roo. 我是Spring Roo的新手。

With Spring Roo I created an application. 使用Spring Roo,我创建了一个应用程序。 When I use the Webapp example in the Spring Roo I get good results with the PDF, Word, .... But the HTML and the "HTML Page By Page Viewer" do not work. 当我在Spring Roo中使用Webapp示例时,使用PDF,Word等可以得到良好的结果。但是HTML和“ HTML页面逐页查看器”不起作用。 When I change the HTML and the "HTML Page By Page Viewer" to a Hello World, they appear correct. 当我将HTML和“ HTML Page By Page Viewer”更改为Hello World时,它们显示正确。 So the JSP pages are correctly declared. 因此,正确声明了JSP页面。 Further I do not get any error. 此外,我没有任何错误。

With the Webapp example code of the JasperReports Library outside of the Spring Roo application I get the desired results, but when I use the same JSP file in my Spring Roo application I get an empty screen. 通过Spring Roo应用程序外部的JasperReports库的Webapp示例代码,可以得到所需的结果,但是当我在Spring Roo应用程序中使用相同的JSP文件时,则会出现空白屏幕。

In the JSP I check that the JasperPrint object is not empty and I can see that it has 10 pages. 在JSP中,我检查JasperPrint对象是否为空,我可以看到它有10页。 System.out.println("Aantal pagina's = " + jasperPrint.getPages().size());

In the FileUploadController I set the session attributes. 在FileUploadController中,我设置了会话属性。 Then I start the message.jsp file. 然后,我启动message.jsp文件。 This message.jsp file is like the Export page in the JasperReports Webapp example. 此message.jsp文件类似于JasperReports Webapp示例中的“导出”页面。 The message.jsp file does show up correctly. message.jsp文件确实显示正确。 Also, all the PDF, Word etc. links work correctly and I do see the correct PDF representation of the Jasper report. 另外,所有的PDF,Word等链接都可以正常工作,我确实可以看到Jasper报告的正确PDF表示形式。 Only the first 2 links, HTML and HTML viewer page by page do not show correctly. 只有前2个链接(HTML和HTML查看器逐页)无法正确显示。 The HTML page is empty. HTML页面为空。 When I change the page with "Hello World" it shows up correctly. 当我使用“ Hello World”更改页面时,它会正确显示。

In Spring Roo I have a FileUpload entity. 在Spring Roo中,我有一个FileUpload实体。 In the FileUploadController I added: 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);
 }
 }

My Message.jsp file is (like the export page in the JasperReports Webapp example) 我的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>

My code in the html.jsp lookes like: 我在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();
 %>

You can try using gvNIX framework (based on Spring-roo) that implements functionalities to works easily with jasper reports and other interesting functionalities. 您可以尝试使用gvNIX框架(基于Spring-roo),该框架实现了可轻松与jasper报告和其他有趣功能一起使用的功能。

Visit the following links to learn more about gvNIX framework: 访问以下链接以了解有关gvNIX框架的更多信息:

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

And here you have an example about how to implement an application with jasper reports using gvNIX: 这里有一个有关如何使用gvNIX用jasper报告实现应用程序的示例:

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

Best Regards, 最好的祝福,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM