Access Seasar Components as Variables"

From Documentation
m (Created page with '{{Template:Smalltalk_Author| |author=Dennis Chen, Engineer, Potix Corporation |date=Aug. 8, 2007 |version=Applicable to zk-2.5.0-FL-2007-08-07 and later. ::Applicable to Seasar …')
 
Line 169: Line 169:
 
=Live Demo=
 
=Live Demo=
  
<gflash width="600" height="400">http://docs.zkoss.org/images/b/bf/Seasar.swf</gflash>
+
<gflash width="600" height="400">Seasar.swf</gflash>
 
 
  
 
=Download=
 
=Download=

Revision as of 01:48, 14 September 2010

DocumentationSmall Talks2007AugustAccess Seasar Components as Variables
Access Seasar Components as Variables

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


Introduction

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


Configuration

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"
  "http://www.seasar.org/dtd/components23.dtd">
<components>
  <component name="greetingMgr" class="GreetingMgrImpl" instance="singleton" >
  </component>
</components>


POJO and Seasar Component

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

  • Greeting.java : 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.


  • GreetingMgr.java: 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.


  • GreetingMgrImpl.java: a implementation of GreetingMgr
public class GreetingMgrImpl implements GreetingMgr{
ArrayList all = new ArrayList();
...
public ArrayList findAll(){
	ArrayList al = new ArrayList();
	al.addAll(all);
	return al;
}
public void addGreeting(Greeting greeting){
	all.add(greeting);
}
public void removeGreeting(Greeting greeting){
	all.remove(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"?>
<zk>
<zscript>
  import java.util.ArrayList; 
  List allGreetings = greetingMgr.findAll();
</zscript>
<window title="Who says greeting!" width="640px">
  <listbox id="box" multiple="true" rows="8">
	<listhead>
	  <listheader label="Name" width="100px" />
	  <listheader label="Comment" width="300px" />
	  <listheader label="Date" width="100px" />
	</listhead>
	<listitem forEach="" value="">
	  <listcell label="" />
	  <listcell label="" />
	  <listcell label="" />
	</listitem>
  </listbox>
  <groupbox>
	<caption label="Say It" />
	Name:
	<textbox id="name" cols="15" />
	Comment:
	<textbox id="comment" cols="40" /><separator/>
	<button label="Add" onClick="add()" />
	<button label="Remove" onClick="remove()" />
  </groupbox>
</window>
<zscript>
  void add(){ 
	//insert a new Greeting into the greetingMgr
	Greeting newGreeting = new Greeting(name.value,comment.value,new java.util.Date()); 
	greetingMgr.addGreeting(newGreeting);
	//insert a new Greeging into the listbox 
	Listitem li = new Listitem(); 
	li.setValue(newGreeting); 
	li.appendChild(new Listcell(newGreeting.name)); 
	li.appendChild(new Listcell(newGreeting.comment)); 
	li.appendChild(new Listcell(newGreeting.formattedDateTime)); 
	box.appendChild(li); 
	name.value = "";
	comment.value = "";
  }
  void remove(){
	Listitem item = box.getSelectedItem();
	if(item!=null){
	  item.detach();
	  greetingMgr.removeGreeting(item.getValue());
	}
  }
</zscript>
</zk>


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

Download

Summary

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.