翻譯|使用教程|編輯:王香|2018-11-21 10:57:02.000|閱讀 470 次
概述:本教程介紹了在Java報表工具中使用JavaServer Faces(JSF)運行Web設計器和Web查看器的基礎知識。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本教程介紹了在Java報表工具中使用JavaServer Faces(JSF)運行Web設計器和Web查看器的基礎知識。例如,打開Master-Detail報表模板以進行編輯。
首先,我們需要創建動態Web項目。

接下來將Stimulsoft Java Libs添加到項目中。

您還可以轉換為Maven項目并配置pom.xml文件以使用Maven中的庫。
<project xmlns="//maven.apache.org/POM/4.0.0"
        xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jsfstimulsoft</groupId>
    <artifactId>jsfstimulsoft</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>
然后,我們需要創建web.xml文件。在這里,我們配置StimulsoftResource的servlet,檢索內容,如* .js文件和圖像文件,該StiWebDesignerActionServlet符合Java web designer,在操作StiWebViewerActionServlet符合Java的Web瀏覽器操作,并且還配置了JavaServer Faces的。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
         xmlns="//java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="//java.sun.com/xml/ns/javaee //java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>stimulsoft</display-name>
    <welcome-file-list>
        <welcome-file>faces/designer.xhtml</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
    <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>StimulsoftDesignerAction</servlet-name>
        <servlet-class>com.stimulsoft.webdesigner.servlet.StiWebDesignerActionServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>StimulsoftDesignerAction</servlet-name>
        <url-pattern>/stimulsoft_webdesigner_action</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>  
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
</web-app>
在下一步中,我們需要實現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<Object> args) {
                            return ((String) args.get(0)).substring(
                                ((Long) args.get(1)).intValue(), ((Long) args.get(2)).intValue());
                        }
 
                        @SuppressWarnings({ "rawtypes", "unchecked" })
                        public List<Class> getParametersList() {
                            return new ArrayList<Class>(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。
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="//xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="//xmlns.jcp.org/xml/ns/javaee //xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
        version="2.2">
    <managed-bean>
        <managed-bean-name>webdesignerBean</managed-bean-name>
        <managed-bean-class>com.stimulsoft.StiWebDesignerBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>webviewerBean</managed-bean-name>
        <managed-bean-class>com.stimulsoft.StiWebViewerBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>
在下一步中,我們需要在WebContent文件夾中創建designer.xhtml頁面。
<!DOCTYPE html>
<html xmlns="//www.w3.org/1999/xhtml"
    xmlns:ui="//java.sun.com/jsf/facelets"
    xmlns:h="//java.sun.com/jsf/html"
    xmlns:f="//java.sun.com/jsf/core"
    xmlns:stiwebdesigner="//stimulsoft.com/webdesigner">
<head>
</head>
    <stiwebdesigner:webdesigner options="#{webdesignerBean.options}"
        handler="#{webdesignerBean.handler}" designerID="#{webdesignerBean.designerID}"/>
</html>
我們還需要在WebContent文件夾中創建viewer.xhtml頁面。
<!DOCTYPE html>
<html xmlns="//www.w3.org/1999/xhtml"
    xmlns:ui="//java.sun.com/jsf/facelets"
    xmlns:h="//java.sun.com/jsf/html"
    xmlns:f="//java.sun.com/jsf/core"
    xmlns:stiwebviewer="//stimulsoft.com/webviewer">
<head>
</head>
    <stiwebviewer:webviewer report="#{webviewerBean.report}" options="#{webviewerBean.options}"
        mailProperties="#{webviewerBean.mailProperties}" viewerID="#{webviewerBean.viewerID}"/>
</html>
現在,您可以將項目部署到Tomcat并運行它。

在下面的屏幕截圖中,您可以看到示例代碼的結果。


購買Stimulsoft正版授權,請點擊“”喲!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn