Use Native Namespace instead of XHTML Namespace"

From Documentation
m
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
As described in the [[ZK Developer's Reference/UI Composing/ZUML/Standard Languages/ZHTML Language|ZUML Language]] section and the [[Work with HTML Tags]] section of the '''ZUML with the XUL Component Set''' chapter, ZK creates a ZK component for each XML element specified with the XHTML namespace. In other words, ZK has to maintain their states at the server. Since the number of HTML tags are usually large, the performance will be improved dramatically if you use the Native namespace instead.
+
ZK creates a component (one of the derives of <javadoc>org.zkoss.zhtml.AbstractTag</javadoc>) for each XML element specified with the [[ZUML Reference/ZUML/Languages/XHTML|XHTML component set]]. In other words, ZK will maintain their states on the server. However, if you won't change their states dynamically (i.e., after instantiated), you could use the [[ZUML Reference/ZUML/Namespaces/Native|native namespace]] instead.
  
For example, the following code snippet creates five components (one <tt>table</tt>, <tt>tr</tt>, <tt>textbox</tt> and two <tt>td</tt>).
+
For example, the following code snippet creates five components (one <javadoc>org.zkoss.zhtml.Table</javadoc>, <javadoc>org.zkoss.zhtml.Tr</javadoc>, <javadoc>org.zkoss.zul.Textbox</javadoc> and two <javadoc>org.zkoss.zhtml.Td</javadoc>).
  
<syntax lang="xml" >
+
<syntaxhighlight line lang="xml" >
<h:table xmlns:h="http://www.w3.org/1999/xhtml">
+
<h:table xmlns:h="xhtml">
 
     <h:tr>
 
     <h:tr>
 
         <h:td>Name</h:td>
 
         <h:td>Name</h:td>
Line 14: Line 14:
 
     </h:tr>
 
     </h:tr>
 
</h:table>
 
</h:table>
</syntax>
+
</syntaxhighlight>
  
On the other hand, the following code snippet creates two components (one special component to generate <tt>table</tt>, <tt>tr</tt> and <tt>td</tt> to the client, and one <tt>textbox</tt>).
+
On the other hand, the following code snippet won't create components for any elements specified with the native space (with prefix <code>n:</code>)<ref>In fact, it will still create some components for the rerender purpose, such as <javadoc method="invalidate()">org.zkoss.zk.ui.Component</javadoc>. However, since they shall not be accessed, you could imagine them as not created at all.</ref>.
  
<syntax lang="xml" >
+
<syntaxhighlight line lang="xml" >
 
<n:table xmlns:n="native">
 
<n:table xmlns:n="native">
 
     <n:tr>
 
     <n:tr>
Line 27: Line 27:
 
     </n:tr>
 
     </n:tr>
 
</n:table>
 
</n:table>
</syntax>
+
</syntaxhighlight>
  
Notice that <tt>table</tt>, <tt>tr</tt> and <tt>td</tt> are generated directly to the client, so they don't have no counterpart at the client. Thus, you can not change it dynamically. For example, the following code snippet is incorrect.
+
Notice that <code>table</code>, <code>tr</code> and <code>td</code> are generated directly to the client, so they have no counterpart at the server. You cannot change their states dynamically. For example, the following code snippet is incorrect.
  
<syntax lang="xml" >
+
<syntaxhighlight line lang="xml" >
 
<n:ul id="x" xmlns:n="native"/>
 
<n:ul id="x" xmlns:n="native"/>
 
<button label="add" onClick="new Li().setParent(x)"/>
 
<button label="add" onClick="new Li().setParent(x)"/>
</syntax>
+
</syntaxhighlight>
  
Rather, you have to use the <tt>html</tt> component or the XHTML namespace, if you want to change dynamically.
+
If you have to change them dynamically, you still have to use the [[ZUML Reference/ZUML/Languages/XHTML|XHTML component set]], or you could use <javadoc>org.zkoss.zul.Html</javadoc> alternatively, if the HTML tags won't contain any ZUL component.
 +
 
 +
'''Notice''' that you could create the native components in Java too. For more information, please refer to [[ZK Developer's Reference/UI Patterns/HTML Tags/The native Namespace|the native namespace]] section.
 +
 
 +
<source lang="java">
 +
    HtmlNativeComponent n =
 +
        new HtmlNativeComponent("table", "<tr><td>When:</td><td>", "</td></tr>");
 +
    n.setDynamicProperty("border", "1");
 +
    n.setDynamicProperty("width", "100%");
 +
    n.appendChild(new Datebox());
 +
    parent.appendChild(n);
 +
</source>
 +
 
 +
<blockquote>
 +
----
 +
<references/>
 +
</blockquote>
 +
 
 +
=The Stub-izing of Native Components=
 +
By default, a native component will be ''stub-ized'', i.e., they will be replaced with a stateless component called <javadoc>org.zkoss.zk.ui.StubComponent</javadoc>, such that the memory footprint will be minimized<ref>Non-native components could be stub-ized too by use of <javadoc method="setStubonly(java.lang.String)" type="interface">org.zkoss.zk.ui.Component</javadoc>. For more information, please refer [[ZK Developer's Reference/Performance Tips/Specify Stubonly for Client-only Components|here]].</ref>
 +
 
 +
Though rarely, you could disable the stubing by setting a component attribute called <code>org.zkoss.zk.ui.stub.native</code> (i.e., <javadoc method="STUB_NATIVE">org.zkoss.zk.ui.sys.Attributes</javadoc>). A typical case is that suppose you have a component that has a native descendant, and you'd like to detach it and re-attach later. Then, you have to set this attribute to false, since the server does not maintain the states of stub-ized components (thus, it cannot be restored when attached back).
 +
 
 +
<source lang="xml">
 +
<div>
 +
    <custom-attributes org.zkoss.zk.ui.stub.native="false"/>
 +
    <n:table xmlns:n="native"> <!-- won't be stub-ized -->
 +
...
 +
</source>
 +
 
 +
Once set, descendant components unless it was set explicitly.
 +
 
 +
<blockquote>
 +
----
 +
<references/>
 +
</blockquote>
  
 
=Version History=
 
=Version History=
  
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-
| &nbsp;
+
| 5.0.6
| &nbsp;
+
| January, 2011
| &nbsp;
+
| The attribute called <code>org.zkoss.zk.ui.stub.native</code> was introduced to disable the ''stub-ization''.
 
|}
 
|}
  
 
{{ ZKDevelopersReferencePageFooter}}
 
{{ ZKDevelopersReferencePageFooter}}

Latest revision as of 08:27, 1 February 2024


DocumentationZK Developer's ReferencePerformance TipsUse Native Namespace instead of XHTML Namespace
Use Native Namespace instead of XHTML Namespace


ZK creates a component (one of the derives of AbstractTag) for each XML element specified with the XHTML component set. In other words, ZK will maintain their states on the server. However, if you won't change their states dynamically (i.e., after instantiated), you could use the native namespace instead.

For example, the following code snippet creates five components (one Table, Tr, Textbox and two Td).

1 <h:table xmlns:h="xhtml">
2     <h:tr>
3         <h:td>Name</h:td>
4         <h:td>
5         <textbox/>
6         </h:td>
7     </h:tr>
8 </h:table>

On the other hand, the following code snippet won't create components for any elements specified with the native space (with prefix n:)[1].

1 <n:table xmlns:n="native">
2     <n:tr>
3         <n:td>Name</n:td>
4         <n:td>
5         <textbox/>
6         </n:td>
7     </n:tr>
8 </n:table>

Notice that table, tr and td are generated directly to the client, so they have no counterpart at the server. You cannot change their states dynamically. For example, the following code snippet is incorrect.

1 <n:ul id="x" xmlns:n="native"/>
2 <button label="add" onClick="new Li().setParent(x)"/>

If you have to change them dynamically, you still have to use the XHTML component set, or you could use Html alternatively, if the HTML tags won't contain any ZUL component.

Notice that you could create the native components in Java too. For more information, please refer to the native namespace section.

    HtmlNativeComponent n =
        new HtmlNativeComponent("table", "<tr><td>When:</td><td>", "</td></tr>");
    n.setDynamicProperty("border", "1");
    n.setDynamicProperty("width", "100%");
    n.appendChild(new Datebox());
    parent.appendChild(n);

  1. In fact, it will still create some components for the rerender purpose, such as Component.invalidate(). However, since they shall not be accessed, you could imagine them as not created at all.

The Stub-izing of Native Components

By default, a native component will be stub-ized, i.e., they will be replaced with a stateless component called StubComponent, such that the memory footprint will be minimized[1]

Though rarely, you could disable the stubing by setting a component attribute called org.zkoss.zk.ui.stub.native (i.e., Attributes.STUB_NATIVE). A typical case is that suppose you have a component that has a native descendant, and you'd like to detach it and re-attach later. Then, you have to set this attribute to false, since the server does not maintain the states of stub-ized components (thus, it cannot be restored when attached back).

<div>
    <custom-attributes org.zkoss.zk.ui.stub.native="false"/>
    <n:table xmlns:n="native"> <!-- won't be stub-ized -->
...

Once set, descendant components unless it was set explicitly.


  1. Non-native components could be stub-ized too by use of Component.setStubonly(String). For more information, please refer here.

Version History

Version Date Content
5.0.6 January, 2011 The attribute called org.zkoss.zk.ui.stub.native was introduced to disable the stub-ization.



Last Update : 2024/02/01

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