Grails : agile, industrial strength, rapid web application development made easy!
ZK: the simplest way to make Web applications rich!
Do you want to combine both of them?
1. Install & test the ZK plug-in
3) Download
ZK plugin , make sure you are in the root directory of your project, type
- grails install-plugin full_path_to_zk_plugin
4) create a new file "list.zul" under "your_project\web-app" with the following content
xml 代码
- <?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>
5) test "list.zul", make sure you are in the root directory of your project, type "grails run-app", then browse to "
http://localhost:8080/your_project/list.zul", you should be able to view the following list.
2. How does ZK plugin do it?
Actually, you can find the whole source code of this plugin by browsing to "your_project\plugins\zkplugin-0.1", 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) Participating in web.xml Generation to modify the "url-pattern" of Grails filter "sitemesh"
I find that accessing the "zul" file will just return a blank page with "sitemesh" filter mapped to "/*", so I have to modify the "url-pattern" of Grails filter "sitemesh". It means that you can NOT use Grails controller after you installed the ZK plugin!
I'm still working on it to see if there is a way to let Grails filter "sitemesh" to ignore some particular url-patterns, your comments are welcome!
3. Accessing the domain class
As you can see in the above sample, accessing domain classes in ZK is very simple, just as what you can do in Grails controller.
4. 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 .
1) create a service.
create a new file "HelloService.groovy" under "your_project\grails-app\services" with the following content
groovy 代码
- class HelloService {
- def serviceMethod(username) {
- return username+", welcome to ZK&Grails world!"
- }
- }
2) create a zul file
create a new file "service.zul" under "your_project\web-app" with the following content
xml 代码
- <?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>
3) test it
Browse to "http://localhost:8080/your_project/service.zul", input a name and click the button!
Enjoy it, and any comments is welcome!