-
Bug
-
Resolution: Fixed
-
Normal
-
8.0.0, 7.0.6
-
Security Level: Jimmy
-
ZK 8.0.1
-
None
using @WireVariable(value="testService", rewireOnActivate=true) in a SelectorComposer works as expected rewiring the variable (e.g. a spring bean). After session activation the the variable is resolved and wired again (e.g. after a server restart)
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) public class SpringTestComposer extends SelectorComposer<Component> { private static final long serialVersionUID = 1L; @WireVariable(value="testService", rewireOnActivate=true) private transient TestService testService;
When trying the same in an MVVM ViewModel the variable remains null after session activation.
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) public class SpringTestViewModel implements Serializable { private static final long serialVersionUID = 1L; @WireVariable(value = "testService", rewireOnActivate = true) private transient TestService testService;
The reason is BindComposer does not listen to session activation leaving the variable unwired (null).
A workaround is to extend BindComposer adding this function:
package org.zkoss.zk.ui.select; //needs to be this package import org.zkoss.bind.BindComposer; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.select.Selectors; import org.zkoss.zk.ui.util.ComponentActivationListener; public class ActivatingBindComposer<T extends Component> extends BindComposer<T> implements ComponentActivationListener{ private static final long serialVersionUID = 1L; public void didActivate(Component comp) { Selectors.rewireVariablesOnActivate(comp, this.getViewModel(), Selectors.newVariableResolvers(this.getViewModel().getClass(), null)); } public void willPassivate(Component arg0) { } }
then in the zul file the ActivatingBindComposer can be used instead of the default BindComposer
<div apply="org.zkoss.zk.ui.select.ActivatingBindComposer" viewModel="@id('vm') @init('zk.support.spring.SpringTestViewModel')" >