Using ZK With Spring Boot
Let's integrate ZK with Spring Boot!
Join the DZone community and get the full member experience.Join For Free
In recent years, Spring Boot has become a more and more popular alternative to building and deploying web applications. Following that popularity, questions arose whether existing or new ZK applications can also integrate with Spring Boot instead of building a classical war file.
The answer has always been "yes." At the same time, the transition from web.xml configuration into Java configuration has proven to be inconvenient, error-prone, and full of questions. That's why we at ZK decided to provide the zkspringboot integration module containing default and adjustable autoconfig and a starter dependency.
This was done with the help and inspiration of our active community. Special thanks go to Dirk and his zk-spring-boot-starter project.
In this article, I assume you are mostly familiar with Spring and Spring Boot and/or ZK.
For the impatient, here is the command sequence to clone and run one of our zkspringboot-demos.
git clone --branch v1.0.4 https://github.com/zkoss/zkspringboot.git cd zkspringboot/zkspringboot-demos/zkspringboot-minimal-jar ../mvnw spring-boot:run
Then, open http://localhost:8080/ in a browser.
Understanding the Project
Let's start with a look in to the project structure of zkspringboot-minimal-jar:
Yes, that's all; only 5 files (I'll be ignoring build.gradle for this article — just assume it works similarly). In the following sections, I'll explain the purpose of all these and how to run the project.
The pom file contains a single dependency,
zkspringboot-starter, referencing the default dependencies needed to get started easily (hence the name suffix "-starter"), which include
zkspringboot-autoconfig , minimal ZK dependencies (
zkbind + dependencies), and a recent Spring Boot version 2.1.0 (
spring-boot-starter-web + dependencies).
In real life projects, you should pick your own project dependencies with newer ZK and Spring Boot versions by replacing the
zkspringboot-starter dependency with
zkspringboot-autoconfig alongside your hand-selected dependencies/versions. In contrast to the zkspringboot-starter, the autoconfig doesn't add any transitive dependencies and only contains the ZK-Spring Boot configuration classes.
The spring-boot-maven-plugin at the end of pom.xml file is a standard Spring Boot plugin to run and repackage the project into a runnable jar file during the build process (nothing ZK specific here).
A minimalistic spring configuration class startup class annotated with @SpringBootApplication.
The main method specifies the entry point for the application, which will come in handy when running the application inside an IDE.
You might ask, "How does the Spring Boot application know about ZK?"
The answer lies in the file spring.factories inside zkspringboot-autoconfig.jar dependency. By adding this jar to the classpath, Spring will discover this file (by convention) and initialize ZK automatically.
A simple zul file just to check ZK is working. Since we chose jar packaging (default), there is no classic web-content-folder. No problem for ZK, it can load and display zul files also from the classpath directly. These so-called class-web-resources (in ZK terms) are located below the web package (which can also contain style, script, image...resources).
I chose to place zul files into the zul folder (you can choose your own package folder(s) below web, as needed). One might think that's a new feature, but in fact, ZK uses this mechanism to provide internal resources from the various zk-jar files — it just wasn't necessary in a classical web application since you had the web-content folder, which is not available in a Spring Boot jar application.
That alone would be sufficient to run/deploy ZK on Spring Boot.
Using the maven plugin, we can start the application easily with a single command and get the familiar output.
You can now access the zul file directly using ZK's ajax and resource servlet mapped to /zkau (this also works in your existing classic ZK application, provided the zul files requested are on the classpath inside the web package folder).
Starting from here, you may decide to go "freestyle" — using your existing Spring MVC skills — and configure @Controller classes with @RequestMappings internal forwards and view resolvers. Or, you can read on to check what else is already provided.
Obviously the URL above isn't nice, which we can change using the
zk.homepage property inside application.properties (ideal for applications with a single entry point SPAs).
That's all you need to internally map from your application root —"http://localhost:8080/" — to the home page — "http://localhost:8080/zkau/web/zul/minimal.zul".
For the interested folks, here's what happens internally:
The optional configuration class ZkHomepageConfiguration.java simply maps the request path "/" to the configured view name "zul/minimal".
The provided zul-view-resolver (enabled by default) then prepends the update-uri "/zkau" and the class-web-resource path "/web". Finally adds the suffix ".zul" to locate the resource. All this conveniently happens in ZkWebMvcConfiguration.java (part of zkspringboot-autoconfig), and if you need something else, you can just disable it (
zk.zul-view-resolver-enabled=false ) and implement your own ViewResolver.
Other ZK Config Files
Due to the lack of a WEB-INF folder, additional zk configuration files (such as zk.xml, lang-addon.xml, zk-label.properties) are placed into "src/main/resources/application/metainfo(/zk)".
Again, this is nothing new and just relies on the built-in mechanism traditionally used by ZK's own and addon jars.
For maximum flexibility building/running, the Spring Boot jar application doesn't require an IDE and works from command line:
Naturally, IDEs supporting maven or gradle are able to import and run the application directly (no additional plugins needed, not even a Spring(-Boot)-Plugin). Often, it suffices to right click the main class MinimalApplication.java and choose something like "run/debug as ... Java Application".
Here a typical run configuration (just like any HelloWorld application):
Just by specifying a "Main class," Spring Boot will be able to do its job and start the embedded Tomcat server and application context including ZK's and your own application configuration.
While the trivial minimal example has reasonably short startup times, those won't remain constant as your project gets larger and more complex. That's why it's vital to be able to change and reload resources without having to restart the application for each tiny change. Again, this might have never affected you from the ZK side as long as you had your zul and resource files in the web-content-folder. Now your resources are on the classpath, which behaves slightly different. By default, various resource caches prevent dynamic reloading of zul pages and other resources. Luckily, those can be disabled during development either inside a zk.xml or via Spring java config.
As a template, you can use the DevelopmentConfig.java (part of zkspringboot-demo-jar), which disables all kinds of caches. The configuration is only activated via the Spring configuration profile "dev." You can activate it in your run config using the VM option
-Dspring.profiles.active=dev (there are multiple ways, please refer to the Spring documentation on profiles).
This development configuration allows changing .zul files and other resources without having to restart the server when running in debug mode.
There's a tiny catch: You might be used to simply saving a file and then reload the browser. Since files are on the classpath, they need to be "built" by the IDE in order to be reloaded in the running application. Being just resource files building involves copying them into the runtime classpath folder. In IntelliJ, this means you have to "recompile" the file (CTRL-SHIFT-F9 on Windows), or in Eclipse, enable "auto-build" and save the file. (Maybe there's also a general function I missed in IntelliJ to auto-rebuild resources on auto-save).
Once that's done, refresh the browser once (make sure it doesn't cache as well, you might have to hard reload (CTRL-F5) to get rid of long term cached resources once).
If someone knows a pure command-line + any-texteditor variant of this, let me know in the comments.
Spring Boot and ZK are both nontrivial frameworks, each having their own conventions and built-in mechanisms. Integration is possible but not always without friction and requires insight into both frameworks.
I hope the "ZK - Spring Boot" module helps during this process and that the explanations above clarify and demystify common questions and misunderstandings. If I missed something critical, please let me know in the comments.
Opinions expressed by DZone contributors are their own.