This tutorial shows the basics of running the web designer and web viewer with Jakarta Faces in the reporting tool for Java. For example, open the Master-Detail report template for editing.
First, we need to create the Dynamic Web Project.
Next add the Stimulsoft Java Libs to the project.
Also you can convert to Maven project and configure the
pom.xml file to use libs from
Maven:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jsfstimulsoft</groupId>
<artifactId>jsfstimulsoft</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.stimulsoft</groupId>
<artifactId>stimulsoft-reports-libs</artifactId>
<version>2023.1.8</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.faces</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
Then, we need to create the 'web.xml' file. Here we configure the
StimulsoftResource
servlet that retrieves the content such as *.js and images files, the
StiWebDesignerActionServlet
that operate with the java web designer, the
StiWebViewerActionServlet
that operate with the java web viewer, and also configure the Jakarta Faces:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<display-name>stimulsoft</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<servlet>
<servlet-name>StimulsoftResource</servlet-name>
<servlet-class>com.stimulsoft.web.servlet.StiWebResourceServletJk</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StimulsoftResource</servlet-name>
<url-pattern>/stimulsoft_web_resource/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>StimulsoftDesignerAction</servlet-name>
<servlet-class>com.stimulsoft.webdesigner.servlet.StiWebDesignerActionServletJk</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StimulsoftDesignerAction</servlet-name>
<url-pattern>/stimulsoft_webdesigner_action</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>StimulsoftAction</servlet-name>
<servlet-class>com.stimulsoft.webviewer.servlet.StiWebViewerActionServletJk</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StimulsoftAction</servlet-name>
<url-pattern>/stimulsoft_webviewer_action</url-pattern>
</servlet-mapping>
</web-app>
In the next step, we need to implement the
StiWebDesignerBean
for populating the report data and save/load the report template:
@Named("WebDesignerBean")
@SessionScoped
public class StiWebDesignerBean {
StiWebDesignerOptions options;
String designerID = "StimulsoftWebDesigner";
/**
* @return the handler
*/
public StiWebDesigerHandler getHandler() {
StiWebDesigerHandlerJk handler = new StiWebDesigerHandlerJk() {
public StiReport getEditedReport(HttpServletRequest request) {
try {
String reportPath = request.getSession().getServletContext().getRealPath("/reports/Master-Detail.mrt");
String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
StiReport report = StiSerializeManager.deserializeReport(new File(reportPath));
report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
report.getCustomFunctions().add(new StiCustomFunction() {
public Object invoke(List<Object> args) {
return ((String) args.get(0)).substring(
((Long) args.get(1)).intValue(), ((Long) args.get(2)).intValue());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Class> getParametersList() {
return new ArrayList<Class>(Arrays.asList(String.class, Long.class, Long.class));
}
public String getFunctionName() {
return "subStr";
}
});
return report;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void onOpenReportTemplate(StiReport report, HttpServletRequest request) {
String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
}
public void onNewReportTemplate(StiReport report, HttpServletRequest request) {
String xmlPath = request.getSession().getServletContext().getRealPath("/data/Demo.xml");
String xsdPath = request.getSession().getServletContext().getRealPath("/data/Demo.xsd");
report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
try {
StiXmlTableFildsRequest tables = StiDataColumnsUtil.parceXSDSchema(new FileInputStream(xsdPath));
for (StiXmlTable table : tables.getTables()) {
StiDataTableSource tableSource = new StiDataTableSource(
"Demo." + table.getName(), table.getName(), table.getName());
tableSource.setColumns(new StiDataColumnsCollection());
for (StiSqlField field : table.getColumns()) {
StiDataColumn column = new StiDataColumn(
field.getName(), field.getName(), field.getSystemType());
tableSource.getColumns().add(column);
}
tableSource.setDictionary(report.getDictionary());
report.getDictionary().getDataSources().add(tableSource);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onSaveReportTemplate(StiReport report, String reportName, HttpServletRequest request) {
try {
String savePath = request.getSession().getServletContext().getRealPath("/save/");
FileOutputStream fos = new FileOutputStream(savePath + reportName);
StiSerializeManager.serializeReport(report, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
return handler;
}
/**
* @return the options
*/
public StiWebDesignerOptions getOptions() {
options = new StiWebDesignerOptions();
return options;
}
/**
* @return the designerID
*/
public String getDesignerID() {
return designerID;
}
}
Next, we need to implement the
StiWebViewerBean
. Here we load the Master-Detail.mrt report template file and render the report. Also we can configure the web viewer, for example set the background color to Gray:
@Named("WebViewerBean")
@SessionScoped
public class StiWebViewerBean {
StiReport report;
StiWebViewerOptions options;
String viewerID = "StimulsoftWebViewer";
StiMailProperties mailProperties;
/**
* @return the report
*/
public StiReport getReport() {
if (report == null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
String reportPath = session.getServletContext().getRealPath("/reports/Master-Detail.mrt");
try {
report = StiSerializeManager.deserializeReport(new File(reportPath));
String xmlPath = session.getServletContext().getRealPath("/data/Demo.xml");
String xsdPath = session.getServletContext().getRealPath("/data/Demo.xsd");
report.getDictionary().getDatabases().add(new StiXmlDatabase("Demo", xsdPath, xmlPath));
report.render();
} catch (Exception e) {
e.printStackTrace();
}
}
return report;
}
/**
* @param report
* the report to set
*/
public void setReport(StiReport report) {
this.report = report;
}
/**
* @return the options
*/
public StiWebViewerOptions getOptions() {
options = new StiWebViewerOptions();
options.getAppearance().setBackgroundColor(StiColorEnum.Gray.color());
// options.getToolbar().setVisible(false);
return options;
}
/**
* @param options
* the options to set
*/
public void setOptions(StiWebViewerOptions options) {
this.options = options;
}
/**
* @return the viewerID
*/
public String getViewerID() {
return viewerID;
}
/**
* @param viewerID
* the viewerID to set
*/
public void setViewerID(String viewerID) {
this.viewerID = viewerID;
}
/**
* @return the mailProperties
*/
public StiMailProperties getMailProperties() {
mailProperties = new StiMailProperties();
return mailProperties;
}
/**
* @param mailProperties
* the mailProperties to set
*/
public void setMailProperties(StiMailProperties mailProperties) {
this.mailProperties = mailProperties;
}
}
Then, configure the 'faces-config.xml' file and add the necessary beans:
<?xml version="1.0" encoding="UTF-8"?>
<xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_4_0.xsd"
version="4.0">
</faces-config>
In the next step we need to create the 'designer.xhtml' page in the 'WebContent' folder:
<!DOCTYPE html html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:stiwebdesigner="http://stimulsoft.com/webdesigner/jksf">
<head>
</head>
<stiwebdesigner:webdesigner options="#{WebDesignerBean.options}"
handler="#{WebDesignerBean.handler}" designerID="#{WebDesignerBean.designerID}"/>
</html>
And also we need to create the 'viewer.xhtml' page in the 'WebContent' folder:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:stiwebviewer="http://stimulsoft.com/webviewer/jksf">
<head>
</head>
<stiwebviewer:webviewer report="#{WebViewerBean.report}" options="#{WebViewerBean.options}"
mailProperties="#{WebViewerBean.mailProperties}" viewerID="#{WebViewerBean.viewerID}"/>
</html>
Finally you can deploy the project to the Tomcat and run it.
In the screenshot below you can see the result of the sample code: