# Zk support for groovy View models or controllers

RomanZK
1 1

Does ZK support development of web applications using groovy in place of java ? For example can I write code such as:

1. ViewModels in groovy ?
2. Implement controller in groovy ?

Can a zuml reference a view model or controller implemented using groovy ? If yes, How can this be achieved ?

delete retag edit

## 4 Replies

MDuchemin
1891 1 5

Hi Roman

It mostly depends on what you are trying to do. For example, someone posted here about using groovy scripts: https://stackoverflow.com/questions/16059514/how-to-integrate-groovy-with-zk-framework-application

but from your description, you mention using groovy to generate full classes? Since a .groovy file containing a class can compile to .class in the same way that a .java file can, it's mostly a matter of setting up your environment to compile groovy file as well as standard files. I've tested it quickly with a tutorial found on google. Using Maven to compile Java and Groovy (with joint compilation).

The rest is mostly you transcribing Java syntax to groovy syntax. For example, this can be used as a viewModel:

package org.zkoss.groovytest;

import org.zkoss.bind.annotation.Command
import org.zkoss.zk.ui.util.Clients

class GroovyViewModel {

def String value1 = "var1";

@Command
def myCommand() {
Clients.log(value1);
}
}


with this zul:

<zk>
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm')@init('org.zkoss.groovytest.GroovyViewModel')"
validationMessages="@id('vmsgs')">
<textbox value="@bind(vm.value1)" onChange="@command('myCommand')"></textbox>
<textbox value="@bind(vm.value1)"></textbox>
</window>
</zk>


and the resulting class from compilation will be used by the ZK engine. Assuming you put all the right imports, that the command is public, and that the properties have getters and setters.

On the MVC/composer side of thing, it's basically the same. Make sure your groovy code compiles to a valid Java class, with the right ZK content (extend composer, has annotations, etc.)

for example see this composer:

package org.zkoss.groovytest;

import org.zkoss.zk.ui.Component
import org.zkoss.zk.ui.event.SelectEvent
import org.zkoss.zk.ui.select.SelectorComposer
import org.zkoss.zk.ui.select.annotation.Listen
import org.zkoss.zk.ui.select.annotation.Wire
import org.zkoss.zk.ui.util.Clients
import org.zkoss.zul.ListModelList
import org.zkoss.zul.Listbox

class GroovyComposer extends SelectorComposer {

@Wire
Listbox lb;

@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
ListModelList model = new ListModelList()
2.upto(4) {model.add("$it")} lb.setModel(model) } @Listen("onSelect=#lb") def myListener(SelectEvent event) { Clients.log(event.selectedItems.first().getValue()) } }  with this zul: <zk> <div apply="org.zkoss.groovytest.GroovyComposer"> <listbox id="lb"> <template name="model"> <listitem> <listcell> <label value="${each}" />
</listcell>
</listitem>
</template>
</listbox>
</div>
</zk>


RomanZK
1 1

Thanks for this detailed response. When I am ready I will give it a go.

MDuchemin
1891 1 5

I'll just add that while it is possible to code in groovy for this purpose, I don't think this is better than coding in java.

Groovy is script-oriented, which makes it good for a number of things, but wasn't designed with the goal of replacing java for compilation.

Classic groovy use-cases generally involve short single-purpose invoke-and-forget scripting. The syntax is less strict, which is good when you just want to output a list of items to console, but it's not so good when you want to generate full classes while keeping track of the java access modifiers on your getters and setters (for example).

In order to replace a Java file by a groovy file for the purpose of running a ZK application, you will still need to understand the java class structure and syntax. In the end, you need to know what compiled .class content you want to generate (i.e. how to do it in pure java) in order to do it in groovy.

This is compounded by the fact that you will find plenty of examples and documentation for ZK using java, but you will not find the same in groovy.

If you are new to ZK, starting in groovy is just making things more difficult. Personally I would recommend starting in pure Java until you have a good grasp on ZK structure and syntax, then moving to groovy if there is a good reason for it.

RomanZK
1 1

Thanks for this response. I certainly will focus on java first.

[hide preview]