【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer

下载Stimulsoft Reports Java最新版本

本教程介绍了在Java报表工具中使用JavaServer Faces(JSF)运行Web设计器和Web查看器的基础知识。例如,打开Master-Detail报表模板以进行编辑。

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

【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer_第1张图片

接下来将Stimulsoft Java Libs添加到项目中。

【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer_第2张图片

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


    4.0.0
    jsfstimulsoft
    jsfstimulsoft
    0.0.1-SNAPSHOT
    war
    
        src
        
            
                maven-compiler-plugin
                3.5.1
                
                    1.6
                    1.6
                
            
        
    
    
        
            com.stimulsoft
            stimulsoft-reports-libs
            2017.1.1
        
    
 

然后,我们需要创建web.xml文件。在这里,我们配置StimulsoftResource的servlet,检索内容,如* .js文件和图像文件,该StiWebDesignerActionServlet符合Java web designer,在操作StiWebViewerActionServlet符合Java的Web浏览器操作,并且还配置了JavaServer Faces的。



    stimulsoft
    
        faces/designer.xhtml
    
    
        60
    
    
        StimulsoftResource
        com.stimulsoft.web.servlet.StiWebResourceServlet
    
    
        StimulsoftResource
        /stimulsoft_web_resource/*
    
    
        StimulsoftDesignerAction
        com.stimulsoft.webdesigner.servlet.StiWebDesignerActionServlet
    
    
        StimulsoftDesignerAction
        /stimulsoft_webdesigner_action
    
    
        StimulsoftAction
        com.stimulsoft.webviewer.servlet.StiWebViewerActionServlet
    
    
        StimulsoftAction
        /stimulsoft_webviewer_action
      
    
        Faces Servlet
        javax.faces.webapp.FacesServlet
        1
    
    
        Faces Servlet
        /faces/*
    
    
        State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2
        javax.faces.STATE_SAVING_METHOD
        client
    
    
        javax.servlet.jsp.jstl.fmt.localizationContext
        resources.application
    
    
        com.sun.faces.config.ConfigureListener
    

在下一步中,我们需要实现StiWebDesignerBean来填充报表数据并保存/加载报表模板。

public class StiWebDesignerBean {
    StiWebDesignerOptions options;
    String designerID = "StimulsoftWebDesigner";
 
    /**
     * @return the handler
     */
    public StiWebDesigerHandler getHandler() {
        StiWebDesigerHandler handler = new StiWebDesigerHandler() {
            public StiReport getEditedReport(HttpServletRequest request) {
                try {
                    String reportPath = request.getSession().getServletContext().getRealPath("/reports/Master-Detail.mrt");
                    String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
                    String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
                    StiReport report = StiSerializeManager.deserializeReport(new File(reportPath));
                    report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
 
                    report.getCustomFunctions().add(new StiCustomFunction() {
                        public Object invoke(List args) {
                            return ((String) args.get(0)).substring(
                                ((Long) args.get(1)).intValue(), ((Long) args.get(2)).intValue());
                        }
 
                        @SuppressWarnings({ "rawtypes", "unchecked" })
                        public List getParametersList() {
                            return new ArrayList(Arrays.asList(String.class, Long.class, Long.class));
                        }
 
                        public String getFunctionName() {
                            return "subStr";
                        }
                    });
                    return report;
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                return null;
            }
 
            public void onOpenReportTemplate(StiReport report, HttpServletRequest request) {
                String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
                String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
                report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
            }
 
            public void onNewReportTemplate(StiReport report, HttpServletRequest request) {
                String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
                String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
                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();
                }
            }
 
            public void onSaveReportTemplate(StiReport report, String reportName, HttpServletRequest request) {
                try {
                    String savePath = request.getSession().getServletContext().getRealPath("/save/");
                    FileOutputStream fos = new FileOutputStream(savePath + reportName);
                    StiSerializeManager.serializeReport(report, fos);
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        return handler;
    }
 
    /**
     * @return the options
     */
    public StiWebDesignerOptions getOptions() {
        options = new StiWebDesignerOptions();
        return options;
    }
 
    /**
     * @return the designerID
     */
    public String getDesignerID() {
        return designerID;
    }
} 
  

接下来,我们需要实现StiWebViewerBean。在这里,我们加载Master-Detail.mrt报告模板文件并呈现报表。我们还可以配置Web查看器,例如将背景颜色设置为灰色。

public class StiWebViewerBean {
    StiReport report;
    StiWebViewerOptions options;
    String viewerID = "StimulsoftWebViewer";
    StiMailProperties mailProperties;
 
    /**
     * @return the report
     * @throws StiDeserializationException
     * @throws SAXException
     * @throws IOException
     */
    public StiReport getReport() throws IOException, SAXException, StiDeserializationException {
        if (report == null) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
            String reportPath = session.getServletContext().getRealPath("/reports/Master-Detail.mrt");
            report = StiSerializeManager.deserializeReport(new File(reportPath));
            String xmlPath = session.getServletContext().getRealPath("/data/Demo.xml");
            String xsdPath = session.getServletContext().getRealPath("/data/Demo.xsd");
            report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
            report.render();
        }
        return report;
    }
 
    /**
     * @param report
     *            the report to set
     */
    public void setReport(StiReport report) {
        this.report = report;
    }
 
    /**
     * @return the options
     */
    public StiWebViewerOptions getOptions() {
        options = new StiWebViewerOptions();
        options.getAppearance().setBackgroundColor(StiColorEnum.Gray.color());
        // options.getToolbar().setVisible(false);
        return options;
    }
 
    /**
     * @param options
     *            the options to set
     */
    public void setOptions(StiWebViewerOptions options) {
        this.options = options;
    }
 
    /**
     * @return the viewerID
     */
    public String getViewerID() {
        return viewerID;
    }
 
    /**
     * @param viewerID
     *            the viewerID to set
     */
    public void setViewerID(String viewerID) {
        this.viewerID = viewerID;
    }
 
    /**
     * @return the mailProperties
     */
    public StiMailProperties getMailProperties() {
        mailProperties = new StiMailProperties();
        return mailProperties;
    }
 
    /**
     * @param mailProperties
     *            the mailProperties to set
     */
    public void setMailProperties(StiMailProperties mailProperties) {
        this.mailProperties = mailProperties;
    }
}

然后,配置faces-config.xml文件并添加必要的bean。



    
        webdesignerBean
        com.stimulsoft.StiWebDesignerBean
        session
    
    
        webviewerBean
        com.stimulsoft.StiWebViewerBean
        session
    

在下一步中,我们需要在WebContent文件夹中创建designer.xhtml页面。





    

我们还需要在WebContent文件夹中创建viewer.xhtml页面。





    

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

【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer_第3张图片

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

【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer_第4张图片

【Stimulsoft Reports Java教程】使用JavaServer Faces运行Web Designer和Web Viewer_第5张图片

下载示例

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