翻譯|使用教程|編輯:李爽夏|2018-11-16 13:50:42.000|閱讀 457 次
概述:本教程介紹如何在Java報(bào)表工具中運(yùn)行Flash查看器和Flash設(shè)計(jì)器。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
首先,我們需要?jiǎng)?chuàng)建動(dòng)態(tài)Web項(xiàng)目。
接下來(lái)將Stimulsoft Java Libs添加到項(xiàng)目中。
您還可以轉(zhuǎn)換為Maven項(xiàng)目并配置pom.xml文件以使用Maven中的庫(kù)。
4.0.0webfxwebfx0.0.1-SNAPSHOTwarsrcmaven-compiler-plugin3.5.11.61.6com.stimulsoftstimulsoft-reports-libs2017.1.1
然后,我們需要在WebContent / WEB-INF文件夾中創(chuàng)建web.xml文件。在這里,我們配置需要初始化Flash查看器和Flash設(shè)計(jì)器的StiDesignerFxServlet,StiViewerFxServlet和ApplicationInitializer。
sti_fx_webindex.jsp60StimulsoftDesignerFxcom.stimulsoft.web.servlet.StiDesignerFxServletStimulsoftDesignerFx/stimulsoft_designerfxStimulsoftViewerFxcom.stimulsoft.web.servlet.StiViewerFxServletStimulsoftViewerFx/stimulsoft_viewerfxcom.stimulsoft.ApplicationInitializer
在下一步中,我們需要實(shí)現(xiàn)ApplizationInitializer,在服務(wù)器啟動(dòng)時(shí)初始化Flash Viewer和Flash Designer。我們可以用它修改屬性,例如設(shè)置DateFormat,Engine.Type等。
此外,還需要指定下一個(gè)類(lèi) - 在啟動(dòng)時(shí)加載報(bào)表的類(lèi),用于保存報(bào)表的類(lèi),用于加載數(shù)據(jù)的類(lèi),本地化類(lèi),電子郵件發(fā)件人類(lèi)和用于呈現(xiàn)報(bào)表的類(lèi)。此外,此示例教程還演示了如何使用Flash查看器和Flash設(shè)計(jì)器的自定義屬性。
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報(bào)告的MyLoadAction.class。此外,在此類(lèi)中,我們將數(shù)據(jù)庫(kù)添加到報(bào)表中。
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,請(qǐng)定義有助于加載數(shù)據(jù)的MyLoadDataAction.class。對(duì)于其他連接,您不應(yīng)使用此類(lèi)。
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)); } }
定義用于通過(guò)電子郵件發(fā)送報(bào)告文件的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,用于根據(jù)需要自定義報(bào)表呈現(xiàn)。在此示例中,我們添加了自定義subStr()函數(shù)的實(shí)現(xiàn)。
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); } }
定義用于保存報(bào)告模板的MySaveAction.class。
public class MySaveAction extends StiSaveAction { @Override public StiOperationResult save(String report, String reportName, boolean newReportFlag) { return new StiSaveLoadFileReport().save(report, reportName, newReportFlag); } }
現(xiàn)在我們需要?jiǎng)?chuàng)建designer.jsp頁(yè)面,在其中顯示Flash設(shè)計(jì)器。在這里,我們加載報(bào)表模板,添加設(shè)計(jì)器組件的Theme屬性并添加變量值。在此之后,將Flash設(shè)計(jì)器標(biāo)簽放到此jsp頁(yè)面。
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); %>
在下面的屏幕截圖中,您可以看到示例代碼的結(jié)果。
最后,我們創(chuàng)建了viewer.jsp頁(yè)面,在其中顯示Flash查看器。在這里,我們可以配置查看器屬性,例如隱藏“打開(kāi)”按鈕并添加變量值。最后,將Flash查看器標(biāo)記放到此jsp頁(yè)面。
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); %>
在下面的屏幕截圖中,您可以看到示例代碼的結(jié)果。
購(gòu)買(mǎi)Stimulsoft正版授權(quán),請(qǐng)點(diǎn)擊“”喲!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn