Monday, 7 January 2013

MVVM Pass Parameter between two zul files using URL Redirect

In this post, we will see how we can pass parameter between two zul files attached by MVVM using URL Redirect

Project Name : MVVMPassParameterUsingReDirect
ZK Version :  6.5.0 CE

Project Structure:

image
one.zul file

   1: <?page title="new page title" contentType="text/html;charset=UTF-8"?>
   2: <zk>
   3:     <window title="MVVM window Passing arguments and retur values. This is one.zul"
   4:         border="normal" apply="org.zkoss.bind.BindComposer"
   5:         viewModel="@id('e') @init('com.demo.UI.oneVM')">
   6:         <separator></separator>
   7:         <label value="ZK Version : ${desktop.webApp.version}"/>
   8:         <separator></separator>
   9:         Type any value and Press  Button to show the Show two.zul file by redirecting the URL
  10:         <separator></separator>
  11:         Value 1 :
  12:         <textbox value="@bind(e.value1)" />
  13:         Value 2 :
  14:         <textbox value="@bind(e.value2)" />
  15:         
  16:         <button label="two.zul" onClick="@command('showtwozul')" />
  17:     </window>
  18: </zk>

two.zul file



   1: <?page title="new page title" contentType="text/html;charset=UTF-8"?>
   2: <zk>
   3:  
   4:     <window id="two"
   5:         title="MVVM Modal window Passing arguments and retur values. This is two.zul"
   6:         action="hide: slideUp" apply="org.zkoss.bind.BindComposer"
   7:         onCancel="@command('closeThis')"
   8:         viewModel="@id('e') @init('com.demo.UI.twoVM')">
   9:         <separator />
  10:         Value 1 :
  11:         <textbox value="@bind(e.value1)" />
  12:         Value 2 :
  13:         <textbox value="@bind(e.value2)" />
  14:         <button label="Close" onClick="@command('closeThis')" />
  15:     </window>
  16: </zk>



oneVM.java



   1: package com.demo.UI;
   2:  
   3: import java.util.HashMap;
   4:  
   5: import org.zkoss.bind.annotation.Command;
   6: import org.zkoss.zk.ui.Executions;
   7: import org.zkoss.zk.ui.Sessions;
   8:  
   9: public class oneVM {
  10:  
  11:     private String value1;
  12:     private String value2;
  13:     
  14:  
  15:     public String getValue1() {
  16:         return value1;
  17:     }
  18:  
  19:     public void setValue1(String value1) {
  20:         this.value1 = value1;
  21:     }
  22:  
  23:     public String getValue2() {
  24:         return value2;
  25:     }
  26:  
  27:     public void setValue2(String value2) {
  28:         this.value2 = value2;
  29:     }
  30:     
  31:     @Command
  32:     public void showtwozul()
  33:     {
  34:         final HashMap<String, Object> map = new HashMap<String, Object>();
  35:         map.put("value1", this.value1 );
  36:         map.put("value2", this.value2);
  37:         Sessions.getCurrent().setAttribute("allmyvalues", map);
  38:         Executions.sendRedirect("two.zul");
  39:         
  40:     }
  41:     
  42:  
  43: }

twoVM.java



   1: package com.demo.UI;
   2:  
   3: import java.util.HashMap;
   4: import org.zkoss.bind.annotation.Command;
   5: import org.zkoss.bind.annotation.ContextParam;
   6: import org.zkoss.bind.annotation.ContextType;
   7: import org.zkoss.bind.annotation.Init;
   8: import org.zkoss.zk.ui.Component;
   9: import org.zkoss.zk.ui.Sessions;
  10: import org.zkoss.zk.ui.select.Selectors;
  11: import org.zkoss.zk.ui.select.annotation.Wire;
  12: import org.zkoss.zul.Window;
  13:  
  14: public class twoVM {
  15:  
  16:     @Wire("#two")
  17:     private Window win;
  18:     private String value1;
  19:     private String value2;
  20:  
  21:     public String getValue1() {
  22:         return value1;
  23:     }
  24:  
  25:     public void setValue1(String value1) {
  26:         this.value1 = value1;
  27:     }
  28:  
  29:     public String getValue2() {
  30:         return value2;
  31:     }
  32:  
  33:     public void setValue2(String value2) {
  34:         this.value2 = value2;
  35:     }
  36:  
  37:     @Init
  38:     public void init(@ContextParam(ContextType.VIEW) Component view) {
  39:         Selectors.wireComponents(view, this, false);
  40:  
  41:         final HashMap<String, Object> map = (HashMap<String, Object>) Sessions
  42:                 .getCurrent().getAttribute("allmyvalues");
  43:         this.value1 = (String) map.get("value1");
  44:         this.value2 = (String) map.get("value2");
  45:  
  46:     }
  47:  
  48:     @Command
  49:     public void closeThis() {
  50:         win.detach();
  51:     }
  52: }



Now run one.zul file and enter some values and click on two.zul file to pass the parameter to two.zul file

image

image


OnlineDemoButton                               image

4 comments:

  1. Thanks for the article.
    Can we do it with @GlobalCommand as well?

    ReplyDelete
  2. put parameters in sessions is the unique alternative?

    ReplyDelete
  3. put parameters in sessions is the unique alternative?

    ReplyDelete