【Stimulsoft Reports Java教程】运行Java Web Designer

下载Stimulsoft Reports Java最新版本

本教程介绍了在Java报表工具中运行Web设计器的基础知识。例如,打开Master-Detail- 详细信息报表模板。

首先,我们需要创建动态Web项目。

【Stimulsoft Reports Java教程】运行Java Web Designer_第1张图片

然后将Stimulsoft Java Libs添加到项目中。

【Stimulsoft Reports Java教程】运行Java Web Designer_第2张图片

您还可以转换为Maven项目并配置pom.xml文件以使用Maven中的库。

 
     4.0.0 < / modelVersion> 
     webdesigner < / groupId > 
     webdesigner < / artifactId> 
     0.0.1-SNAPSHOT < / version> 
     war  
    
         src < / sourceDirectory> 
         
             
                 maven-compiler-plugin < / artifactId> 
                 3.5.1 < / version> 
                 
                     1.6 < / source> 
                     1.6 < / target> 
                < / configuration> 
            < / plugin> 
        < / plugins> 
    < / build> 
     
        
             com.stimulsoft  
             stimulsoft-reports-libs < / artifactId> 
             2017.1.1 < / version> 
        < / dependency> 
    < / dependencies> 
 < / project>

接下来,我们需要在WebContent / WEB-INF文件夹中创建web.xml文件。在这里,我们配置StimulsoftResource servlet,它检索诸如* .js和图像文件之类的内容,以及使用java web设计器操作的StiWebDesignerActionServlet。



    stimulsoft_webdesigner
        
        index.jsp
    
    
        StimulsoftResource
        com.stimulsoft.web.servlet.StiWebResourceServlet
    
    
        StimulsoftResource
        /stimulsoft_web_resource/*
    
    
        StimulsoftDesignerAction
        com.stimulsoft.webdesigner.servlet.StiWebDesignerActionServlet
    
    
        StimulsoftDesignerAction
        /stimulsoft_webdesigner_action
    

在下一步中,我们需要在WebContent文件夹中创建index.jsp页面。在这里,我们加载Master-Detail.mrt报表模板文件并定义报表的数据路径。


<%@page import=";java.io.FileOutputStream"%>
<%@page import=";java.io.FileInputStream"%>
<%@page import=";com.stimulsoft.report.utils.data.StiDataColumnsUtil"%>
<%@page import=";com.stimulsoft.report.dictionary.StiDataColumnsCollection"%>
<%@page import=";com.stimulsoft.report.dictionary.StiDataColumn"%>
<%@page import=";com.stimulsoft.report.utils.data.StiSqlField"%>
<%@page import=";com.stimulsoft.report.dictionary.dataSources.StiDataTableSource"%>
<%@page import=";com.stimulsoft.report.utils.data.StiXmlTable"%>
<%@page import=";com.stimulsoft.report.utils.data.StiXmlTableFildsRequest"%>
<%@page import=";com.stimulsoft.webdesigner.StiWebDesigerHandler"%>
<%@page import=";com.stimulsoft.webdesigner.StiWebDesignerOptions"%>
<%@page /> import="com.stimulsoft.report.dictionary.databases.StiXmlDatabase"%>
<%@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/webdesigner" prefix="stiwebdesigner"%>


Stimulsoft Webdesigner for Java



    <%
        final String reportPath = request.getSession().getServletContext().getRealPath("/reports/Master-Detail.mrt");
        final String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
        final String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
        final String savePath = request.getSession().getServletContext().getRealPath("/save/");
...

我们还可以指定Web设计器选项,例如设置必要的本地化。

....
        StiWebDesignerOptions options = new StiWebDesignerOptions(); 
        //options.setLocalization(request.getSession().getServletContext().getRealPath("/localization/de.xml"));
...

接下来,我们需要实现StiWebDesigerHandler来填充报表数据并保存/加载报表模板。

....
        StiWebDesigerHandler handler = new StiWebDesigerHandler(){
            //Occurred on loading webdesinger. Must return edited StiReport
            public StiReport getEditedReport(HttpServletRequest request){
                try{
                    StiReport report = StiSerializeManager.deserializeReport(new File(reportPath));
                    report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
                    return report;
                } catch (Exception e){
                    e.printStackTrace();
                }
                return null;
            }
 
            //Occurred on opening StiReport. Method intended for populate report data.
            public void onOpenReportTemplate(StiReport report, HttpServletRequest request){
                report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
            }
 
            //Occurred on new StiReport. Method intended for populate report data.
            public void onNewReportTemplate(StiReport report, HttpServletRequest request){
                report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
                try{
                    StiXmlTableFildsRequest tables = StiDataColumnsUtil.parceXSDSchema(new FileInputStream(xsdPath));
                    for (StiXmlTable table : tables.getTables()){
                        StiDataTableSource tableSource = new StiDataTableSource(
                            "Demo." + table.getName(), table.getName(), table.getName());
                        tableSource.setColumns(new StiDataColumnsCollection());
                        for (StiSqlField field : table.getColumns()){
                            StiDataColumn column = new StiDataColumn(
                                field.getName(), field.getName(), field.getSystemType());
                            tableSource.getColumns().add(column);
                        }
                        tableSource.setDictionary(report.getDictionary());
                        report.getDictionary().getDataSources().add(tableSource); 
                    } 
                } catch (Exception e){
                    e.printStackTrace(); 
                } 
            }
 
            //Occurred on save StiReport. Method must implement saving StiReport
            public void onSaveReportTemplate(StiReport report, StiRequestParams requestParams, HttpServletRequest request) {
                try {
                    FileOutputStream fos = new FileOutputStream(savePath + requestParams.designer.fileName);
                    if (requestParams.designer.password != null) {
                        StiSerializeManager.serializeReport(report, fos, requestParams.designer.password);
                    } else {
                        StiSerializeManager.serializeReport(report, fos, true);
                    }
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
 
        pageContext.setAttribute("handler", handler);
        pageContext.setAttribute("options", options);
    %>
...

最后,将web designer标记放到jsp页面。

...
    

现在,您可以将项目部署到Tomcat并运行它。

【Stimulsoft Reports Java教程】运行Java Web Designer_第3张图片

在下面的屏幕截图中,您可以看到示例代码的结果。

【Stimulsoft Reports Java教程】运行Java Web Designer_第4张图片

下载示例

你可能感兴趣的:(产品)