Saturday, March 29, 2008

ZK Example Application with: Maven, Spring, Hibernate, Acegi

I've been working with ZK for a while now, and like its ease of use and low learning curve. However, I had a pretty hard time using it with some of my other favourite frameworks, specifically: Maven, Spring, and Acegi. Also, I found some of the issues around navigation and the initial setup a bit confusing.

Something that would have helped me a lot would have been an example that includes a full Spring / Acegi setup, packaged with Maven so it works 'out of the box', including a couple of screens that actually does your regular search / crud operations. So, I've decided to extract such an example from a project I've been working on. This is just my tiny contribution to the ZK community.

This example app includes all of the above, as well as using Hibernate for persistence, though that is all wrapped in Spring and accessed via services. It's far from perfect, but I think it's a good start. I would greatly appreciate any feedback or suggestions if anyone uses this.

Download:
Download the project as a ZIP file from here.

Usage:
When you launch, you will be presented with a login screen. The Acegi setup is such that a hard-coded user 'admin' with password 'password' can log in only until an actual user exists in the database. The system has screens for administering users, roles and privileges.

Initial setup:
Firstly, you will need Maven, Java, and a database. The example application uses PostgreSQL. To use a different DB setup, edit the following files:
  1. zkskeleton-persistence/pom.xml (The driver from Maven)
  2. zkskeleton-datasource-context.xml (Connection when deployed)
  3. zkskeleton-domain-test-context.xml (Connection for integration tests)
The example includes integration tests so a DB is required to run test cases. To get it going, create a database, user and schema called 'zkskeleton'. The user's password must also be 'zkskeleton'. In postgresql the following should work:
CREATE USER zkskeleton WITH PASSWORD 'zkskeleton' createdb nosuperuser;
CREATE DATABASE zkskeleton WITH OWNER = zkskeleton;
\c zkskeleton
CREATE SCHEMA zkskeleton AUTHORIZATION zkskeleton
To get it running:
  1. Unpack it somewhere
  2. mvn clean install
  3. Deploy the WAR from zkskeleton-web/target/zkskeleton.war
To set it up in your IDE:
I use idea, but eclipse is pretty similiar
  1. mvn idea:clean idea:idea -DdownloadSources=true
  2. Open the project file
Customizing:
The first thing you'll probably want to change is the name, to do this refactor all the packages from 'zkskeleton' to something appropriate. You will also need to change the project directories and xml file names. Then do a global search and replace from 'zkskeleton' to your application name. You will probably also want to change the DB setup, this can be done as explained above.

Architecture:
Of course there are lots of clever ways to do this, but my architecture looks something like this:
  1. Domain layer depending on nothing, containing the domain entities
  2. Persistence layer depending on Domain, containing Hibernate crud stuff
  3. Service Shared layer depending on Domain (bad thing, but I can live with it for purely web based) containing service interfaces and DTO's
  4. Service depending on Service Shared containing service implementations
  5. UI layer depending on Service Shared containing ZK classes, controllers and extensions
  6. Web layer depending on Service and UI, containing the ZK markup files, webapp setup, and Acegi and service wiring.
Each of the above layers exist in its own Maven project, which is in turn wrapped in one top-level project.

PS: The idea behind the navigation is borrowed from the ZK demo app, I hope they don't mind. Since that source is freely available it's probably fine, just giving credit where it's due though.

14 comments:

Anonymous said...

Thanks for the skeleton. I've used ZK embedded in a box for web admin of the box but not in a maven/spring environment.

Here are some suggestions to make your skeleton run quicker out of the box:

1. add jetty:run goal to the web project so people can type "mvn jetty:run" to start the app and access from the browser:

2. use HSQLDB so no database needs to be setup to get the app running.

If interested, I can post the changes.

Thanks for the good work and contributing!

Unknown said...

Well, I went ahead and zipped up your zkskeleton with the above hsqldb and jetty:run changes included:

http://robert.chou.googlepages.com/zkskeleton_hsqldb.zip

thanks,
rob

Unknown said...

Hi

I've downloaded zkskeleton_hsqldb.zip.
When I execute "mvn jetty:run" I get the following error:
"The plugin 'org.apache.maven.plugins:maven-jetty-plugin' does not exist or no valid version could be found".
I tried to add the jetty plugin
groupId:org.mortbay.jetty, artifactId:maven-jetty-plugin
to pom.xml as it is suggested in several forums but I still get the same error.

I decided to give it a try without Jetty and I built zkskeleton.war using "mvn clean install".
I deployed zkskeleton.war in Apache Tomcat/6.0.16. The application starts with login.zul but when I press the Login button I get the following error:
The requested resource (/j_acegi_security_check) is not available.

Could someone help me to solve this issue?

Many thanks

Tom

Andre van der Schyff said...

Hi Thomas,

To run 'mvn jetty:run', you first have to run 'mvn clean install' from the base directory. You then start jetty using 'mvn jetty:run' from the 'web' directory. I've tested it and it seems to work well. Regarding the login page, I've noticed it does not seem to work in the hsqldb version posted by n4spd, i got the same error. I was trying to fix it but got sidetracked, I will look at it again although it will probably take me a few days.

I also want to change it so it uses Spring Security rather than Acegi.

Andre

Unknown said...

Thanks Andre.
Now I can run the hsqldb version with jetty and see what the application does. I'm new to web development, just trying to figure out how things should be done and a small working CRUD app using standard frameworks and tools is a very good starting point. It would be very nice of you to share the Spring Security version when/if it's done. I think I will try to improve the user interface in the next few days.

Tom

Anonymous said...

can't download the sample program.
anyone can send a copy to me? my email address is joywaycn@yahoo.com.cn

Trifon's Creatures said...

Hi,

very nice post.

I managed to fix the problem in hsqlDB by changing LoginWindow.

public void doLogin(String username, String password) {
Form form = new Form();
form.setDynamicProperty("action", "j_acegi_security_check");


before was:
form.setDynamicProperty("action", "/j_acegi_security_check");

Hope that this work for everyone.
Trifon

Pavel Kaminsky said...

Hi guys, i couldn't get it going with the HSQLDB..

what is the user/pass?
is it 'zkskeleton'?

because once i've modified the "LoginWindow" i couldn't pass the login screen,
and if i don't i get the exception mentioned above.

Unknown said...

Hi
i using sample with postgre..
but i can't login..
did i need insert some value to table in postgre db?
please tell me what should input to login form..i so newbie at this..

Unknown said...

oh i forget this..
when i press the button always appeared this :
HTTP Status 404 -/j_acegi_security_check

type Status report

message /j_acegi_security_check

description The requested resource (/j_acegi_security_check) is not available.
Apache Tomcat/6.0.18

Anonymous said...

See prevision comment about this problem from Trifon Trifonov:

in file LoginWindow.java change next string

form.setDynamicProperty("action", "/j_acegi_security_check");

to

form.setDynamicProperty("action", "j_acegi_security_check");

This work for postgresql.

Peter Shepetko

Rai said...

This is a good example. Thank you for this. I am having trouble logging in and also running the unit tests with Postgres. I even went as far as to create the schema like so:

String[] lines = cfg.generateSchemaCreationScript(new PostgreSQLDialect());

The funny thing is, the tests pass, but there is no data in the underlying tables. The sequence never increments when I check this server side. Anyone have this issue?

Suresh said...

Hi Guys,
I am a c++ developer, I have started learning web development, while surfing I found this blog and I felt this is correct place to start my learning.

I have downloaded zkskeleton_hsqldb.zip.

Can any one help me in how to configure or to set it up these projects in eclipse IDE.

Thanks in advance.

Anonymous said...

Hi I'm new in ZK developpement , please could you explain how to create a maven project in eclipse that support zk spring framework ,thank you.