Access Seasar Components as Variables

From Documentation
DocumentationSmall Talks2007AugustAccess Seasar Components as Variables
Access Seasar Components as Variables

Dennis Chen, Engineer, Potix Corporation
Aug. 8, 2007
Applicable to zk-2.5.0-FL-2007-08-07 and later.
Applicable to Seasar 2.4


Seasar is the most popular open source Dependency-Injection framework in Japan. In this article, we demonstrate a simple way to integrate ZK with Seasar by configuring a VariableResolver, and then we can access components of Seasar as variables in ZK. For example, in this demo, we can access Seasar greetingMgr component directly just like access any ZK variables.

Demo Installation

To run the demo in this article, just download seasar2Demo.war and deploy it to tomcat.

The start link in the demo is http://localhost:8080/seasar2Demo/greetingDemo.zul


You must download and install ZK (check ZK Tutorial ), and you must download and install Seasar as well.(check Seasar Tutorial ). After that, add following configuration.

  • app.dicon (in the root of classes) : declare a component "greetingMgr", we declare "greetingMgr" in default scope "singleton" (by instance attribute).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC
  "-//SEASAR//DTD S2Container 2.3//EN"
  <component name="greetingMgr" class="GreetingMgrImpl" instance="singleton" >

POJO and Seasar Component

There are one POJO and one Seasar component(with one interface) in this Demo :

  • : a POJO for greeting information.
public class Greeting {
String name;
String comment;
Date dateTime;
public String getFormattedDateTime(){
	if(dateTime==null) return "";
	return new SimpleDateFormat("yy/MM/dd HH:mm").format(dateTime);

Greeting is a simple POJO which contains getter/setter of member variable.

  • a interface for insert/delete greeting
public interface GreetingMgr {
public ArrayList findAll();
public void addGreeting(Greeting greeting);
public void removeGreeting(Greeting greeting);

GreetingMgr is a interface for operating greeting data.

  • a implementation of GreetingMgr
public class GreetingMgrImpl implements GreetingMgr{
ArrayList all = new ArrayList();
public ArrayList findAll(){
	ArrayList al = new ArrayList();
	return al;
public void addGreeting(Greeting greeting){
public void removeGreeting(Greeting greeting){

GreetingMgrImpl is the simplest implementation of GreetingMgr, and it does not save data into Database in order to run the demo more easily

ZUL file

In this demo there is only one zul file, which shows you how to add and delete data with the variable "greetingMgr" in one page.

  • greetingDemo.zul
<?variable-resolver class="org.zkoss.zkplus.seasar.DelegatingVariableResolver"?>
  import java.util.ArrayList; 
  List allGreetings = greetingMgr.findAll();
<window title="Who says greeting!" width="640px">
  <listbox id="box" multiple="true" rows="8">
	  <listheader label="Name" width="100px" />
	  <listheader label="Comment" width="300px" />
	  <listheader label="Date" width="100px" />
	<listitem forEach="" value="">
	  <listcell label="" />
	  <listcell label="" />
	  <listcell label="" />
	<caption label="Say It" />
	<textbox id="name" cols="15" />
	<textbox id="comment" cols="40" /><separator/>
	<button label="Add" onClick="add()" />
	<button label="Remove" onClick="remove()" />
  void add(){ 
	//insert a new Greeting into the greetingMgr
	Greeting newGreeting = new Greeting(name.value,comment.value,new java.util.Date()); 
	//insert a new Greeging into the listbox 
	Listitem li = new Listitem(); 
	li.appendChild(new Listcell(; 
	li.appendChild(new Listcell(newGreeting.comment)); 
	li.appendChild(new Listcell(newGreeting.formattedDateTime)); 
	name.value = "";
	comment.value = "";
  void remove(){
	Listitem item = box.getSelectedItem();

1. Add a declaration <?variable-resolver class="org.zkoss.zkplus.seasar.DelegatingVariableResolver"?> at the beginning of the page, and then we can access components of Seasar as variables in ZK.

2. Get all Greeting by calling greetingMgr.findAll();

3. Add a Greeting by calling greetingMgr.addGreeting();

4. Remove a Greeting by calling greetingMgr.removeGreeting();

Live Demo



Dependency-Injection is a popular technology of Java. In this article we show you how to integrate ZK with Seasar, which is the most popular open source Dependency-Injection framework in Japan. We hope you enjoy this new feature and give us feedbacks, so we can do things better.

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