Creating a Simple Sightseeing Application"
m (→Preparing) |
|||
Line 44: | Line 44: | ||
</source> | </source> | ||
− | =A Straightforward Way | + | =A Straightforward Way = |
− | This section will introduce implementation | + | This section will introduce the first implementation. We call it the straightforward way since the application will manipulate the data model directly in Java. It is a common practice found in most frameworks. In the next section will introduce another implementation that use ZK data binding to manipulate the data model automatically. |
==Design User Interface Page Using ZK Markup Language== | ==Design User Interface Page Using ZK Markup Language== | ||
− | We first develop the application's view page file: resort.zul | + | We first develop the application's view page file: <code>resort.zul</code>. It includes three components: listbox, gmaps (Google Maps), and groupbox (detailed pane). |
− | + | To have better separation of UI and business logic, there is no Java code or scriplets in <code>resort.zul</code>. All business logic are defined in a separate Java file — <code>ResortController.java</code>. | |
− | The interaction between the zul file and the Java file is triggered by a user's activities. When a user selects an item in the | + | The interaction between the zul file and the Java file is triggered by a user's activities. When a user selects an item in the list box, an onSelect event will be fired, and the refreshUI() method of ResortController.java will be invoked to display the selected item on the Google Maps, and to show the coordinates of selected item. |
<source lang="xml"> | <source lang="xml"> |
Revision as of 04:17, 30 September 2010
Before You Start
Translations are available in Français and Español.
Other tutorials
- Tutorial - A brief tutorial guides you through the most fundamental features and concepts of ZK.
- Creating Hello World with Eclipse and ZK Studio - Step by step to create a Web application from scratch with Eclipse and ZK Studio.
- Creating a database driven application - Step by step to create a database-driven application.
Introduction
This tutorial aims to use a simple example to demonstrate how to implement a ZK application. You could try the application on line first before keep reading.
Our sample application is a sightseeing web site that uses Google Maps. UI has three parts:
- A list box: show every resort spots and allow the user to pick up for detailed information
- Google Maps: display the location of the selected resort spot.
- Detail pane: show detailed information of the selected resort spot.
We'll show two ways to build an Ajax-enabled Web application using ZK: a straightforward way that manipulates data beans manually, and an automatic way by use of ZK data binding.
Preparing
We can create a simple data model with Java class first because we will use this model latter. The data model consists of the following attributes: name, description, latitude, and longitude.
public class Resort {
private String _name;
private String _description;
private Double _latitude;
private Double _longitude;
public Resort(){};
public Resort(String name, String description, Double latitude, Double longitude) {
_name = name;
_description = description;
_latitude = latitude;
_longitude = longitude;
}
// getter and setter methods are omitted.
}
A Straightforward Way
This section will introduce the first implementation. We call it the straightforward way since the application will manipulate the data model directly in Java. It is a common practice found in most frameworks. In the next section will introduce another implementation that use ZK data binding to manipulate the data model automatically.
Design User Interface Page Using ZK Markup Language
We first develop the application's view page file: resort.zul
. It includes three components: listbox, gmaps (Google Maps), and groupbox (detailed pane).
To have better separation of UI and business logic, there is no Java code or scriplets in resort.zul
. All business logic are defined in a separate Java file — ResortController.java
.
The interaction between the zul file and the Java file is triggered by a user's activities. When a user selects an item in the list box, an onSelect event will be fired, and the refreshUI() method of ResortController.java will be invoked to display the selected item on the Google Maps, and to show the coordinates of selected item.
<!-- resort.zul -->
<window id="win" use="org.zkforge.resort.ui.ResortController" title="ZK Tutorial" width="1024px" border="normal">
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAACZ90QS5pNiEvbXhI0k2NLRTGXUEQDrMr1bP0DVG
8X36ZVCheaRQMK05Rsdneh4skNF6KXaLI8maLlA" type="text/javascript"/>
<hbox>
<listbox id="lb" onSelect="win.refreshUI()">
<listhead sizable="true">
<listheader label="Name" width="100px"/>
<listheader label="Description" width="250px"/>
</listhead>
<listitem forEach="${win.resorts}" value="${each}">
<listcell label="${each.name}" />
<listcell label="${each.description}" />
</listitem>
</listbox>
<gmaps id="gmap" zoom="16" showTypeCtrl="true" mapType="satellite" showLargeCtrl="true" width="610px"
height="400px">
<ginfo id="ginfo"/>
</gmaps>
</hbox>
<groupbox mold="3d" width="100%">
<caption label="Resort"/>
Name: <textbox id="name" disabled="true"/>
Description:<textbox id="desc" disabled="true"/>
Latitude: <doublebox id="lat" disabled="true"/>
Longitude: <doublebox id="lng" disabled="true"/>
</groupbox>
</window>
Manipulate UI Components Using Java
In the Java file(ResortController.java), we retrieve the UI components by their id, and put the data of the selected item into them. Then, ZK engines will update the web page in Ajax automatically.
<!--ResortController.java -->
public class ResortController extends Window {
List _resorts = new ArrayList();
public ResortController(){
_resorts.add(new Resort("La Tour Eiffel",
"Insolite ! Découvrez la Tour Eiffel autrement.",48.8585599391,2.2945332527));
_resorts.add(new Resort("Buckingham Palace",
"The official London residence of the sovereign.",51.5013698182,-0.1416689157));
_resorts.add(new Resort("東京タワ",
"総數4萬の光ファンタジーと都內最大級15mのツリー!",35.6583487578,139.7457217178));
_resorts.add(new Resort("Der Kölner Dom",
"Offizieller Name Hohe Domkirche St. Peter und Maria",50.9414206771,6.9586372375));
}
public void refreshUI(){
Listbox lb = (Listbox) getFellow("lb");
Resort resort = (Resort) lb.getSelectedItem().getValue();
((Textbox)getFellow("name")).setValue(resort.getName());
((Textbox)getFellow("desc")).setValue(resort.getName());
((Doublebox)getFellow("lat")).setValue(resort.getLatitude());
((Doublebox)getFellow("lng")).setValue(resort.getLongitude());
Gmaps gmap = (Gmaps) getFellow("gmap");
Ginfo ginfo = (Ginfo) getFellow("ginfo");
ginfo.setLat(resort.getLatitude());
ginfo.setLng(resort.getLongitude());
ginfo.setContent(resort.getDescription());
gmap.panTo(resort.getLatitude(),resort.getLongitude());
gmap.setZoom(16);
gmap.openInfo(ginfo);
}
public List getResorts() {
return _resorts;
}
public void setResorts(List resorts) {
_resorts = resorts;
}
}
This example illustrates the simplicity of building an Ajax-enabled web application with ZK. The "View" and "Controller" are cleanly separated, and no JavaScript code is required! We believe that the best way to use Ajax is to not have to know of its existence.
An Automatic Way (With Data Binding)
To simplify the job of maintaining the consistency of data between the data bean and the user interface, ZK provides the mechanism of data-binding. You simply define the relations between the "Model" (data beans) and the "View" (UI components), and ZK's data-binding feature will maintain the data consistency between them automatically.
Define Relations between Data Bean and UI Components
One simple way is to directly define such relations in the web page. One of the major differences is that we add another data bean which represents the selected item, and then bind all of related ZK components to it.
<!-- resort_databind.zul -->
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<window id="win" use="org.zkforge.resort.ui.ResortController2" title="ZK Tutorial" width="1024px" border="normal">
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAACZ90QS5pNiEvbXhI0k2NLRTGXUEQDrMr1bP0DVG
8X36ZVCheaRQMK05Rsdneh4skNF6KXaLI8maLlA" type="text/javascript"/>
<hbox>
<listbox model="@{win.resorts}" selectedItem="@{win.selected}" onSelect="win.refreshUI()">
<listhead sizable="true">
<listheader label="Name" width="100px"/>
<listheader label="Description" width="250px"/>
</listhead>
<listitem self="@{each=resort}">
<listcell label="@{resort.name}" />
<listcell label="@{resort.description}" />
</listitem>
</listbox>
<gmaps id="gmap" zoom="16" showTypeCtrl="true" mapType="satellite" showLargeCtrl="true" width="610px"
height="400px">
<ginfo id="ginfo" lng="@{win.selected.longitude}" lat="@{win.selected.latitude}"
content="@{win.selected.description}"/>
</gmaps>
</hbox>
<groupbox id="gb" mold="3d" width="100%">
<caption label="Resort"/>
Name:<textbox id="name" value="@{win.selected.name}" disabled="true"/>
Description:<textbox id="desc" value="@{win.selected.description}" disabled="true"/>
Latitude:<doublebox id="lat" value="@{win.selected.latitude}" disabled="true"/>
Longitude:<doublebox id="lng" value="@{win.selected.longitude}" disabled="true"/>
</groupbox>
</window>
Revise the Java File
With the help of data-binding, the handling codes in ResortController.java are much reduced. Once the user selects a different item, the data bean will be changed, and then data of all related ZK components will be updated automatically.
<!--ResortController2.java -->
public class ResortController extends Window {
List _resorts = new ArrayList();
Resort _selected = new Resort();
public ResortController(){
_resorts.add(new Resort("La Tour Eiffel",
"Insolite ! Découvrez la Tour Eiffel autrement.",48.8585599391,2.2945332527));
_resorts.add(new Resort("Buckingham Palace",
"The official London residence of the sovereign.",51.5013698182,-0.1416689157));
_resorts.add(new Resort("東京タワ",
"総數4萬の光ファンタジーと都內最大級15mのツリー!",35.6583487578,139.7457217178));
_resorts.add(new Resort("Der Kölner Dom",
"Offizieller Name Hohe Domkirche St. Peter und Maria",50.9414206771,6.9586372375));
}
public void refreshUI(){
Gmaps gmap = (Gmaps) getFellow("gmap");
Ginfo ginfo = (Ginfo) getFellow("ginfo");
gmap.panTo(ginfo.getLat(),ginfo.getLng());
gmap.setZoom(16);
gmap.openInfo(ginfo);
}
public List getResorts() {
return _resorts;
}
public void setResorts(List resorts) {
_resorts = resorts;
}
public Resort getSelected() {
return _selected;
}
public void setSelected(Resort selected) {
_selected = selected;
}
}
Installing the Web Application
- Install Java SDK
- If you haven't already done so, please download and install Java SDK.
- Install a Servlet Container
- If you don't already have a Servlet container, please download and install Apache Tomcat.
- Download the Web Application
- Download this application's files here.
- Unzip the file
- Unzip resort.zip, and copy resort.war to $TOMCAT_HOME/webapps/.
- Start your Web Server, and point your browser to: http://localhost:8080/resort/resort.zul
Last Update : 2010/09/30