From Documentation

Jump to: navigation, search




  • Author
    Ian Tsai, Engineer, Potix Corporation
  • Date
    August 10, 2007
  • Version
  1. Apache-Tomcat-5.5.X
  2. ZK Freshly (zk-2.5.0-FL-2007-08-01 and later)
  3. zk-JspTags 0.9.1


Contents

Introduction

ZK-JspTags is a JSP Tag Library trying to support all ZK features from JSP platform. To know how to use this lib, please refer to:ZK Small Talks - Use ZK JSP Tags in Your JSP Pages. In this article, I'll introduce two new features Initiator and Annotation in ZK JSP Tag Library which came from ZK originally. With these two new features, we can implement Annotated Databinding in JSP.


tld uri property

In ZK JspTags 0.9.1, we move tag definition file zuljsp.tld to path META-INF in archive package zuljsp.jar. So we don't need to declare ZK taglib in web.xml anymore. Instead of declaring it in web.xml, we must set uri property as http://www.zkoss.org/jsp/zul in page taglib's declaration.


Declaration of Initiator

To declare an initiator while page loading in JSP, we use the following code:


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="z" %>
...
<z:init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit" />
<z:init use="org.zkoss.jspdemo.MyInit" arg0="This is an initiator."/>	
...


Compare with the ZUML declaration:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="....." ?>	
...


There are only two differences between ZUML tag and ZK JSP tag.

1. ZUML tag follows XML format and ZK JspTags follows JSP Spec.

2. Initiator class attribute name is different. In ZUML we use class. In ZK JspTags we use use.

(All because JSP TLD JAVA Bean getClass() method is constrained with Object::getClass().)


Declaration of Annotation

Same as ZUML's annotation declaration, ZK JSP Tag Library define an Annotation in this way:


<z:listbox id="feedListBox" model="@{my_list}" rows="8" selectedItem="@{selected}">
    <z:listitem self="@{each='my'}">
...

Demo of Databinding

Please download jspdatabind.war, and we'll use this demo to see how Initiator and Annotation work.

Listbox1.png

This demo is very simple. We use Initiator to prepare a list of data(org.zkoss.jspdemo.MyValue) and bind it with a Listbox. The code clip of Annotated Databinding is shown below:

...
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="z" %>
<z:init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit"/><%-- Declare AnnotateDataBinder...--%>
<z:init use="org.zkoss.jspdemo.MyInit"/><%-- Prepare test data list "my_list"...--%>

...

<z:page>

 ...<%-- same as original ZK databinding...--%>
	<z:listbox id="feedListBox" model="@{my_list}" rows="8" selectedItem="@{selected}">
		<z:listitem self="@{each='my'}">
			<z:listcell label="@{my.value}"/>
			<z:listcell label="@{my.date}"/>
		</z:listitem>
	</z:listbox>
...
</z:page>
...


Data Transfering between JSP and ZK

Although in the sample code above, using ZK Initiator to prepare data list is relatively simple, using other JSP tags in JSP to prepare data is more common. Here we'll show how to transfer data between JSP and ZK.

<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="org.zkoss.jspdemo.MyValue" %>
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="z" %>
<z:init use="org.zkoss.zkplus.databind.AnnotateDataBinderInit"/>
...
<% 
// JSP scriplet to generate test data "my_list", can be easily wrapped to normal JSP tags...
final ArrayList my_list = new ArrayList();
final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
for(int i=0;i<10;i++)
{
	final String ref = "index: "+i;
	my_list.add(new MyValue(){
		public String getDate() {
			return format.format(new Date());
		}
		public String getValue() {
			return ref;
		}
	});
}
request.setAttribute("my_list",my_list);// use Request to store and pass data.

%>
...
<z:zscript>
	System.out.println("fire when load page..." );
	my_list = requestScope.get("my_list");// get data from request and declare page variable "my_list" implicitly.
</z:zscript>
...
<%-- same as original ZK databinding...--%>
<z:listbox id="feedListBox" model="@{my_list}" rows="8" selectedItem="@{selected}">
    <z:listitem self="@{each='my'}">
	<z:listcell label="@{my.value}"/>
	<z:listcell label="@{my.date}"/>
    </z:listitem>
</z:listbox>
...


Download




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.
You got stuck here?
Let us know why!
For questions please use the forum