翻譯|使用教程|編輯:李爽夏|2018-11-16 13:50:42.000|閱讀 457 次
概述:本教程介紹如何在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@fc6vip.cn