Introdutiona
ZK is an event-driven, component-based framework to enable rich user interfaces for web application, with ZK plugin, you can develop your web application by using ZK as the view layer and grails as the service & domain(GROM) layer.
Install & test the ZK plug-in
Install Grails first
Please follow http://grails.codehaus.org/Installation
Create a simple Grails App
Please Follow the Grails Quikc Start (http://grails.codehaus.org/Quick+Start), to build a simple Grails project with the Book domain class
Install ZK plugin
Download it from (grails-zkplugin-0.2.zip), the source code also included. Make sure you are in the root directory of your project, type
grails install-plugin full_path_to_zk_plugin
Test the plug-in
Create a new file "list.zul" under "your_project\web-app" with the following content
<?xml version="1.0" encoding="UTF-8"?> <?page zscriptLanguage="Groovy"?> <window border="normal" title="Groovy Test" id="MainWindow" width="400px"> <zscript> books = Book.findAll() </zscript> <listbox> <listhead> <listheader label="ID"/> <listheader label="Title"/> <listheader label="Author"/> </listhead> <listitem forEach="${books}"> <listcell label="${each.id}"/> <listcell label="${each.title}"/> <listcell label="${each.author}"/> </listitem> </listbox> </window>
then browse to "http://localhost:8080/your_project/list.zul", you should be able to view the following list.

How does ZK plugin do it?
Right now, ZK plugin is made up of three parts:
- maintaining the basic ZK realted java libaries
- Participating in web.xml Generation to add ZK related servlets
- Modifying sitemesh's configuration file, to exclude url patterns of ZK
Accessing the domain class
As you can see in the above sample, Grails domain classes can be accessed directly in ZK, just as what you can do in Grails controller.
Accessing the service
Accessing grails services in ZK is also very easy. Grails will define a bean with name "xxxService" of each service "XxxService", and you can access the Services in zscript directly with the help of DelegatingVariableResolver.
Create a service.
Create a new file "HelloService.groovy" under "your_project\grails-app\services" with the following content
class HelloService {
def serviceMethod(username) {
return username+", welcome to ZK&Grails world!"
}
}
Create a zul file
Create a new file "service.zul" under "your_project\web-app" with the following content
<?xml version="1.0" encoding="UTF-8"?> <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> <?page zscriptLanguage="Groovy"?> <window border="normal" title="Groovy Test" id="MainWindow" width="400px"> <textbox id="username"/> <button label="Call HelloService" onClick="result.value=helloService.serviceMethod(username.value)"/> <separator/> <label id="result"/> </window>
Other things
Thanks to ZK & Grails, now writing a AJAX web-app is more easy!
If there are any questions, just email me by "flyisland AT gmail DOT com"
2008.3.29: I just deleted the sentence "With version 0.2, Grails controllers can work with ZK now!", since it confused people who had interest in this plug-in.
Actually, ZK is not supposed to work with Grails controller together, is just another web framework. You can replace Grails controller by ZK, or use them both at the same time. But it's hard to build communication between them.
Since now you can access Domain & Service class directly in ZK, you do anything you want with ZK just like with Grails controller.


