关于apply属性使用的一些例子(转载来自用户:tan_dan)

 

前些天根据在ZK官网上找了些关于apply的使用例子,例子很简单。但是还是受益
匪浅。为避免忘记,做下详细的代码记录:

ZUL 文件:

  1. <zk>
  2. <!--方式一 <window title="composer1 example" width="300px" apply="com.ui.MyComposer" border="normal"> -->
  3. <!--方式二 <window title="composer1 example" width="300px" apply="com.ui.MyComposer1" border="normal"> -->
  4. <!--方式二 <window title="composer1 example" width="300px" apply="com.ui.MyComposer2" border="normal"> -->
  5. <windowtitle="composer1 example"width="300px"apply="com.ui.MyComposer3"border="normal">
  6. <grid>
  7. <rows>
  8. <row>First name :<textboxid="fname"forward="onChange=onFirstName"/></row>
  9. <row>Second name :<textboxid="sname"forward="onChange=onSecondName"/></row>
  10. <row>Full name :<labelid="fullname"/></row>
  11. </rows>
  12. </grid>
  13. </window>
  14. </zk>

通过指定apply的内容调用UI层的不同方法。

例1:com.ui.MyComposer 文件代码:

  1. public class MyComposer implements Composer{ //实现Composer接口
  2. private Textbox firstname;
  3. private Textbox secondname;
  4. private Label fullname;
  5. public void doAfterCompose(Component win) throws Exception {
  6. firstname = (Textbox) win.getFellow("fname"); //页面的数据通过getFellow获得
  7. secondname = (Textbox) win.getFellow("sname");
  8. fullname = (Label) win.getFellow("fullname");
  9. //定义并注册事件监听器
  10. win.addEventListener("onFirstName",
  11. new EventListener() {
  12. public void onEvent(Event even) throws Exception {
  13. fullname.setValue(firstname.getValue()+" "+secondname.getValue());
  14. }
  15. });
  16. win.addEventListener("onSecondName",
  17. new EventListener() {
  18. public void onEvent(Event arg0) throws Exception {
  19. fullname.setValue(firstname.getValue()+" "+secondname.getValue());
  20. }
  21. });
  22. }
  23. }

例2:com.ui.MyComposer1 文件代码:

  1. public class MyComposer1 extends GenericComposer { 继承GenericComposer 类
  2. private Textbox firstname;
  3. private Textbox secondname;
  4. private Label fullname;
  5. public void doAfterCompose(Component win) throws Exception {
  6. super.doAfterCompose(win);
  7. firstname = (Textbox) win.getFellow("fname");
  8. secondname = (Textbox) win.getFellow("sname");
  9. fullname = (Label) win.getFellow("fullname");
  10. //添加 addEventListener(remove 到该方法外)
  11. }
  12. public void onFirstName(Event event){
  13. fullname.setValue(firstname.getValue()+" "+ secondname.getValue());
  14. }
  15. public void onSecondName(Event event){
  16. fullname.setValue(firstname.getValue()+" "+ secondname.getValue());
  17. }
  18. }

相对于MyComposer 来说, MyComposer1 的代码更具有可读性,它将监听器作为独立的方法从doAfterCompose方法
中分离出来,但其执行的效果是相同的。这是因为MyCompser1 继承了 GenericComposer 类


例3:MyComposer2.java 文件代码:

  1. public class MyComposer2 extends GenericAutowireComposer {
  2. private Textbox fname; //auto-wired (属性名要与id标志匹配)
  3. private Textbox sname;
  4. private Label fullname;
  5. //所有的getFellow 都可以省了
  6. public void onFirstName(Event event){
  7. fullname.setValue(fname.getValue()+" "+sname.getValue());
  8. }
  9. public void onSecondName(Event event){
  10. fullname.setValue(fname.getValue()+" "+sname.getValue());
  11. }
  12. }

例3相比于例2来说,代码更为简洁。它通过继承GenericAutowireComposer ,只要属性名与Id值相同
就可以自动绑定数据而不需要调用getFellow方法。

GenericAutowireComposer类中的doAfterCompose 方法会自动帮你注入匹配的值。包括Spring的bean类
如例4:

spring-config.xml 文件代码:

  1. ...
  2. <bean id="taskDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  3. ...

taskEditor.zul 文件代码:

  1. <window id="taskWnd" title="Task Editor" border="normal" width="500px" apply="TaskEditorComposer">
  2. <grid>
  3. <rows>
  4. <row>Title: <textbox id="title"/></row>
  5. <row>Description: <textbox id="description"/></row>
  6. </rows>
  7. </grid>
  8. <button id="saveBtn" label="Save" forward="onClick=onSaveTask"/>
  9. </window>


TaskEditorComposer.java 文件代码

  1. public class TaskEditorComposer extends GenericAutowireComposer {
  2. private TransactionProxyFactoryBean taskDao; //Spring bean auto wired
  3. private Textbox title; //auto wired
  4. private Textbox description; //auto wired
  5. private Button saveBtn; //auto wired
  6. public void onSaveTask(Event event) {
  7. Task currentTask = componentScope.get("task");
  8. currentTask.setTitle(title.getValue());
  9. currentTask.setDescription(description.getValue());
  10. taskDao.update(currentTask);
  11. }
  12. ...
  13. }


例5:MyComposer3.java 文件代码:

该例子是为了说明GenericAutowireComposer 类支持隐式对象,例子用"self"举例

  1. public class MyComposer3 extends GenericAutowireComposer{
  2. private Textbox fname;
  3. private Textbox sname;
  4. private Label fullname;
  5. public void onFirstName(Event event){
  6. fullname.setValue(fname.getValue()+" "+sname.getValue());
  7. ((Window)self).setTitle("First Name Changed ");
  8. alert("First Name Changed to " + fname.getValue());
  9. }
  10. public void onSecondName(Event event){
  11. fullname.setValue(fname.getValue()+" "+sname.getValue());
  12. ((Window)self).setTitle("Second Name Changed ");
  13. alert("Second Name Changed to " + sname.getValue());
  14. }
  15. }

例6 是例5的另一种实现。将java代码嵌套在zul文件中

例6:comp1_3.zul 文件代码:

  1. <zk>
  2. <window title="composer 1_3 example " border="normal" width="300px">
  3. <attribute name="onFirstName">
  4. <!--[CDATA[
  5. fullname.setValue(fname.getValue()+" "+sname.getValue());
  6. ((Window)self).setTitle("First name changed");
  7. alert("First name change to " + fname.getValue());
  8. ]]>
  9. </attribute>
  10. <attribute name="onSecondName">
  11. <![CDATA[
  12. fullname.setValue(fname.getValue()+" "+sname.getValue());
  13. ((Window)self).setTitle("Second name changed");
  14. alert("Second name change to " + sname.getValue());
  15. ]]-->
  16. </attribute>
  17. <grid>
  18. <rows>
  19. <row>First Name : <textbox id="fname" forward="onChange=onFirstName"/></row>
  20. <row>Second Name : <textbox id="sname" forward="onChange=onSecondName"/></row>
  21. <row>Full Name : <label id="fullname"/></row>
  22. </rows>
  23. </grid>
  24. </window>
  25. </zk>


例7 : comp2.zul 文件代码

  1. <zk>
  2. <!-- <window title="comp2 example " border="normal" apply="com.ui.MyComposer4" width="500"> -->
  3. <window title="comp2 example " border="normal" apply="com.ui.MyComposer5" width="500">
  4. <grid>
  5. <rows>
  6. <row>First Name:<textbox id="fname"/></row>
  7. <row>Second Name:<textbox id="sname"/></row>
  8. <row>full Name:<label id="fullname"/></row>
  9. </rows>
  10. </grid>
  11. </window>
  12. </zk>


MyComposer4.java 文件代码:


  1. public class MyComposer4 extends GenericForwardComposer{
  2. private Label fullname;
  3. private Textbox fname;
  4. private Textbox sname;
  5. public void onChange$fname(){
  6. fullname.setValue(fname.getValue()+" "+sname.getValue() );
  7. }
  8. public void onChange$sname(){
  9. fullname.setValue(fname.getValue()+" "+sname.getValue());
  10. }
  11. public void onClick$fullname(){
  12. ((Window)self).setTitle(" full name OnClick ");
  13. alert("the full name is " + fullname.getValue());
  14. }
  15. }


MyComposer4 类继承了 GenericForwardComposer.可以通过监听器的方法名指定监听的
组件(以xx$xx的方式),不需要在zul文件中指定 “forward” 属性


例8 :实际应用中,当你不得不实现其他接口,继承其他的类时,你可以使用以下方式以达到例6的效果

MyComposer5.java 文件代码:

  1. public class MyComposer5 implements Composer{
  2. private Textbox fname;
  3. private Textbox sname;
  4. private Label fullname;
  5. public void doAfterCompose(Component win) throws Exception {
  6. //绑定变量值
  7. Components.wireVariables(win, this);
  8. //注册onXXX事件监听器
  9. Events.addEventListeners(win, this);
  10. //自动跳转
  11. Components.addForwards(win, this);
  12. }
  13. public void onChange$fname(Event event){
  14. fullname.setValue(fname.getValue()+" "+sname.getValue());
  15. }
  16. public void onChange$sname(Event event){
  17. fullname.setValue(fname.getValue()+" "+sname.getValue());
  18. }
  19. }


例9 : 当需要使用use 属性时

comp4.zul 文件代码:

  1. <window title="mywindow example" border="normal" width="300px" use="MyWindow">
  2. <grid>
  3. <rows>
  4. <row>First Name: <textbox id="firstName"/></row>
  5. <row>Last Name: <textbox id="lastName"/></row>
  6. <row>Full Name: <label id="fullName"/></row>
  7. </rows>
  8. </grid>
  9. </window>


MyComposer.java 文件代码:


  1. public class MyComposer6 extends Window implements AfterCompose{
  2. private Textbox fname;
  3. private Textbox sname;
  4. private Label fullname;
  5. public void afterCompose() {
  6. //绑定属性值
  7. Components.wireVariables(this, this);
  8. //此时 不需要注册事件监听器
  9. //指定自动跳转
  10. Components.addForwards(this, this);
  11. }
  12. public void onChange$fname(Event event){
  13. fullname.setValue(fname.getValue()+" "+ sname.getValue());
  14. }
  15. public void onChange$sname(Event event){
  16. fullname.setValue(fullname.getValue()+" "+ sname.getValue());
  17. }
  18. }
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值