【Stimulsoft Reports Java教程】執行Java Web Viewer
阿新 • • 發佈:2018-11-20
本教程介紹了在Java報表工具中執行Web檢視器的基礎知識。例如,在網頁上顯示帶有儀表板的報表。
首先,我們需要建立動態Web專案。
接下來將Stimulsoft Java Libs新增到專案中。
您還可以轉換為Maven專案並配置pom.xml檔案以使用Maven中的庫。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>webviewer</groupId> <artifactId>webviewer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.stimulsoft</groupId> <artifactId>stimulsoft-reports-libs</artifactId> <version>2017.1.1</version> </dependency> </dependencies> </project>
接下來,我們需要在WebContent / WEB-INF資料夾中建立web.xml檔案。在這裡,我們配置了StimulsoftResource servlet,它檢索諸如* .js和影象檔案之類的內容,以及使用java web檢視器操作的StiWebViewerActionServlet。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/webapp_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>stimulsoft_webviewer</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>StimulsoftResource</servlet-name> <servlet-class>com.stimulsoft.web.servlet.StiWebResourceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StimulsoftResource</servlet-name> <url-pattern>/stimulsoft_web_resource/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>StimulsoftAction</servlet-name> <servlet-class>com.stimulsoft.webviewer.servlet.StiWebViewerActionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StimulsoftAction</servlet-name> <url-pattern>/stimulsoft_webviewer_action</url-pattern> </servlet-mapping> </web-app>
在下一步中,我們需要在WebContent資料夾中建立index.jsp頁面。在這裡,我們載入Dashboards.mrt報告模板檔案並呈現報表。我們還可以配置Web檢視器,例如將背景顏色設定為灰色。最後,將Web檢視器標記放到jsp頁面。
<%@page import=";com.stimulsoft.base.drawing.StiColorEnum"%> <%@page import=";com.stimulsoft.base.drawing.StiColor"%> <%@page import=";com.stimulsoft.webviewer.StiWebViewerOptions"%> <%@page import=";com.stimulsoft.webviewer.StiWebViewer"%> <%@page import=";java.io.File"%> <%@page import=";com.stimulsoft.report.StiSerializeManager"%> <%@page import=";com.stimulsoft.report.StiReport"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://stimulsoft.com/webviewer" prefix="stiwebviewer"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Stimulsoft Reports for Java</title> </head> <body> <% StiReport report = StiSerializeManager.deserializeReport( new File(request.getSession().getServletContext().getRealPath("/reports/Dashboards.mrt"))); report.render(); StiWebViewerOptions options = new StiWebViewerOptions(); options.getAppearance().setBackgroundColor(StiColorEnum.Gray.color()); pageContext.setAttribute("report", report); pageContext.setAttribute("options", options); %> <stiwebviewer:webviewer report="${report}" options="${options}" /> </body> </html>
現在,您可以將專案部署到Tomcat並執行它。
在下面的螢幕截圖中,您可以看到示例程式碼的結果。