专业Java报表工具Stimulsoft Reports.Java常见问题解答(二)

本文介绍了Stimulsoft Reports.Java使用过程中常见问题解答。

5.如何使用报表设计器创建示例页面/strong>

用报表设计器创建一个简单的页面。为此,请将以下库放入WebContent WEB-INF lib 目录中:stimulsoft.lib.jar,stimulsoft.reports-base.jar,stimulsoft.reports-report.jar,stimulsoft.reports-flex.jar, stimulsoft.reports-web.jar。结果,可以看到以下内容(下图):

接下来,打开web.xml进行编辑,它应该类似于清单2:

<ml version="1.0" encoding="UTF-8" gt;<web-app xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:web="http://java.sun.com/xml/ns/javaee/webapp_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee"id="WebApp_ID" version="2.5"><display-name>sti_webviewer</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- configuration, this parameter indicates the main application directory --><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>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></web-app>

清单2. web.xml的内容

其余的web.xml块保持不变,它们定义了工作所需的servlet。然后,编辑index.jsp(请参见下面的代码):

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %><%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Stimulsoft Reports.Fx for Java</title></head><body><h1 align="center">My first report!</h1><stidesignerfx:iframewidth="100%" height="90%" align="middle"styleClass="" frameborder="0" styleId=""marginheight="4" marginwidth="10" name="stiviewer"scrolling="no" style="" title="report"/></body></html>

清单3. index.jsp的内容

在JSP中添加taglib指令(清单4)。它们将与页面上的自定义标签一起使用。

<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %><%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>

清单4.自定义Stimulsoft标签

添加标签<stidesignerfx:iframe />,它是html标签iframe的类似物,并支持其所有属性。作为应用程序部署的结果,请参见以下内容(图9):

专业Java报表工具Stimulsoft Reports.Java常见问题解答(二)

6.您是否有自定义标签的描述/strong>

从清单5中可以看出,它分为两个组件:DesignerFx和ViewerFx。考虑一个DesignerFx组件。对于ViewerFx,其工作方式相同。

<stidesignerfx:link text="a link for jumping to the Designer"/><stidesignerfx:button value="a button for jumping to the Designer"/><stidesignerfx:frame title="analog of the html tag frame which contains a Designer"/><stidesignerfx:iframe title="analog of the html tag iframe which contains a Designer"/>

所有这些都是类似HTML标记的类似物,支持所有属性。扩展了标准属性列表,以显示报告和设置报告变量。report =“ SimpleList.mrt”属性将打开名称为SimpleList.mrt的报告。报表中的变量可以通过两种方式传递:

以以下格式将variableStr属性的值设置为字符串:“ Variable1 = value1&Variable2 = value2”。在这种情况下,将在报表中传递两个具有value1的变量Variable1和具有value2的Variable2。例如,您需要编辑index.jsp文件以通过单击按钮打开名为MyFirstReport.mrt的报告,并且MyVar报告变量具有stidesignerfx值(清单6)。

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %><%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %><%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %><html><head><title>Report</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><stidesignerfx:button value="Run the report designer" report="MyFirstReport.mrt" variableStr="MyVar=stidesignerfx" /></html>

也可以将参数作为Map <string,string =>传递给报表。重新设计我们的网页,如下所示(清单6)。在这种情况下,名称为MyFirstReport.mrt的报表将被加载到报表中,并将两个参数传递到该报表中。

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %><%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %><%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %><html><head><title>Report</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><%Map<String, String>variableMap= new HashMap<String, String>();variableMap.put("Variable1", "var1");variableMap.put("Variable2", "var2");request.setAttribute("myMap", variableMap);%><stidesignerfx:iframe report="MyFirstReport.mrt" variableMap="myMap"width="100%" height="100%" align="right"styleClass="" frameborder="0" styleId=""marginheight="1" marginwidth="1" name="stidesignerfx"scrolling="no" style="" title="report" /></body></html>

此处的数据作为HashMap传递,此参数应设置为请求或会话,并将其存在的键作为variableMap属性传递给标签。不允许应用两个属性variableMap和variableStr。

7.如何覆盖加载,保存和加载自定义数据的方法/strong>

在运行之前,应先配置应用程序。为了进行配置,使用了web.xml中指定的

my.servlet.ApplicationInitializer类。package my.servlet;import java.io.IOException;import java.util.Properties;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import my.actions.MyLoadAction;import my.actions.MyLoadDataAction;import my.actions.MyLocalizationAction;import my.actions.MyMailAction;import my.actions.MyRenderReportAction;import my.actions.MySaveAction;import com.stimulsoft.base.exception.StiException;import com.stimulsoft.flex.StiFlexConfig;/** * Application initialization. */public class ApplicationInitializer implements ServletContextListener {@Overridepublic void contextInitialized(final ServletContextEvent event) {try {// configuration applicationStiFlexConfig stiConfig = initConfigWithoutDir();// ---------------------------------------------------------// need to override the standard methods// another commentstiConfig.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);// set variable in servlet context attribute// Map<String, String> myVariableMap = new HashMap<String, String>();// myVariableMap.put("Variable1", "myVariableMap");// event.getServletContext().setAttribute("myMap", myVariableMap);} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void contextDestroyed(final ServletContextEvent event) {// empty}public StiFlexConfig initConfigWithoutDir() throws StiException, IOException {Properties properties = new Properties();// load your own Properties;// InputStream inStream = getClass().getResourceAsStream("RESOURCE_PATH");// properties.load(inStream);return new StiFlexConfig(properties);}}

将在其中定义带有文件stimulsoft.properties的主应用程序目录。为了保存或加载自己的报告,有必要在配置中指定这些类,就像指定可以从xml加载数据的类一样。类如下:列出MyLoadAction.java

package my.actions;import java.io.InputStream;import com.stimulsoft.actions.LoadAction;import com.stimulsoft.library.LoadFileReport;public class MyLoadAction extends LoadAction {@Overridepublic InputStream load(String repotrName) {System.out.println("my load class" + repotrName);return new LoadFileReport().getReport(repotrName);}}
package my.actions;import com.stimulsoft.flex.StiSaveAction;import com.stimulsoft.flex.utils.StiOperationResult;import com.stimulsoft.flex.utils.StiSaveLoadFileReport;public class MySaveAction extends StiSaveAction {@Overridepublic StiOperationResult save(String report, String reportName, boolean newReportFlag) {System.out.println("must override this method to specify your own save report");return new StiSaveLoadFileReport().save(report, reportName, newReportFlag);}}
package my.actions;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;import com.stimulsoft.flex.StiLoadDataAction;public class MyLoadDataAction extends StiLoadDataAction {@Overrideprotected String getConnectionString() {System.out.println("must override this method to specify your own connection string");// return// "Data Source=localhost\SQLEXPRESS;Initial Catalog=Mybase;User ID=UserName; Password=Password;";return super.getConnectionString();}@Overrideprotected String getUserName() {System.out.println("must override this method to specify your own user name");// return "UserName";return super.getUserName();}@Overrideprotected String getPassword() {System.out.println("must override this method to specify your own password");// return "Password";return super.getPassword();}@Overrideprotected String getQuery() {System.out.println("my Query " + super.getQuery());return super.getQuery();}@Overridepublic Connection getConnection() throws ClassNotFoundException, SQLException {System.out.println("must override this method to specify your own connection");boolean overrideByConnectionString = getConnectionString() != null&& getConnectionString().equals("needOverride");boolean overrideByDataSource = getDataSourceName() != null&& getDataSourceName().equals("DataSourceOverride");if (overrideByConnectionString || overrideByDataSource) {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");Properties info = new Properties();info.setProperty("user", "test");info.setProperty("password", "test");String connectionString = "jdbc:sqlserver://localhost\SQLEXPRESS1:1433;databaseName=mybase;";return DriverManager.getConnection(connectionString, info);} else {return super.getConnection();}}}

8.如何使用SWT(标准小部件工具包)组件/strong>

创建一个新的Java项目。为此,请选择文件>新建> Java项目。在项目向导中,输入项目的名称,例如“ swtfx”。在项目属性中,将一个关系添加到“ Standard Widget Toolkit(SWT)”库,以设计应用程序GUI。还要添加用于处理报告的库:stimulsoft.reports.fx-core-2011.1.1000.jarstimulsoft.reports.fx-designer-2011.1.1000.jar,stimulsoft.reports.fx-viewer-2011.1.1000.jar,stimulsoft。 reports.fx-swt-2011.1.1000.jar。

可以通过事件分发和修改报告和选项。为此,请按以下方式更改Reports.java中的代码。

package my.swt;import java.io.File;import java.util.HashMap;import java.util.Map;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.SelectionListener;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import com.stimulsoft.StiConfig;import com.stimulsoft.StiReportSwt;import com.stimulsoft.library.StiException;import com.stimulsoft.widgets.ViewerFx;public class Reports {Display display = new Display();Shell shell = new Shell(display);private final ViewerFx viewer;public Reports() throws StiException {shell.setLayout(new GridLayout(2, false));Map<String, String> params = new HashMap<String, String>();params.put("Variable1", "My parameter");viewer = new ViewerFx(shell, SWT.NO, "SimpleList.mrt", params);GridDatagridData = new GridData(GridData.FILL_BOTH);viewer.getComposite().setLayoutData(gridData);Button button = new Button(shell, SWT.PUSH);button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));button.setText("update report");button.addSelectionListener(new SelectionListener() {public void widgetSelected(SelectionEvent arg0) {try {Map<String, String>paramsNew = new HashMap<String,String>();paramsNew.put("Variable1", "My new parameter");viewer.assignReport("SimpleList.mrt", paramsNew);} catch (Exception e) {e.printStackTrace();}}public void widgetDefaultSelected(SelectionEvent arg0) {}});shell.setMaximized(true);shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}public static void main(String[] args) {try {initReport();new Reports();} catch (StiException e) {e.printStackTrace();}}private static void initReport() throws StiException {StiConfigstiConfig = new StiConfig(new File("D:/stimulsoft"));StiReportSwt.init(stiConfig);}}

这将导致以下结果:

¥°

单击更新报告按钮后,报告将被更新:

°¥°

使用呈现报告的组件时,应在运行该应用程序之前配置该应用程序。应该创建以下方法。

private static void initReport() throws StiException {File stiDir = new File("D:/Stimulsoft");new Resource().copyAll(stiDir);StiSwtConfig stiConfig = new StiSwtConfig(stiDir);StiReportSwt.init(stiConfig);}

将在其中 定义带有stimulsoft.properties文件的主应用程序目录。初始化与针对Web应用程序描述的初始化相同。还可以为设计者和查看者覆盖应用程序的端口。例如:

private static void initReport() throws StiException {// Initialization is identical to that described for the web applicationFile stiDir = new File("D:/Stimulsoft");new Resource().copyAll(stiDir);StiSwtConfig stiConfig = new StiSwtConfig(stiDir);// ---------------------------------------------------------// need to override the standard methods// another commentstiConfig.setLoadClass(MyLoadAction.class).setSaveClass(MySaveAction.class);stiConfig.setXMLDataClass(MyXMLDataAction.class).setLoadDataClass(MyLoadDataAction.class);stiConfig.setViewerPort(7845).setDesignerPort(8789);// ---------------------------------------------------------StiReportSwt.init(stiConfig);}

也可以将设计器和查看器组件用作对话框。组件类别:

com.stimulsoft.widgets.DesignerFxDialogcom.stimulsoft.widgets.ViewerFxDialog

使用Designer组件的示例:

package my.swt;import java.io.File;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.SelectionListener;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import com.stimulsoft.StiConfig;import com.stimulsoft.StiReportSwt;import com.stimulsoft.library.StiException;import com.stimulsoft.widgets.DesignerFxDialog;public class DialogReports {public static void main(String[] args) {try {initReport();Display display = new Display();Shell shell = new Shell(display);shell.setLayout(new GridLayout());final DesignerFxDialog dialog = new DesignerFxDialog(shell,SWT.RESIZE | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);dialog.setSize(1250, 800);Button button = new Button(shell, SWT.PUSH);button.setText("open dialog");button.addSelectionListener(new SelectionListener() {public void widgetSelected(SelectionEvent arg0) {try {dialog.open("SimpleList.mrt");} catch (StiException e) {}}public void widgetDefaultSelected(SelectionEvent arg0) {}});shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();} catch (StiException e) {e.printStackTrace();}}private static void initReport() throws StiException {File stiDir = new File("D:/stimulsoft");StiConfig stiConfig = new StiConfig(stiDir);StiReportSwt.init(stiConfig);}}

产品介绍 | 下载试用 | 优惠活动 | 在线客服

标签:

来源:慧都

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

上一篇 2021年1月3日
下一篇 2021年1月3日

相关推荐

发表回复

登录后才能评论