Align IT and Business in an Agile Manner

Agile Software Development

Subscribe to Agile Software Development: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Agile Software Development: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Agile Development Authors: Pat Romanski, Elizabeth White, Jason Bloomberg, Charles Araujo, Liz McMillan

Tutorial

Developing MVC Applications Using ZK and Spring

MVC app with ZK and Spring

The ZK framework provides a variety of paradigms to develop Web applications. One the one hand it is always nice to have options, but on the other this can cause un-intentional paradigm disparities when several developers are using the framework to develop an application without a consistent design.

By and far, Model View Controller pattern has been adapted by various  Web applications and frameworks (like Apache Struts, Apache Beehive PageFlows)  to develop quality Web applications.

The following is an illustration of how an MVC based Web application can be developed using ZK and Spring. ZK has introduced a lot of features that enable easy integration with Spring. The source code of example application is available for download here.

The Model:
The model is usually an object or a group of objects representing a business entity. In this example, an object called UserProfile is designed to represent a user. It is defined declaratively per EJB 3 standards.

The View:
The zul page, userProfile.zul is a the page that acts as a view that provides information to the user.

The Controller:
ZK provides a convenience feature use Spring beans with minimal code. All spring beans can be accessed by their names as long as a zul page has the following declaration:

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>

With this declaration, any Spring bean can be accessed by its name. For example, the following zscript scriptlet accesses a Spring bean UserProfileController using its name.

<zscript>
import com.example.userprofile.controller.UserProfileController;
UserProfileController controller = userProfileController;

</zscript>
Note that ‘userProfileController’ is the name of the Spring bean per its declaration
@Controller("userProfileController")
@Scope("prototype")
public class UserProfileController {

}

The annotation ‘Scope’ defines scope and lifecycle of a Spring bean. By default all Spring beans are Singletons. When declared as of type ‘prototype’, Spring generates the bean objects using a factory pattern. So each call made in zscript on page render gets a new object instance, thus making sure that every user gets a new object whose life span is that of the zul page itself. Using Spring MVC framework, further options are available to specify session scoped controllers.

More Stories By Srinivas Jaini

Srinivas Jaini is a software technologist with extensive experience in developing enterprise software applications.