Friday, 1 February 2013

ZK Passing Parameter between two files using MVC - Part 2.

Overview

This is the second of a series of articles about Passing parameter between two zul files using MVC Design pattern.This article will focus on the How to pass some arguments from one window to modal window. 

So What we are going to do ?
1. Assume that you have small screen with a button to show another screen which opened as modal.
2. Now on Clicking of the Button, we will show the Second screen and also we will pass some arguments to the destination window.
In the first article, we just displayed the passed arguments in zul file which do not have any controller. This example will how you can receive the passed arguments in the controller and display back to the view.

Step 1:
If you are new ZK, then first set the development environment by following this document.

Step 2:
Let us create the Project in eclipse. Click File -> New -> ZK Project . Let us name the project as ZKMVCPassingParameter2

image
Step 3:
Now let us create a zul file in the name of ParentWindow.zul. This zul is pretty simple contains two labels with textboxes and one button. So on clicking the button, we will call another zul file and also we will also pass the value of the text boxes typed by the user.
<?page title="Parent Window" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Parent Window" border="normal" apply="org.com.demo.ParentWindowController">
<label value="First Name"></label>
<separator></separator>
<textbox id="firstName"></textbox>
<separator></separator>
<label value="Last Name"></label>
<separator></separator>
<textbox id="lastName"></textbox>
<separator></separator>
<button label="Show Window" id="showWindow"></button>
</window>
</zk>


Step 4:
Now let us create a zul file in the name of ChildWindow.zul. In this zul file also, we have two labels and two text boxes in which by default when this zul file is called we are going to receive the arguments and fill the values.

<zk>
 
    <window id="childwindow" title="Child Window" border="normal"
 
        mode="modal" width="20%" apply="org.com.demo.ChildWindowController">
 
        <label value="First Name"></label>
 
        <separator></separator>
 
        <textbox id="firstName"></textbox>
 
        <separator></separator>
 
        <label value="Last Name"></label>
 
        <separator></separator>
 
        <textbox id="lastName"></textbox>
 
        <separator></separator>
 
        <button label="Close" onClick="childwindow.detach()"></button>
 
    </window>
 
</zk>

Step 5:
Now let us start the actual implementation to achieve our output. First we will we will add our controller to our Parentwindow.zul as follows. Create a package called "org.com.demo" and create a java file called "ParentWindowController.java" Now on button click, we will call the ChildWindow.zul with the arguments as shown here.


package org.com.demo;
 
import java.util.HashMap;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;
 
@SuppressWarnings("serial")
 
public class ParentWindowController extends SelectorComposer<Window> {
 
    @Wire
 
    Textbox firstName;
 
    @Wire
 
    Textbox lastName;
 
    @Listen("onClick=#showWindow")
 
    public void submit() {
 
        final HashMap<String, Object> map = new HashMap<String, Object>();
 
        map.put("firstName", firstName.getValue());
 
        map.put("lastName", lastName.getValue());
 
        Executions.createComponents("ChildWindow.zul", null, map);
    }
 
}
Step 6:
Now Let receive the parameter in the ChildWindow controller class. In the package “org.com.demo" and create a java file called "ChildWindowController.java"

import java.util.HashMap;
 
 
 
import org.zkoss.zk.ui.Execution;
 
import org.zkoss.zk.ui.Executions;
 
import org.zkoss.zk.ui.select.SelectorComposer;
 
import org.zkoss.zk.ui.select.annotation.Listen;
 
import org.zkoss.zk.ui.select.annotation.Wire;
 
import org.zkoss.zul.Textbox;
 
import org.zkoss.zul.Window;
 
 
 
@SuppressWarnings("serial")
 
public class ChildWindowController extends SelectorComposer<Window> {
 
 
 
    @Wire
 
    Textbox firstName;
 
    @Wire
 
    Textbox lastName;
 
 
 
    public void doAfterCompose(Window comp) throws Exception {
 
        super.doAfterCompose(comp);
 
        final Execution execution = Executions.getCurrent();
 
        firstName.setValue((String) execution.getArg().get("firstName"));
 
        lastName.setValue((String) execution.getArg().get("lastName"));
 
    }
 
 
 
}

Step 7:
That’s all.  Now you can run ParentWindow.zul and type some value for first and last Name and click the button to show the values in the childwindow.zul
Project Structure

image


No comments:

Post a Comment