Tuesday, 24 April 2012

ZK MVC Using GenericForwardComposer utility class


Welcome back

This is the 4th post in a series of ZK MVC

Here is the zk documentation and we are following that
and creating new example

http://books.zkoss.org/wiki/Small_Talks/2008/August/ZK_MVC_Made_Easy




Here is the Summary

Method 1 : Implements Composer
We implement Composer interface. Here we used getfellow method to hold the
reference for the UI Components and also we added listener to handle
events

Here is the Link for the first post
http://emrpms.blogspot.in/2012/04/mvc-using-composer-interface.html


Method 2 : extends GenericComposer
Here, we removed all the event lisener by extending the GenericComposer
But remember, we need to call super doaftercompose.

Here is the link for the second post
http://emrpms.blogspot.in/2012/04/zk-mvc-using-genericcomposer-utility.html

Method 3: extends GenericAutowireComposer
Here, we removed all the getfellow methods and let them auto wired
Here we dont need override doaftercompose

http://emrpms.blogspot.in/2012/04/zk-mvc-using-genericautowirecomposer.html

Until now, the previous example codes always use the forward attribute in zul file to forward
event of the button click. Can we remove such forward attributes?"

The org.zkoss.zk.ui.util.GenericForwardComposer utility class is designed to fulfill this requirement. Note that GenericForwardComposer extends the GenericAutowireComposer class, so it has all features of its parent class(GenericAutowireComposer) and grandparent class(GenericComposer).

Here is my zul code

<?page title="Example7" contentType="text/html;charset=UTF-8"?>
<zk>
<label
value=" http://books.zkoss.org/wiki/Small_Talks/2008/August/ZK_MVC_Made_Easy.
Using org.zkoss.zk.ui.util.GenericForwardComposer utility class"
style="font-size : 18px;font-family: verdana,arial,sans-serif;" />
<separator />
<window
title="MVC Pattern Using org.zkoss.zk.ui.util.GenericForwardComposer utility class"
border="normal" width="700px" apply="com.me.Example9">
<grid>
<columns>
<column label="" />
<column label="" />
</columns>
<rows>
<row>
First Name :
<textbox id="firstName" />
</row>
<row>
Last Name :
<textbox id="lastName" />
</row>
<row>
Address :
<textbox id="address" />
</row>
<row>
<button id="Clear" label="Clear" />
</row>

</rows>
</grid>
</window>
</zk>


In the above code, we have used apply attribute of the XML. By this, we are associating
with a component, such that the composer can control the UI rooted the given component.



Here is composer(java file)


package com.me;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Textbox;

@SuppressWarnings("rawtypes")
public class Example9 extends GenericForwardComposer {

private static final long serialVersionUID = 1L;
private Textbox firstName;
private Textbox lastName;
private Textbox address;

public void onClick$Clear(Event event) {
firstName.setValue("");
lastName.setValue("");
address.setValue("");
}
}

Important Notes in the above code.

1. The event handler must be declared as public. Otherwise, they will be ignored from auto-wiring.



Here is the demo
http://zkfiddle.org/direct/3c7jise/2/v6.0.0-MVC-Using-GenericForwardComposer?run=3gbaqbu

Here is the source code
http://zkfiddle.org/sample/3c7jise/2-MVC-Using-GenericForwardComposer

2 comments:

  1. Hello,

    What is better SelectorComposer or GenericForwardComposer?

    Thanks.

    ReplyDelete