Grails - ZK Plugin

Introduction

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(GORM) layer.

This project has been moved to http://code.google.com/p/zkgrails/. Please feel free to use the new version.

Last stable is ZK Grails 0.6.1, which supports ZK 3.6.0 and Grails 1.1.

Install & test the ZK plug-in

Check the license

This plug-in is released under the GPL V2 license. If you use it, make sure that this license matches your environment, especially if you decide to use this plug-in inside a closed-source product.

Install Grails first

Please follow http://grails.org/Installation

Create a simple Grails App

Please Follow the Grails Quick 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-zk-0.6.1.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_projectweb-app" with the following content

<?xml version="1.0" encoding="UTF-8"?>
<?page zscriptLanguage="GroovyGrails"?>

<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>

&nbsp;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:

  1. maintaining the basic ZK realted java libaries
  2. Participating in web.xml Generation to add ZK related servlets
  3. 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_projectgrails-appservices" 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_projectweb-app" with the following content

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<?page zscriptLanguage="GroovyGrails"?>

<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>

Test it

Browse to "http://localhost:8080/your_project/service.zul", input a name and click the button!

Other things

Thanks to ZK & Grails, now writing a AJAX web-app is more easy!

This project is originated by "flyisland AT gmail DOT com", and now maintained by Chanwit.

2009.3.11: ZKGrails 0.6.1 is released. This version adds support for Grails 1.1, and it will break Grails 1.0.4.

2008.8.19: ZKGrails 0.5 is released.

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.