From Documentation

Revision as of 07:13, 4 October 2012 by Iantsai (Talk | contribs)
Jump to: navigation, search

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

  • Author
    Ian YT Tsai, Engineer, Potix Corporation
  • Date
    October 05, 2012
  • Version
    ZK 6



Spring Framework is one of the most common framework for Java web developer. In this article, I'll introduce how to integrate ZK, Spring and Hibernate together to build a web application. First, I'll introduce the prerequisite of the demo application. The,n I'll focus on guiding how to use ZK Spring DelegatingVariableResolver properly in your ZK code and some programming tips of the Spring part design.

If you already read Practices Of Using CDI In ZK, this article is the counterpart in Spring.

Develop Environment Preparation

To get the source code, you can download everything from [1], please get the zip file named, and it's better to get the latest one. If your are familiar with Git, you can also clone my smalltalk repository.

IDE Setup

In this article, we use Eclipse with M2Eclipse to manage our Maven Project.

  1. Eclipse 3.x: I think everything should be fine in 4.X but I haven't try it.
  2. M2Eclipse: a Maven management Eclipse plugin.
  3. RunJettyRun: a simple Jetty Server which is very easy to use with M2Eclipse.

Demo Project Setup

If you are a Maven user and already cloned my repository , the project is a Maven project, you can use Eclipse Import Existing Project function to import it. If you get the code by downloading tthe zip file, unpack it, and put them to the project type you preferred.

About The Demo Application

The demo case of this article is the same one in Practices Of Using CDI In ZK, it is an online order management system.text

This demo consists of 4 parts:

  1. Login, when it's the first time the user requests the URL: http://localhost:8080/springZkDemo, the request will be redirected to the login page and ask user to perform the login process.
  2. product view, after login, user will be redirected back to main page which has three fragments, the first one is product view which displays all available products.
  3. shopping cart view, At the east part of main view which displays user's shopping cart content, user can add, remove and change quantity of products in cart.
  4. order view, The bottom of the main page displays user's order and order details of each order.

Entity Beans of This Application

In this demo application we have several entity beans all under package demo.model.bean

  • Product, a product with several attributes like name, price, quantity
  • CartItem, a shopping cart item which has attribute amount and pointed to a product.
  • Order, an order which contains multiple order item.
  • OrderItem, an item that has attributes which comes from cartitem
  • User, a user with attributes user name and password

Persistence Layer

Logic Layer

Logic Layer is a layer for business objects of this demo, in this demo application, all object about this layer has been put in package demo.web.model.

Business Object Design practice

In this demo application, we have several main business objects which provides a set of logical API to interact with view. They are:

  • demo.web.model.ProductManager
  • demo.web.model.UserOrderManager
  • demo.web.model.UserCredentialManager
  • demo.web.model.ShoppingCart

Let's use them to see how to design your business object in some common scenario.

Application Scope Practice

In demo.web.model.ProductManager, as the scenario here is to provide a list of available products for view to display, the code is very simple:

public class ProductManager {
	private ProductDAO productDao;

	public List<Product> findAllAvailable() {
		return productDao.findAllAvailable();

We give a @Named annotation which will tell CDI the bean name of ProductManager, then we set the scope of our ProductManager to be @ApplicationScoped as product manager is generic to every user.

Inconsistent Scope handling

Though our ProductManager is application scoped, the ProductDAO which CDI injected is RequestScoped, this causes a scope inconsistent problem and needs to be solved.

Zk cdi integration cdi proxy.png

As figure shown above, to deal with this inconsistency, during injection, CDI created a proxy object which wrapped some code that will get correct instance of ProductDAO according to request scope .

Session Scope Practice

In web logical layer design, a business object which needs to keep state across multiple requests should be stored in session. In CDI, you can annotate a managed bean with @SessionScoped to make sure requests belongs to one session will access the same instance.

To identify if a managed bean should be session scoped or not, you can check if it contains unmanaged bean member fields. Normally a bean contains member fields which have a strong flavor of current user state could be session scoped, for example in our demo application while user try to request http://localhost:8080/backend_demo/index.zul we need to check if current user is authenticated. if not, well redirect him to login.zul for login process:

Zk cdi integration login proc.png

In order to do this, I designed demo.web.model.UserCredentialManager which is a session object maintaining the state of user credential:

public class UserCredentialManager implements Serializable{
	public UserCredentialManager(){}
	private User user;

	private UserDAO userDao;
	public synchronized void login(String name, String password) {
		User tempUser = userDao.findUserByName(name);
		if (tempUser != null && tempUser.getPassword().equals(password)) {
			user = tempUser;
		} else {
			user = null;

As you can see above, the UserCredentialManager has a login(name, password) method and stored a User bean inside. Here's one thing to be careful, in CDI, a session scoped object needs to be Serializable otherwise CDI will throw exception while parsing bean. To bean's member field, except managed bean(ex: UserDAO) every member field needs to be Serializable or with keyword transient .

You can also inject BO into another BO, for example, in demo.web.model.UserOrderManager we need userCredentialManager to get current user:

public class UserOrderManager implements Serializable{

	private OrderDAO orderDao;
	private UserCredentialManager userCredentialManager;

	public List<Order> findAll() {
		return orderDao.findByUser(userCredentialManager.getUser());

Presentation Layer

Context Injection in ZK

ZK's Listener




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.