Zk how to pass parameter from java code to zk page

2019-08-15 13:58发布

问题:

I am novice in ZK.

I use very old zk framework version(legacy project).

I render zk page so:

Executions.createComponents("/myZul.zul", null, null))

I need to pass parameter to zul. And if parameter is true I need render checkbox and otherwise - not on myZul.zul

I need something like this on zul:

if(parameter){

    <checkbox id="my_id" label="my checkbox"  />
}

UPDATE my zul:

<window xmlns="http://www.zkoss.org/2005/zul" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:h="http://www.w3.org/1999/xhtml" 
        xmlns:zk="http://www.zkoss.org/2005/zk"
        xmlns:ca="http://www.zkoss.org/2005/zk/client"
        xsi:schemaLocation="http://www.zkoss.org/2005/zul      http://www.zkoss.org/2005/zul/zul.xsd "
        border="normal"
        closable="false"
        position="center,center"
        width="383px"
        height="270px"
        onCancel="self.detach();"
        id="decisionCommentWindow"
        title="${c:l('approvalTaskWindow.title')}"
        use="handlers.ZulHandler">
        ....

I need override method doAfterCompose inside of ZulHandler?

One more time:

zul:

<window xmlns="http://www.zkoss.org/2005/zul" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:h="http://www.w3.org/1999/xhtml" 
        xmlns:zk="http://www.zkoss.org/2005/zk"
        xmlns:ca="http://www.zkoss.org/2005/zk/client"
        xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd "
        border="normal"
        closable="false"
        position="center,center"
        width="383px"
        height="270px"
        onCancel="self.detach();"
        id="decisionCommentWindow"
        title="${c:l('approvalTaskWindow.title')}"
        use="handlers.ZulHandler">


    <zk if="${isManufacturingKey}">

        <checkbox id="checkbox_id" label="check"  />

    </zk>






</window>

zul creater:

...
Map args = new HashMap<String, Boolean>();
                    Boolean isManufacturing = true;
                    args.put("isManufacturingKey", isManufacturing);
                    ZulHandler window = Preconditions
                            .checkNotNull((ZulHandler) Executions.createComponents(
                                    "/decision_comment_window.zul", null, args));
                    window.setTitle(decisionModel.getName() + " decision");
                    if (isManufacturing) {
                        Checkbox checkbox = (Checkbox) Path
                                .getComponent("/decisionCommentWindow/emergency_change_checkbox_id");
                        checkbox.setChecked(workflow.getEmergencyChange());//I have Null pointer here  because checkbox is null
                    }
...

回答1:

You can do this :

HashMap map = new HashMap<String, String>();
Boolean isManufacturing = true;
args.put("isManufacturingKey", isManufacturing);   
map.put("isManufactringChecked",workflow.getEmergencyChange());
Executions.createComponents("/myZul.zul", null , map);

create this controller :

public class ZulHandler extends SelectorComposer {

  @Wire("#win")
  private Window myWin;
  private Boolean manufacturing;
  private Boolean manufactringChecked; 
  @Override
  public void doAfterCompose(Component window) throws Exception {
    super.doAfterCompose(window);

    if (arg.containsKey("isManufacturingKey")) {
        manufacturing =  (Boolean) arg.get("isManufacturingKey"));
    } else {
       //Declare what is is when you don't have the arg
    }
    if (arg.containsKey("isManufactringChecked")) {
        manufactringChecked =  (Boolean) arg.get("isManufactringChecked"));
    } else {
       //Declare what is is when you don't have the arg
    }
}

public Boolean isManufacturing () {
    return manufacturing;
}

public Boolean isManufactringChecked() {
    return manufactringChecked ;
}

change your zul from this :

use="handlers.ZulHandler">
<zk if="${isManufacturingKey}">
    <checkbox id="checkbox_id" label="check" />
</zk>

to :

apply="handlers.ZulHandler">
<checkbox id="checkbox_id" label="check" checked="${$composer.manufactringChecked}"
    visible="${$composer.manufacturing}"/>

Now you just need to implement the doOk method there. If its a button that just close that window you can do the following in the zul :

<button label="Close" onClick="spaceOwner.detach()" />

edit:
Mine first solution was correct for your problem. You just have the next problem because you work wrong. Your problem is that you want your caller to influence the target. Just give params and let the controller of your target do the work.
It's like a car factory, they say your car can drive up to 200km/hr, but you as controller of the car, do you drive 200 km/hr or when its not specified, can you still drive the car?
You also don't need to make dubbelposts for the same issue. You could make an issue for the nullpointer but the title is almost the same and what's in the post also.

edit:

If you need to set the title => put the title in the args and claim in in your code. otherwise, I edited the controller class so you have your window variable.(or you make yourself a public getter and setter for the window)



回答2:

I created a zul and set arg in main zul. send a parameter and I want to change this parameter. For example:

Map args = new HashMap<String, Boolean>(); 
args.put("ReadOnly", false);
Executions.createComponents("/example.zul", null, args);

if I save button cliked and changing ReadOnly true, How to change ReadOnly in main.zul;



回答3:

Try it:

<zk if="${parameter}">

<checkbox id="my_id" label="my checkbox"  />

</zk>

more info:http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Advanced/Pass_Arguments_to_Include_Component



回答4:

java code:

Map args = new HashMap<String, Boolean>();
args.put("isManufacturingKey", true);
ZulHandler window = Preconditions.checkNotNull((ZulHandler) Executions.createComponents("/decision_comment_window.zul", null, args)); 

right access in .zul file:

"${arg.isManufacturingKey}"

note: I access to parameter using arg accessor

It is working for my ZK version!!!



标签: java zk