Integrating ZK Framework on a Spring Boot application

The complete project can be found here:

As you already know if you read my previous posts, I kinda decided to shift my focus to Groovy and I’m learning as I go. Feel free to correct me any time on the comments if you feel I’m doing something wrong or not optimal.

In this post I’ll show how to integrate the ZK Framework on a Spring-Boot project just to output a “Hello World” message coming from an autowired bean just so we can check if everything is correctly wired.

As usual, let’s start with the build.gradle file:

Ok, there’s a few differences here from my previous examples.

Firstly, at the moment of this post, ZK’s dependencies on MavenCentral are a bit messed up… zsoup is missing, zcommon’s last version is an older one, etc… So I had to add a custom maven repository to the project:

This repository has all ZK’s dependencies on their final versions. So let’s use it.

Also, I couldn’t find much on the Web about how manage a ZK project with Gradle so I added each dependency one by one to see what they would import and I ended up with the following dependencies.

Now, one of the ZK’s dependencies also pulled slf4j-jdk14 which then conflicted with logback. So I had to add the following exclusion:

Ok, back to the code: the Application.class haven’t changed much from my other posts:

ZKConfiguration.class is where I configured the ZK Loader. It’s located on the same package as the Application.class because, if you noticed, I haven’t added any parameters to @ComponentScan, which means it will only look on the same package as the Application.class. This is pretty enough for this example.

So, these beans initiate ZK Loaders’s servlets so ZK’s calls are correctly handled. Some of the documentation of ZK Loader can be found here. It’s somewhat hard to find documentation on the XML-free approach like above so make sure to take notes ūüôā

Next, I added a very simple bean just so we can call it from the ZK controller class and demonstrate that wiring with Spring is working. Remember that since the class is on the same package as the Application.class, @ComponentScan will create the bean on the startup because it sees the @Component annotation.

Here is the controller for the index.zul:

Firstly, we need to add the @VariableResolver annotation and point it to DelegatingVariableResolver.class which will resolve beans declared in Spring and make them available to the controller.

Now we wire our simpleBean with ZK’s @WireVariable, note that @Autowired won’t work here.

def message is the property I’m going to bind on the index.zul file. Here I take advantage of the fact that Groovy automatically handles the getter and setter for this property which makes my code much more readable, specially when I have lots of bindings.

And for the sake of this example, I added a init() method just to send “Hello World” to our bean that just echoes it back so we know autowiring worked. Notice the @Init annotation that just tells ZK which method will run before rendering the page.

Here is the index.zul:

Pretty simple: viewModel points to the controller class and sets an alias to it called “vm” which then you can use to reference methods on the controller. The <label> tag is binded to the message method.

Hope it helps, later!


You may also like...

Leave a Reply

%d bloggers like this: