【Stimulsoft Reports Java教程】运行Java ViewerFx和DesignerFx

本教程介绍如何在Java报表工具中运行Flash查看器和Flash设计器。

下载Stimulsoft Reports Java最新版本

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

【Stimulsoft Reports Java教程】运行Java ViewerFx和DesignerFx

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

【Stimulsoft Reports Java教程】运行Java ViewerFx和DesignerFx

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

4.0.0webfxwebfx0.0.1-SNAPSHOTwarsrcmaven-compiler-plugin3.5.11.61.6com.stimulsoftstimulsoft-reports-libs2017.1.1

然后,我们需要在WebContent / WEB-INF文件夹中创建web.xml文件。在这里,我们配置需要初始化Flash查看器和Flash设计器的StiDesignerFxServlet,StiViewerFxServlet和ApplicationInitializer。

sti_fx_webindex.jsp60StimulsoftDesignerFxcom.stimulsoft.web.servlet.StiDesignerFxServletStimulsoftDesignerFx/stimulsoft_designerfxStimulsoftViewerFxcom.stimulsoft.web.servlet.StiViewerFxServletStimulsoftViewerFx/stimulsoft_viewerfxcom.stimulsoft.ApplicationInitializer

 

在下一步中,我们需要实现ApplizationInitializer,在服务器启动时初始化Flash Viewer和Flash Designer。我们可以用它修改属性,例如设置DateFormat,Engine.Type等。

此外,还需要指定下一个类 – 在启动时加载报表的类,用于保存报表的类,用于加载数据的类,本地化类,电子邮件发件人类和用于呈现报表的类。此外,此示例教程还演示了如何使用Flash查看器和Flash设计器的自定义属性。

public class ApplicationInitializer implements ServletContextListener {     @Override    public void contextInitialized(final ServletContextEvent event) {        try {            // configuration application            StiFlexConfig stiConfig = initConfig();            // Setup custom properties            stiConfig.getProperties().setProperty("Engine.Type", "Java");            stiConfig.getProperties().setProperty("Appearance.DateFormat", "yyyy");            stiConfig.getProperties().setProperty("Appearance.VariablesPanelColumns", "3");            // stiConfig.getProperties().setProperty("Designer.Dictionary.AllowModifyConnections",            // "False");            // stiConfig.getProperties().setProperty("Designer.Dictionary.AllowModifyDataSources",            // "False");            // stiConfig.getProperties().setProperty("Viewer.Toolbar.ShowSendEMailButton", "True");            // ---------------------------------------------------------            // need to override the standard methods            // another comment            stiConfig.setLoadClass(MyLoadAction.class);            stiConfig.setSaveClass(MySaveAction.class);            stiConfig.setLoadDataClass(MyLoadDataAction.class);            stiConfig.setMailAction(MyMailAction.class);            stiConfig.setLocalizationAction(MyLocalizationAction.class);            stiConfig.setRenderReportAction(MyRenderReportAction.class);             StiFlexConfig.init(stiConfig);        } catch (Exception e) {            throw new RuntimeException(e);        }    }     @Override    public void contextDestroyed(final ServletContextEvent event) {        // empty    }     public StiFlexConfig initConfig() throws StiException, IOException {        // Properties properties = new Properties();        // load your own Properties;        // InputStream inStream = getClass().getResourceAsStream("RESOURCE_PATH");        // properties.load(inStream);        // return new StiFlexConfig(properties);        return new StiFlexConfig();    } }

 

定义需要加载hte报告的MyLoadAction.class。此外,在此类中,我们将数据库添加到报表中。

public class MyLoadAction extends StiLoadAction {     @Override    public InputStream load(String repotrName) {        try {            StiReport report = StiSerializeManager.deserializeReport(new File(repotrName));            StiXmlDatabase xmlDatabase = new StiXmlDatabase("Demo", "/Data/Demo.xsd", "/Data/Demo.xml");            report.getDictionary().getDatabases().add(xmlDatabase);            ByteArrayOutputStream out = new ByteArrayOutputStream();            StiSerializeManager.serializeReport(report, out);            return new ByteArrayInputStream(out.toByteArray());        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();            return null;        }    } }

 

如果使用Jdbc Connection,请定义有助于加载数据的MyLoadDataAction.class。对于其他连接,您不应使用此类。

public class MyLoadDataAction extends StiLoadDataAction {     @Override    protected String getConnectionString() {        return super.getConnectionString();    }     @Override    protected String getUserName() {        return super.getUserName();    }     @Override    protected String getPassword() {        return super.getPassword();    }     @Override    public String getQuery() {        return super.getQuery();    }     @Override    public Connection getConnection() throws ClassNotFoundException, SQLException {        boolean overrideByConnectionString = getConnectionString() != null &&             getConnectionString().equals(StiAbstractAdapter.OVERRIDE_CONNECTION_STRING);        boolean overrideByDataSource = getDataSourceName() != null && getDataSourceName().equals("DataSourceOverride");        if (overrideByConnectionString || overrideByDataSource) {            Class.forName("com.mysql.jdbc.Driver");            Properties info = new Properties();            info.setProperty("driver", "com.mysql.jdbc.Driver");            info.setProperty("user", "root");            info.setProperty("password", "password");            String connectionString = "jdbc:mysql://localhost/sakila";            return DriverManager.getConnection(connectionString, info);        } else {            return super.getConnection();        }    } }

 

定义需要检索可用本地化并加载必要的本地化文件的MyLocalizationAction.class。

public class MyLocalizationAction extends StiLocalizationAction {     @Override    public ListgetLocalizations() throws StiException, FileNotFoundException {        Listlist = new ArrayList();        File localizationDir = getLocalizationDir();        if (localizationDir.exists()) {            IteratoriterateLocalization = StiFileUtil.iterateFiles(localizationDir, new String[] { "xml" }, false);            for (; iterateLocalization.hasNext();) {                File fileLoc = iterateLocalization.next();                InputStream is = new BufferedInputStream(new FileInputStream(fileLoc));                StiLocalizationInfo localization = StiXmlMarshalUtil.unmarshal(is, StiLocalizationInfo.class);                localization.setKey(fileLoc.getName());                list.add(localization);            }        }        return list;    }     @Override    protected File getLocalizationDir() {        return new File("Localization");    }     @Override    public InputStream getLocalization(String key) throws StiException, FileNotFoundException {        File file = new File(getLocalizationDir(), key);        return new BufferedInputStream(new FileInputStream(file));    } }

 

定义用于通过电子邮件发送报告文件的MyMailAction.class。

public class MyMailAction extends StiMailAction {     @Override    public void init(StiMailData mailData, StiMailProperties mailConf) {        this.mailData = mailData;        this.mailConf = mailConf;        session = getSession();    }     @Override    protected Session getSession() {        Properties props = getProperties();        return Session.getInstance(props);    }     @Override    protected Properties getProperties() {        Properties props = new Properties();        props.put("mail.smtp.auth", "true");        props.put("mail.smtp.starttls.enable", "true");        return props;    }     @Override    protected Message getMessage() throws MessagingException {        Message message = new MimeMessage(session);        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailConf.getFrom()));        message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(        StiValidationUtil.isNotNullOrEmpty(            mailData.getMailOptions().getEmail()) nbsp;mailData.getMailOptions().getEmail() : mailConf.getRecipients()));        message.setSubject(            StiValidationUtil.isNotNullOrEmpty(                mailData.getMailOptions().getSubject()) nbsp;mailData.getMailOptions().getSubject() : mailConf.getSubject());        BodyPart text = getTextPart();        BodyPart body = getFilePart();         Multipart mp = new MimeMultipart();        mp.addBodyPart(text);        mp.addBodyPart(body);         message.setContent(mp);        return message;    }     @Override    protected BodyPart getTextPart() throws MessagingException {        MimeBodyPart text = new MimeBodyPart();        text.setText(StiValidationUtil.isNotNullOrEmpty(            mailData.getMailOptions().getMessage()) nbsp;mailData.getMailOptions().getMessage() : mailConf.getBody(),            "UTF-8", "plain");        return text;    }     @Override    protected BodyPart getFilePart() throws MessagingException {        PreencodedMimeBodyPart body = new PreencodedMimeBodyPart("base64");        body.setFileName(mailData.getMailOptions().getFileName());        body.setContent(mailData.getData(), mailData.getMIMEType());        return body;    }     private Transport getTransport() throws MessagingException {        Transport transport = session.getTransport("smtp");        transport.connect(mailConf.getHost(), mailConf.getSmtpPort(), mailConf.getUserName(), mailConf.getPassword());        return transport;    }     @Override    public void sendMessage() throws MessagingException {        Message message = getMessage();        Transport transport = getTransport();        transport.sendMessage(message, message.getAllRecipients());        transport.close();    } }

 

定义MyRenderReportAction.class,用于根据需要自定义报表呈现。在此示例中,我们添加了自定义subStr()函数的实现。

public class MyRenderReportAction extends StiRenderReportAction {     @Override    public StiReport render(StiReport report) throws IOException, StiException {        // Add custom function        report.getCustomFunctions().add(new StiCustomFunction() {            public Object invoke(Listargs) {                return ((String) args.get(0)).substring(((Long) args.get(1)).intValue(), ((Long) args.get(2)).intValue());            }             @SuppressWarnings({ "rawtypes" })            public ListgetParametersList() {                return new ArrayList(Arrays.asList(String.class, Long.class, Long.class));            }             public String getFunctionName() {                return "subStr";            }        });        return super.render(report);    } }

 

定义用于保存报告模板的MySaveAction.class。

public class MySaveAction extends StiSaveAction {     @Override    public StiOperationResult save(String report, String reportName, boolean newReportFlag) {        return new StiSaveLoadFileReport().save(report, reportName, newReportFlag);    } }

 

现在我们需要创建designer.jsp页面,在其中显示Flash设计器。在这里,我们加载报表模板,添加设计器组件的Theme属性并添加变量值。在此之后,将Flash设计器标签放到此jsp页面。

Report<%    final String reportPath = request.getSession().getServletContext().getRealPath("/reports/SimpleList.mrt");     Properties props = new Properties();    props.put("Theme","Office2013");    request.setAttribute("props", props);    MapvariableMap = new HashMap();    variableMap.put("Variable1","variable");    request.setAttribute("map",variableMap);    request.setAttribute("props",props);%>

 

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

【Stimulsoft Reports Java教程】运行Java ViewerFx和DesignerFx

最后,我们创建了viewer.jsp页面,在其中显示Flash查看器。在这里,我们可以配置查看器属性,例如隐藏“打开”按钮并添加变量值。最后,将Flash查看器标记放到此jsp页面。

Stimulsoft report<%    final String reportPath = request.getSession().getServletContext().getRealPath("/reports/SimpleList.mrt");    Properties props = new Properties();    props.put("Viewer.Toolbar.ShowOpenButton","False");    request.setAttribute("props", props);     MapvariableMap = new HashMap();    variableMap.put("Variable1", "St");    request.setAttribute("map",variableMap);    request.setAttribute("props",props);%>

 

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

【Stimulsoft Reports Java教程】运行Java ViewerFx和DesignerFx

下载示例

购买Stimulsoft正版授权,请点击“咨询在线客服”哟!

标签:HTML5报表JavaStimulsoft

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年10月12日
下一篇 2018年10月12日

相关推荐

发表回复

登录后才能评论