翻譯|行業資訊|編輯:楊鵬連|2021-02-05 11:28:05.930|閱讀 371 次
概述:本文介紹了Stimulsoft Reports.Java使用過程中常見問題解答。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Stimulsoft Reports.Java是一個專為在Java應用程序中的報表進行交互和處理的報表工具。 Java技術允許在不同的平臺、不同的操作系統和不同的硬件上使用程序。正因為這樣,Stimulsoft Reports.Java現在幾乎在任何地方都可用,同時還是一個擁有高質量的功能和用戶友好的報表工具。
立即點擊下載Stimulsoft Reports.Java v2020.2.2最新版
5.如何使用報表設計器創建示例頁面?
用報表設計器創建一個簡單的頁面。為此,請將以下庫放入WebContent \ WEB-INF \ lib \目錄中:stimulsoft.lib.jar,stimulsoft.reports-base.jar,stimulsoft.reports-report.jar,stimulsoft.reports-flex.jar, stimulsoft.reports-web.jar。結果,可以看到以下內容(下圖):

圖8.將庫放置在項目中
接下來,打開web.xml進行編輯,它應該類似于清單2:<?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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:iframe width="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的內容
<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %> <%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>
清單4.自定義Stimulsoft標簽
添加標簽<stidesignerfx:iframe />,它是html標簽iframe的類似物,并支持其所有屬性。作為應用程序部署的結果,請參見以下內容(圖9):
6.您是否有自定義標簽的描述?
從清單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.如何覆蓋加載,保存和加載自定義數據的方法?
在運行之前,應先配置應用程序。為了進行配置,使用了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 {
@Override
public void contextInitialized(final ServletContextEvent event) {
try {
// configuration application
StiFlexConfig stiConfig = initConfigWithoutDir();
// ---------------------------------------------------------
// 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);
// 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);
}
}
@Override
public 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.javapackage my.actions;
import java.io.InputStream;
import com.stimulsoft.actions.LoadAction;
import com.stimulsoft.library.LoadFileReport;
public class MyLoadAction extends LoadAction {
@Override
public 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 {
@Override
public 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 {
@Override
protected 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();
}
@Override
protected String getUserName() {
System.out.println("must override this method to specify your own user name");
// return "UserName";
return super.getUserName();
}
@Override
protected String getPassword() {
System.out.println("must override this method to specify your own password");
// return "Password";
return super.getPassword();
}
@Override
protected String getQuery() {
System.out.println("my Query " + super.getQuery());
return super.getQuery();
}
@Override
public 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(標準小部件工具包)組件?
創建一個新的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。

package my.swt;
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.stimulsoft.StiConfig;
import com.stimulsoft.StiReportSwt;
import com.stimulsoft.library.StiException;
import com.stimulsoft.widgets.DesignerFx;
public class Reports {
public static void main(String[] args) {
initReport();
Display display = new Display();
Shell shell = new Shell(display);
GridLayoutgridLayout = newGridLayout();
gridLayout.marginHeight=0;
gridLayout.marginWidth=0;
shell.setLayout(gridLayout);
shell.setText("My report");
try {
DesignerFx designer = new DesignerFx(shell, SWT.NO);
GridDatagridData = new GridData(GridData.FILL_BOTH);
designer.getComposite().setLayoutData(gridData);
} catch (StiException e) {
e.printStackTrace();
Label label = new Label(shell, SWT.NONE);
label.setLayoutData(newGridData(SWT.BEGINNING, SWT.CENTER, false, false));
label.setText("failed to create reports designer");
}
shell.setMaximized(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Private static void initReport() {
try {
File stiDir = new File("D:/stimulsoft");
StiConfig stiConfig = new StiConfig(stiDir);
StiReportSwt.init(stiConfig);
} catch (StiException e) {
e.printStackTrace();
}
}
}
可以通過事件分發和修改報告和選項。為此,請按以下方式更改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 application
File stiDir = new File("D:/Stimulsoft");
new Resource().copyAll(stiDir);
StiSwtConfig stiConfig = new StiSwtConfig(stiDir);
// ---------------------------------------------------------
// need to override the standard methods
// another comment
stiConfig.setLoadClass(MyLoadAction.class).setSaveClass(MySaveAction.class);
stiConfig.setXMLDataClass(MyXMLDataAction.class).setLoadDataClass(MyLoadDataAction.class);
stiConfig.setViewerPort(7845).setDesignerPort(8789);
// ---------------------------------------------------------
StiReportSwt.init(stiConfig);
}
也可以將設計器和查看器組件用作對話框。組件類別:com.stimulsoft.widgets.DesignerFxDialog com.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);
}
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: