翻译|使用教程|编辑:李爽夏|2018-11-16 13:50:42.000|阅读 411 次
概述:本教程介绍如何在Java报表工具中运行Flash查看器和Flash设计器。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
首先,我们需要创建动态Web项目。
接下来将Stimulsoft Java Libs添加到项目中。
您还可以转换为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()) ? mailData.getMailOptions().getEmail() : mailConf.getRecipients())); message.setSubject( StiValidationUtil.isNotNullOrEmpty( mailData.getMailOptions().getSubject()) ? 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()) ? 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); %>
在下面的屏幕截图中,您可以看到示例代码的结果。
最后,我们创建了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正版授权,请点击“”哟!
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn