Building Stateless UI"

From Documentation
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 
__TOC__
 
__TOC__
 +
 
= 1 Setting up =
 
= 1 Setting up =
 
To set up stateless components in a ZK 10 application, you need to include the stateless components module and define a Dispatcher Richlet Filter in your `WEB-INF/web.xml` file.
 
To set up stateless components in a ZK 10 application, you need to include the stateless components module and define a Dispatcher Richlet Filter in your `WEB-INF/web.xml` file.
  
 
== Including Required Jar ==
 
== Including Required Jar ==
<pre>
+
<syntaxhighlight>
 
dependencies {
 
dependencies {
 
     implementation "org.zkoss.zk:stateless:${zkVersion}"
 
     implementation "org.zkoss.zk:stateless:${zkVersion}"
 
     ...
 
     ...
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
== Dispatcher Richlet Filter ==
 
== Dispatcher Richlet Filter ==
<pre>
+
<syntaxhighlight>
&lt;filter>
+
<filter>
     &lt;filter-name>DispatcherRichletFilter&lt;/filter-name>
+
     <filter-name>DispatcherRichletFilter</filter-name>
     &lt;filter-class>org.zkoss.stateless.ui.http.DispatcherRichletFilter&lt;/filter-class>
+
     <filter-class>org.zkoss.stateless.ui.http.DispatcherRichletFilter</filter-class>
     &lt;init-param>
+
     <init-param>
         &lt;param-name>basePackages&lt;/param-name>
+
         <param-name>basePackages</param-name>
         &lt;param-value>&lt;!-- your base packages -->&lt;/param-value>
+
         <param-value><!-- your base packages --></param-value>
     &lt;/init-param>
+
     </init-param>
&lt;/filter>
+
</filter>
&lt;filter-mapping>
+
<filter-mapping>
     &lt;filter-name>DispatcherRichletFilter&lt;/filter-name>
+
     <filter-name>DispatcherRichletFilter</filter-name>
     &lt;url-pattern>/*&lt;/url-pattern>
+
     <url-pattern>/*</url-pattern>
&lt;/filter-mapping>
+
</filter-mapping>
</pre>
+
</syntaxhighlight>
  
 
= Constructing UI with IComponent =
 
= Constructing UI with IComponent =
 
Stateless components in ZK 10 are immutable and are created using Java APIs. Here is an example of how to compose a UI with IComponent.
 
Stateless components in ZK 10 are immutable and are created using Java APIs. Here is an example of how to compose a UI with IComponent.
  
<pre>
+
<syntaxhighlight>
 
// Example of composing UI with IComponent
 
// Example of composing UI with IComponent
 
public IComponent demo() {
 
public IComponent demo() {
 
     return IButton.of("add items").withSclass("add-items");
 
     return IButton.of("add items").withSclass("add-items");
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
= Handling Events and Accessing UI State in a Stateless Manner =
 
= Handling Events and Accessing UI State in a Stateless Manner =
 
Events in stateless components are wired using the `@Action` annotation, and the UI state is accessed via the `@ActionVariable`.
 
Events in stateless components are wired using the `@Action` annotation, and the UI state is accessed via the `@ActionVariable`.
  
<pre>
+
<syntaxhighlight>
 
// Wiring an event listener
 
// Wiring an event listener
 
@Action(type = Events.ON_CLICK) // Wiring event
 
@Action(type = Events.ON_CLICK) // Wiring event
Line 53: Line 54:
 
     ...
 
     ...
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
This is a basic outline based on the documents available. For more detailed information and examples, it is recommended to refer directly to the documents: "Diving into Stateless Components" and "ZK 10 is ready for building Cloud-Native Applications."
 
This is a basic outline based on the documents available. For more detailed information and examples, it is recommended to refer directly to the documents: "Diving into Stateless Components" and "ZK 10 is ready for building Cloud-Native Applications."

Revision as of 10:32, 22 November 2023


Building Stateless UI


1 Setting up

To set up stateless components in a ZK 10 application, you need to include the stateless components module and define a Dispatcher Richlet Filter in your `WEB-INF/web.xml` file.

Including Required Jar

dependencies {
    implementation "org.zkoss.zk:stateless:${zkVersion}"
    ...
}

Dispatcher Richlet Filter

<filter>
    <filter-name>DispatcherRichletFilter</filter-name>
    <filter-class>org.zkoss.stateless.ui.http.DispatcherRichletFilter</filter-class>
    <init-param>
        <param-name>basePackages</param-name>
        <param-value><!-- your base packages --></param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>DispatcherRichletFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Constructing UI with IComponent

Stateless components in ZK 10 are immutable and are created using Java APIs. Here is an example of how to compose a UI with IComponent.

// Example of composing UI with IComponent
public IComponent demo() {
    return IButton.of("add items").withSclass("add-items");
}

Handling Events and Accessing UI State in a Stateless Manner

Events in stateless components are wired using the `@Action` annotation, and the UI state is accessed via the `@ActionVariable`.

// Wiring an event listener
@Action(type = Events.ON_CLICK) // Wiring event
public void addItem() {
    ...
}

// Accessing UI state
@Action(type = Events.ON_CLICK)
public void addItem(@ActionVariable(targetId = ActionTarget.SELF, field = "id") String orderId) {
    ...
}

This is a basic outline based on the documents available. For more detailed information and examples, it is recommended to refer directly to the documents: "Diving into Stateless Components" and "ZK 10 is ready for building Cloud-Native Applications."



Last Update : 2023/11/22

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