Wicket LoadableDetachableModel 更新问题
我有 3 个 LoadableDetachableModel。在他们的加载方法中,我调用全局变量(同一类)。在两种情况下,模型工作得很好(方法 testModel1(); 和 testModel3();)。
但!在 testModel2() 中;是第一次调用此页面后的剩余值。
我需要解释这怎么可能。
起初我以为ResourceReference在init之后就不能改变了。然而方法 testModel3() 证明了我并非如此。 (此方法工作正常)。
链接在pastebin.com: http://pastebin.com/jiFVWMMW
输出:
构造对象 在 testModel1 中值 = 25.26.609 在 testModel2 中值 = 25.26.609 在 testModel2 中值 = 25.26.609 在 testModel3 中值 = 25.26.671 在 testModel3 中值 = 25.26.671 构造对象 在 testModel1 中值 = 25.31.625 在 testModel2 中值 = 25.26.609 在 testModel2 中值 = 25.26.609 在 testModel3 中值 = 25.31.640 在 testModel3 中值 = 25.31.640 构造对象 在 testModel1 中值 = 25.32.125 在 testModel2 中值 = 25.26.609 在 testModel2 中值 = 25.26.609 在 testModel3 中值 = 25.32.140 在 testModel3 值 = 25.32.140
代码:
package app.web;
import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.Resource;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.time.Time;
import org.wicketstuff.annotation.mount.MountPath;
/**
* HomePage
*/
@MountPath(path = "home")
public final class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
private static final String DATE_FORMAT = "mm.ss.SS";
transient private final Map cssMap;
public HomePage() {
System.out.println("Constructing object");
cssMap = new HashMap();
cssMap.put("time", Time.now().toString(DATE_FORMAT));
}
@Override
protected void onBeforeRender() {
testModel1();
testModel2();
testModel3();
super.onBeforeRender();
}
/**
* working just fine
*/
private void testModel1() {
add(new Label("message1", new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel1 value = " + ((String) cssMap.get("time")));
return ((String) cssMap.get("time"));
}
}));
}
/**
* DOES NOT UPDATE!!!! (load is being called, but cssMap has original value. WTF?!
*/
private void testModel2() {
add(new StyleSheetReference("cssReference", new ResourceReference("testcss.css") {
private static final long serialVersionUID = 1L;
@Override
protected Resource newResource() {
return new Resource() {
private static final long serialVersionUID = 1L;
@Override
public IResourceStream getResourceStream() {
LoadableDetachableModel LDM = new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel2 value = " + ((String) cssMap.get("time")));
return ((String) cssMap.get("time"));
}
};
return new StringResourceStream(LDM.getObject());
}
};
}
}));
}
/**
* work just fine - StyleSheet is changing...
*/
private void testModel3() {
add(new StyleSheetReference("cssReference2", new ResourceReference("testcss2.css") {
private static final long serialVersionUID = 1L;
@Override
protected Resource newResource() {
return new Resource() {
private static final long serialVersionUID = 1L;
@Override
public IResourceStream getResourceStream() {
LoadableDetachableModel LDM = new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel3 value = " + Time.now().toString(DATE_FORMAT));
return Time.now().toString(DATE_FORMAT);
}
};
return new StringResourceStream(LDM.getObject());
}
};
}
}));
}
}
html 标记:
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<link wicket:id="cssReference"/>
<link wicket:id="cssReference2"/>
<body>
message1 will be here
</body>
</html>
testcss.css: ${time}
testcss2.css: ${时间}
I have 3 LoadableDetachableModels. In their load method I am calling global variable (of the same class). In two cases, models are working just fine (method testModel1(); and testModel3();).
BUT! In testModel2(); is remaining value from first calling this Page.
I need explanation how is that even possible.
At first, I thought that ResourceReference can not be changed after init. However method testModel3() proved me otherwise. (this method is working correctly).
link on pastebin.com:
http://pastebin.com/jiFVWMMW
ouput:
Constructing object
in testModel1 value = 25.26.609
in testModel2 value = 25.26.609
in testModel2 value = 25.26.609
in testModel3 value = 25.26.671
in testModel3 value = 25.26.671
Constructing object
in testModel1 value = 25.31.625
in testModel2 value = 25.26.609
in testModel2 value = 25.26.609
in testModel3 value = 25.31.640
in testModel3 value = 25.31.640
Constructing object
in testModel1 value = 25.32.125
in testModel2 value = 25.26.609
in testModel2 value = 25.26.609
in testModel3 value = 25.32.140
in testModel3 value = 25.32.140
code:
package app.web;
import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.Resource;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.time.Time;
import org.wicketstuff.annotation.mount.MountPath;
/**
* HomePage
*/
@MountPath(path = "home")
public final class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
private static final String DATE_FORMAT = "mm.ss.SS";
transient private final Map cssMap;
public HomePage() {
System.out.println("Constructing object");
cssMap = new HashMap();
cssMap.put("time", Time.now().toString(DATE_FORMAT));
}
@Override
protected void onBeforeRender() {
testModel1();
testModel2();
testModel3();
super.onBeforeRender();
}
/**
* working just fine
*/
private void testModel1() {
add(new Label("message1", new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel1 value = " + ((String) cssMap.get("time")));
return ((String) cssMap.get("time"));
}
}));
}
/**
* DOES NOT UPDATE!!!! (load is being called, but cssMap has original value. WTF?!
*/
private void testModel2() {
add(new StyleSheetReference("cssReference", new ResourceReference("testcss.css") {
private static final long serialVersionUID = 1L;
@Override
protected Resource newResource() {
return new Resource() {
private static final long serialVersionUID = 1L;
@Override
public IResourceStream getResourceStream() {
LoadableDetachableModel LDM = new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel2 value = " + ((String) cssMap.get("time")));
return ((String) cssMap.get("time"));
}
};
return new StringResourceStream(LDM.getObject());
}
};
}
}));
}
/**
* work just fine - StyleSheet is changing...
*/
private void testModel3() {
add(new StyleSheetReference("cssReference2", new ResourceReference("testcss2.css") {
private static final long serialVersionUID = 1L;
@Override
protected Resource newResource() {
return new Resource() {
private static final long serialVersionUID = 1L;
@Override
public IResourceStream getResourceStream() {
LoadableDetachableModel LDM = new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override
protected String load() {
System.out.println("in testModel3 value = " + Time.now().toString(DATE_FORMAT));
return Time.now().toString(DATE_FORMAT);
}
};
return new StringResourceStream(LDM.getObject());
}
};
}
}));
}
}
html markup:
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<link wicket:id="cssReference"/>
<link wicket:id="cssReference2"/>
<body>
message1 will be here
</body>
</html>
testcss.css:
${time}
testcss2.css:
${time}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该模型永远不会分离,也不绑定到组件。当组件分离时,组件绑定模型会自动分离,该模型永远不会分离,因此值始终保持不变。
如果你像这样重组你的类,它应该可以工作:
This model is never detached, nor is it bound to a component. Component-bound models get detached automatically when the components are detached, this Model is never detached and hence the value always remains the same.
It should work if you restructure your class like this: