Embedded ZK Application"

From Documentation
m
 
(32 intermediate revisions by 6 users not shown)
Line 2: Line 2:
  
 
=Employment/Purpose=
 
=Employment/Purpose=
Instead of using iframe, there is a new way to use ZK in a non-ZK web container. For example, we could use NodeJs, Python, etc. as the web application, and embed another ZK application in the web pages.
+
Instead of using an iframe, ZK provides JavaScript API for a non-Java EE web container. Hence, you can embed a ZK application in a web application based on NodeJs, Python, etc.
 +
 
 
{{ZK EE}}
 
{{ZK EE}}
[ since 9.0.1 ]
+
{{versionSince| 9.0.1 }}
  
 
=Prerequisite=
 
=Prerequisite=
 
==Settings in the ZK application==
 
==Settings in the ZK application==
 
We use library property to enable the embedded feature.
 
We use library property to enable the embedded feature.
For example: (In zk.xml)
+
For example: (In '''zk.xml''')
 
<source lang="xml">
 
<source lang="xml">
 
<library-property>
 
<library-property>
Line 17: Line 18:
 
</source>
 
</source>
  
=Example=
+
= Demo Example=
There are two web applications, one is a non-ZK web application (http://localhost:8080), and another one is ZK application (http://zkembedded-app).
+
In the  [https://github.com/zkoss-demo/zkembedded-demo Demo project], there are 2 web applications, one is a non-ZK web application (http://localhost:8080), and another one is a ZK application (http://zkembedded-app).
  
We'll embed ZK in the non-ZK web application.
+
We'll embed ZK into the non-ZK web application.
  
 
'''index.html'''
 
'''index.html'''
<syntax lang="html" high="11,13">
+
<syntaxhighlight line lang="html" highlight="11,13">
 
<!DOCTYPE html>
 
<!DOCTYPE html>
 
<html>
 
<html>
Line 40: Line 41:
 
</body>
 
</body>
 
</html>
 
</html>
</syntax>
+
</syntaxhighlight>
  
In line 11, we use <script> to load ZK embedded JS.
+
* line 11: load the zk embedded JS API script
 
+
* line 13: load '''demo.zul''' (of the ZK application) into the DOM Element with id '''embeddedZK'''.
In line 13, we call zEmbedded to load the demo.zul in ZK application, and the "embeddedZK" means that the embedded ZK page would be rendered in the corresponding DOM Element (DOM id).
 
  
 
To see more information, please download the [https://github.com/zkoss-demo/zkembedded-demo Demo project].
 
To see more information, please download the [https://github.com/zkoss-demo/zkembedded-demo Demo project].
Line 50: Line 50:
 
=API in embedded.js=
 
=API in embedded.js=
  
We provide two API methods for embed ZK.
+
We provide two methods to embed ZK.
  
 
==zEmbedded.load(domId, ZKSrc)==
 
==zEmbedded.load(domId, ZKSrc)==
Line 56: Line 56:
 
The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.
 
The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.
  
This function would return a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promise object], which means that we can call functions after ZK is ready.
+
This function returns a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promise object], which means that we can call functions after ZK is ready.
  
<syntax lang="javascript">
+
<syntaxhighlight line lang="javascript">
  zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul').then(function() {
+
zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul')
     zk.log('ZK is ready!');
+
  .then(function(result) {
 +
     zk.log('ZK is ready!' + result.widget.uuid); //result contains the first widget
 
   }).catch(reason => {
 
   }).catch(reason => {
 
     alert('ZK mounting error: ' + reason);
 
     alert('ZK mounting error: ' + reason);
  });
+
});
</syntax>
+
</syntaxhighlight>
 +
 
 +
The "then" method of the promise is invoked when the embedding has completed, and the embedded page has been loaded.
 +
 
 +
The "catch" method of the promise is invoked when the embedding process encounters a failure. The reason object contains the error message associated with the failure.
 +
 
 +
==zEmbedded.load(domId, ZKSrc, ZKHost)==
 +
{{versionSince| 9.6.0 }}
 +
 
 +
To handle the URL redirection, we can specify the ZK Host URL.
 +
<syntaxhighlight line lang="html">
 +
<script id="embeddedScript" src="/embedded/embedded.js"></script>
 +
<script>
 +
zEmbedded.load('embeddedZK', '/embedded/demo.zul', '/embedded');
 +
</script>
 +
</syntaxhighlight>
 +
 
 +
Notice that the URL redirection should rewrite the locations:
 +
 
 +
<syntaxhighlight lang="javascript">
 +
/embedded/embedded.js -> http://zkembedded-app/zkau/web/js/zkmax/embedded/embedded.js
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="javascript">
 +
/embedded -> http://zkembedded-app/embedded
 +
</syntaxhighlight>
  
 
==zEmbedded.destroy(domId, skipError)==
 
==zEmbedded.destroy(domId, skipError)==
  
We can remove the specific ZK desktop in the HTML DOM Element. If "skipError" is true, the function would skip error messages when an error occurs.
+
This will destroy the embedded ZK desktop at server side and clear the DOM Element. Use "skipError = true" to ignore error messages.
  
=Control ZK Components when using embedded ZK=
+
==Better way to include embedded.js in ZK 10==
After loading the ZK contents, we could use the ZK Client binding to control the ZK components in the view.
+
{{versionSince| 10.0.0 }}
  
To see more information, please refer to [http://books.zkoss.org/zk-mvvm-book/8.0/data_binding/client_binding_api.html ZK MVVM Book - Client Binding].
+
Since ZK 10, we can set the zkEmbedded servlet in '''web.xml'''
 +
<syntaxhighlight lang="xml">
 +
<servlet>
 +
    <servlet-name>zkEmbedded</servlet-name>
 +
    <servlet-class>org.zkoss.zkmax.ui.http.EmbeddedServlet</servlet-class>
 +
</servlet>
 +
<servlet-mapping>
 +
    <servlet-name>zkEmbedded</servlet-name>
 +
    <url-pattern>/zkEmbedded</url-pattern>
 +
</servlet-mapping>
 +
</syntaxhighlight>
 +
Then we can include embedded.js in html in the following way
 +
<syntaxhighlight lang="xml">
 +
<script src="/${webappRoot}/zkEmbedded"></script>
 +
</syntaxhighlight>
  
=Cross-Origin Resource Sharing issue=
+
=Control ZK Components when using embedded ZK=
When using Embedded ZK, we need to handle all of the requests, whose target URL point to ZK application.
+
After loading the ZK contents, we could use the ZK Client command binding to control the ZK components on a page.
  
To deal with this problem, we should handle the requests whose target is ZK application.
+
To see more information, please refer to [http://books.zkoss.org/zk-mvvm-book/9.5/data_binding/client_binding_api.html ZK MVVM Book - Client command binding].
  
We could use Nginx or some apache solutions to redirect those requests to the ZK URL, or using java Filter, container config to provide the request header information.
+
=Cross-Origin Resource Sharing=
 +
In Cross Origin scenarios the responses from the ZK application need to set at least the following CORS headers:
  
<syntax lang="javascript">
+
<syntaxhighlight line lang="javascript">
 
Access-Control-Allow-Origin: [allowed embedding origins]
 
Access-Control-Allow-Origin: [allowed embedding origins]
 
Access-Control-Allow-Headers: zk-sid
 
Access-Control-Allow-Headers: zk-sid
Line 88: Line 129:
 
Access-Control-Allow-Credentials: true
 
Access-Control-Allow-Credentials: true
 
Access-Control-Allow-Methods: GET, POST
 
Access-Control-Allow-Methods: GET, POST
</syntax>
+
</syntaxhighlight>
 +
 
 +
This can be done by configuring your server (e.g. nginx, apache-httpd, tomcat, spring-boot...) appropriately and is '''not''' ZK-specific.
 +
Please refer to the related documentation (e.g. [https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS MDN: Cross-Origin Resource Sharing (CORS)]) and your specific server configuration guides.
 +
 
 +
=Limitations=
 +
==Cannot Embed Pages from Different ZK Versions==
 +
Since ZK loads JavaScript and CSS in the global namespace, so multiple versions of these assets will conflict with each other. Hence, you cannot embed a <code>page1.zul</code> from '''ZK 8''' application and a <code>page2.zul</code> from '''ZK 9''' application into one page.
 +
 
 +
==Cannot Embed Multiple Pages with WebSocket Enabled==
 +
{{versionSince| 10.0.0 }}
  
 +
zEmbedded supports WebSocket under the condition that only one ZK page can be embedded into a non-ZK page when WebSocket is enabled.
  
 
=Version History=
 
=Version History=
{{LastUpdated}}
+
 
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-
 
| 9.0.0
 
| 9.0.0
 
|  
 
|  
|  
+
|-
 +
| 10.0.0
 +
| Jan 15, 2024
 +
| zEmbedded supports WebSocket
 
|}
 
|}
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 06:18, 1 February 2024


Embedded ZK Application


Employment/Purpose

Instead of using an iframe, ZK provides JavaScript API for a non-Java EE web container. Hence, you can embed a ZK application in a web application based on NodeJs, Python, etc.

  • Available for ZK:
  • http://www.zkoss.org/product/zkhttp://www.zkoss.org/whyzk/zkeeVersion ee.png

Since 9.0.1

Prerequisite

Settings in the ZK application

We use library property to enable the embedded feature. For example: (In zk.xml)

<library-property>
	<name>org.zkoss.web.servlet.http.embedded.enabled</name>
	<value>true</value>
</library-property>

Demo Example

In the Demo project, there are 2 web applications, one is a non-ZK web application (http://localhost:8080), and another one is a ZK application (http://zkembedded-app).

We'll embed ZK into the non-ZK web application.

index.html

 1 <!DOCTYPE html>
 2 <html>
 3 	<head>
 4 		<meta charset="UTF-8">
 5 		<title>Title</title>
 6 	</head>
 7 	<body>
 8 		<div id="embeddedZK" style="height:80%">  
 9 			 Loading...
10 		</div>
11 		<script id="embeddedScript" src="http://zkembedded-app/zkau/web/js/zkmax/embedded/embedded.js" />
12 		<script>
13 			zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul');
14 		</script>
15 	</body>
16 </html>
  • line 11: load the zk embedded JS API script
  • line 13: load demo.zul (of the ZK application) into the DOM Element with id embeddedZK.

To see more information, please download the Demo project.

API in embedded.js

We provide two methods to embed ZK.

zEmbedded.load(domId, ZKSrc)

The "domId" means after loading resource from "ZKSrc", the content of "domId" (HTML DOM Element) would be replaced with the ZK content.

This function returns a Promise object, which means that we can call functions after ZK is ready.

1 zEmbedded.load('embeddedZK', 'http://zkembedded-app/demo.zul')
2   .then(function(result) {
3     zk.log('ZK is ready!' + result.widget.uuid); //result contains the first widget
4   }).catch(reason => {
5     alert('ZK mounting error: ' + reason);
6 });

The "then" method of the promise is invoked when the embedding has completed, and the embedded page has been loaded.

The "catch" method of the promise is invoked when the embedding process encounters a failure. The reason object contains the error message associated with the failure.

zEmbedded.load(domId, ZKSrc, ZKHost)

Since 9.6.0

To handle the URL redirection, we can specify the ZK Host URL.

1 <script id="embeddedScript" src="/embedded/embedded.js"></script>
2 <script>
3 	zEmbedded.load('embeddedZK', '/embedded/demo.zul', '/embedded');
4 </script>

Notice that the URL redirection should rewrite the locations:

/embedded/embedded.js -> http://zkembedded-app/zkau/web/js/zkmax/embedded/embedded.js
/embedded -> http://zkembedded-app/embedded

zEmbedded.destroy(domId, skipError)

This will destroy the embedded ZK desktop at server side and clear the DOM Element. Use "skipError = true" to ignore error messages.

Better way to include embedded.js in ZK 10

Since 10.0.0

Since ZK 10, we can set the zkEmbedded servlet in web.xml

<servlet>
    <servlet-name>zkEmbedded</servlet-name>
    <servlet-class>org.zkoss.zkmax.ui.http.EmbeddedServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>zkEmbedded</servlet-name>
    <url-pattern>/zkEmbedded</url-pattern>
</servlet-mapping>

Then we can include embedded.js in html in the following way

<script src="/${webappRoot}/zkEmbedded"></script>

Control ZK Components when using embedded ZK

After loading the ZK contents, we could use the ZK Client command binding to control the ZK components on a page.

To see more information, please refer to ZK MVVM Book - Client command binding.

Cross-Origin Resource Sharing

In Cross Origin scenarios the responses from the ZK application need to set at least the following CORS headers:

1 Access-Control-Allow-Origin: [allowed embedding origins]
2 Access-Control-Allow-Headers: zk-sid
3 Access-Control-Expose-Headers: zk-sid, zk-error
4 Access-Control-Allow-Credentials: true
5 Access-Control-Allow-Methods: GET, POST

This can be done by configuring your server (e.g. nginx, apache-httpd, tomcat, spring-boot...) appropriately and is not ZK-specific. Please refer to the related documentation (e.g. MDN: Cross-Origin Resource Sharing (CORS)) and your specific server configuration guides.

Limitations

Cannot Embed Pages from Different ZK Versions

Since ZK loads JavaScript and CSS in the global namespace, so multiple versions of these assets will conflict with each other. Hence, you cannot embed a page1.zul from ZK 8 application and a page2.zul from ZK 9 application into one page.

Cannot Embed Multiple Pages with WebSocket Enabled

Since 10.0.0

zEmbedded supports WebSocket under the condition that only one ZK page can be embedded into a non-ZK page when WebSocket is enabled.

Version History

Version Date Content
9.0.0
10.0.0 Jan 15, 2024 zEmbedded supports WebSocket



Last Update : 2024/02/01

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.