我研究 ZK 框架已经有一段时间了。我无法弄清楚如何在 HTTP 请求中设置参数并在其他页面中获取它们?
我也无法使用@GlobalCommand
注释。这实际上是如何@GlobalCommand
工作的?
我不想将值发送到弹出窗口或模式对话框。我想通过 sendRedirect 等将值发送到独立的 zul 页面......
请提供这两件事的例子。
我做的例子@GlobalCommand
DemoVM.java
package com.nagarro.viewmodel;
import java.util.HashMap;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.zk.ui.Executions;
import org.zkoss.bind.annotation.NotifyChange;
public class DemoVM {
private String value1;
private String value2;
@Command
public void showModelWin() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("value1", this.value1);
map.put("value2", this.value2);
BindUtils.postGlobalCommand(null, null, "getValues", map);
Executions.sendRedirect("ModelWindow.zul");
}
@GlobalCommand
@NotifyChange({ "value1", "value2" })
public void refreshvalues(@BindingParam("returnvalue1") String str1,
@BindingParam("returnvalue2") String str2) {
this.value1 = str1;
this.value2 = str2;
}
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
}
演示.zul
<?page title="Demo Page" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="MVVM Modal window Passing arguments and return values"
border="normal" apply="org.zkoss.bind.BindComposer" id="DemoVM"
viewModel="@id('e') @init('com.nagarro.viewmodel.DemoVM')">
Type any value and Press the Model Window Button
<separator />
Value 1 :
<textbox value="@bind(e.value1)" />
Value 2 :
<textbox value="@bind(e.value2)" />
<button label="Model Window" onClick=" @command('showModelWin')" />
</window>
</zk>
模型窗口VM.java
package com.nagarro.viewmodel;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import java.util.HashMap;
import org.zkoss.bind.BindUtils;
import java.util.Map;
public class ModelWindowVM {
private String value1;
private String value2;
@Init
public void init(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("value1") String v1,
@ExecutionArgParam("value2") String v2) {
this.value1 = v1;
this.value2 = v2;
}
@Command
public void save() {
Map<String, Object> args = new HashMap<String, Object>();
args.put("returnvalue1", this.value1);
args.put("returnvalue2", this.value2);
BindUtils.postGlobalCommand(null, null, "refreshvalues", args);
Executions.sendRedirect("Demo.zul");
}
@GlobalCommand("getValues")
@NotifyChange({"value1","value2"})
public void getValues(@BindingParam("value1") String value1, @BindingParam("value2") String value2){
this.value1 = value1;
this.value2 = value2;
}
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
}
模型窗口.zul
<?page title="Model Window Page" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="modalDialog"
title="MVVM Modal window Passing arguments and retur values"
width="420px" height="auto" border="normal" minimizable="false"
maximizable="false" closable="true"
action="hide: slideUp" apply="org.zkoss.bind.BindComposer"
onCancel="@command('closeThis')"
viewModel="@id('e') @init('com.nagarro.viewmodel.ModelWindowVM')">
Change the values and Press the Ok Button to return changed
values.
<separator />
Value 1 :
<textbox value="@bind(e.value1)" />
Value 2 :
<textbox value="@bind(e.value2)" />
<button label="Ok" onClick="@command('save')" />
</window>
</zk>
我使用了 BindUtils.postGlobalCommand() 方法,但它不起作用。