Difference between revisions of "Template:Tutorial common chapters"
Line 12: | Line 12: | ||
= Start from Example Project = | = Start from Example Project = | ||
− | You can [https://github.com/zkoss-demo/gettingStarted/ | + | You can get [https://github.com/zkoss-demo/gettingStarted/ the source code of this article] and import it to your IDE without starting from scratch. Please follow the README to run the project. |
= Start a New Project = | = Start a New Project = |
Revision as of 07:49, 5 January 2022
Tutorial Objective
Our target application is a simple car catalog application. This application has two functions:
- Search cars.
- Enter a keyword in the input field, click Search and search results will be displayed in the car list below.
- View details.
- Click an item from the car list, the area below the car list will show the selected car's details including model, price, description, and preview.
Start from Example Project
You can get the source code of this article and import it to your IDE without starting from scratch. Please follow the README to run the project.
Start a New Project
Creating a new project with Maven command is quick and doesn't need any IDE. We assume readers have basic understandings for maven, so we won't cover maven basic here. If you are unfamiliar with maven, please take some time to read tutorials, like Maven Tutorial or Maven in 5 Minutes before commencing.
For those readers who don't use maven, please refer to ZK Installation Guide/Quick Start. If you want to know how to build the application with ZK first, please skip this section and start from Declaring Domain Class.
The archetype is a maven project template tool. It can help you quickly create a project with predefined configurations and dependencies. You can use the command below to generate a project with ZK provided archetypes:
mvn archetype:generate -DarchetypeCatalog=http://mavensync.zkoss.org/maven2/
Then follow the instructions described in ZK Installation Guide/Quick Start/Create and Run Your First ZK Application with Eclipse and Maven#Use the command line version of Maven to create a project to complete the creation and run the application.
Both eclipse(since 4.5) and IntelliJ IDEA support maven integrated features.
Declaring Domain Class
The following is the domain object that represents a car.
public class Car {
private Integer id;
private String model;
private String make;
private String preview;
private String description;
private Integer price;
//omit getter and setter for brevity
}
We then define a service class to perform the business logic (search cars) shown below:
public interface CarService {
/**
* Retrieve all cars in the catalog.
* @return all cars
*/
public List<Car> findAll();
/**
* search cars according to keyword in model and make.
* @param keyword for search
* @return list of car that match the keyword
*/
public List<Car> search(String keyword);
}
In this example, we have defined a class - CarServeImpl that implements the above interface. For simplicity, it uses a static list object as the data model. You can rewrite it so that it connects to a database in a real application. Implementation details are not in the scope of this article, please refer to source code repository.
Building User Interface
UI is a good start to building an application as it helps you define the scope of your application. ZK provides hundreds of readily made UI components, so developers can rapidly build their desired user interface by combining and mix-matching these components without having to create a page from scratch.
In ZK, you can use ZK User Interface Markup Language (ZUML), an XML-formatted language, to describe UI. By ZK's convention, the files to describe the user interface with ZUML uses .zul as the name suffix. In zul files, one component is represented as an XML element (tag) and you can configure each component's style, behavior, and function by setting XML element's attributes. (check ZK Component Reference for details)
In the case of this example application, first of all, we want to design a window with the specified title and normal border as our application's frame.
Extracted from search.zul
<window title="Search" width="600px" border="normal">
<!-- put child components inside a tag's body -->
</window>
As window is the outermost component, it is called the root component. Window is a commonly used container because it is a basic display element of a desktop-like application while it can also enclose other components. All other components inside window are called its child components and should be put in window tag's body. We set window's title bar text with "title" attribute and make window display a normal border with "border" attribute. For "width" attribute, use CSS like syntax such as "800px" or "60%".
Basically, our example application's user interface is divided into 3 areas within the window (from top to bottom):
- search function
- car list
- car details.
Search Area
ZK components are like building blocks, you can combine and mix-match existing components to construct your desired UI. To allow users to search, we need a text to prompt users for input, a place to enter keywords, and a button for triggering the search. We can use the following ZK components to fulfill this requirement:
Extracted from search.zul
1 <div>
2 Keyword:
3 <textbox id="keywordBox" />
4 <button id="searchButton" label="Search" image="/img/search.png" />
5 </div>
- Line 3~4: Specifying the
id
attribute for some components allows you to control them by referencing their id. - Line 4: You can easily create an image button by specifying the path for the
image
attribute.
Car List Area
ZK provides several components to display a collection of data such as listbox, grid, and tree. In this example, we have chosen to use a listbox to display a list of cars with 3 columns: Model, Make, and Price. We set the "height" attribute, so the number of rows visible is limited with respect to the height specified; you can drag scroll-bar to see the rest of rows. The "emptyMessage" attribute is used to show a message when listbox contains no items. The listbox is a container component, and you can add listhead to define a column. The listitem is used to display data, and the number of listcell in one listitem should equal to the number of listheader. Here we use listcell with static label to demonstrate structure of a listitem, and we'll talk about how to create listitem dynamically with respect to each data object in the next chapter.
Extracted from search.zul
<listbox id="carListbox" height="160px" emptyMessage="No car found in the result">
<listhead>
<listheader label="Model" />
<listheader label="Make" />
<listheader label="Price" width="20%"/>
</listhead>
<listitem>
<listcell label="car model"></listcell>
<listcell label="make"></listcell>
<listcell>$<label value="price" /></listcell>
</listitem>
</listbox>
Car Details Area
Like the hbox, hlayout and vlayout are also layout components which arrange their child components in horizontal and vertical order. But they are more light-weighted than <hbox/>/ <vbox/> since they render <div/> instead of ><table/>. If you don't have alignment need, using <hlayout/>/ <vlayout/> can reduce a page's complexity and your browser's rendering cost.
By combing these 2 layout components, we can display more information on 2 different divisions. The style attribute allows you to customize component's style with CSS syntax.
Extracted from search.zul
<hlayout style="margin-top:20px" width="100%">
<image id="previewImage" width="250px" />
<vlayout hflex="1">
<label id="modelLabel" />
<label id="makeLabel" />
<label id="priceLabel" />
<label id="descriptionLabel" />
</vlayout>
</hlayout>
You can see the complete zul file through the link in the source repository.