论坛首页 Java企业应用论坛

ZK plugin for Grails

浏览 4216 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-31  

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
1) Install Grails first (http://grails.codehaus.org/Installation)
2) Follow the Grails Quikc Start (http://grails.codehaus.org/Quick+Start), to build a simple Grails project with the Book domain class
3) Download ZK plugin , make sure you are in the root directory of your project, type
  1. 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 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <?page zscriptLanguage="Groovy"?>  
  3.   
  4. <window  border="normal" title="Groovy Test" id="MainWindow"  width="400px">  
  5. <zscript>  
  6.     books = Book.findAll()  
  7. </zscript>  
  8. <listbox>  
  9.     <listhead>  
  10.         <listheader label="ID"/>  
  11.         <listheader label="Title"/>  
  12.         <listheader label="Author"/>  
  13.     </listhead>  
  14.     <listitem forEach="${books}">  
  15.         <listcell label="${each.id}"/>  
  16.         <listcell label="${each.title}"/>  
  17.         <listcell label="${each.author}"/>  
  18.     </listitem>  
  19. </listbox>  
  20. </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 代码
  1. class HelloService {
  2.     def serviceMethod(username) {
  3.          return username+", welcome to ZK&Grails world!"
  4.     }
  5. }

2) create a zul file
create a new file "service.zul" under "your_project\web-app" with the following content
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>  
  3. <?page zscriptLanguage="Groovy"?>  
  4.   
  5. <window  border="normal" title="Groovy Test" id="MainWindow"  width="400px">  
  6.     <textbox id="username"/>  
  7.     <button label="Call HelloService"   
  8.         onClick="result.value=helloService.serviceMethod(username.value)"/>  
  9.     <separator/>  
  10.     <label id="result"/>  
  11. </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!
   发表时间:2007-10-31  
nice work,you should join this plugin into grails plugins
0 请登录后投票
   发表时间:2007-10-31  
Good job!
If you could resolve 'you can NOT use Grails controller after you installed the ZK plugin!' problem, that would be great!

Cheers
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics