Wicket LoadableDetachableModel 更新问题

发布于 2024-10-20 05:43:24 字数 5218 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-10-27 05:43:24
LoadableDetachableModel LDM = new LoadableDetachableModel()

该模型永远不会分离,也不绑定到组件。当组件分离时,组件绑定模型会自动分离,该模型永远不会分离,因此值始终保持不变。

如果你像这样重组你的类,它应该可以工作:

// model should be a field, so detachModels() can see it
private IModel<String> LDM = new LoadableDetachableModel<String>() {

    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);
    }
};

public void detachModels() {
    super.detachModels();
    // detach the model
    LDM.detach();
}

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() {
                    return new StringResourceStream(LDM.getObject());
                }
            };
        }
    }));
}
LoadableDetachableModel LDM = new LoadableDetachableModel()

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:

// model should be a field, so detachModels() can see it
private IModel<String> LDM = new LoadableDetachableModel<String>() {

    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);
    }
};

public void detachModels() {
    super.detachModels();
    // detach the model
    LDM.detach();
}

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() {
                    return new StringResourceStream(LDM.getObject());
                }
            };
        }
    }));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文