Wicket LoadableDetachableModel更新问题

时间:2011-03-04 00:45:05

标签: java wicket

我有3个LoadableDetachableModels。在他们的加载方法中,我调用全局变量(同一类)。在两种情况下,模型工作正常(方法testModel1();和testModel3();)。

BUT!在testModel2()中;是第一次调用此页面时的剩余价值。

我需要解释这是怎么回事。

首先,我认为在init之后无法更改ResourceReference。然而,方法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: $ {time}

1 个答案:

答案 0 :(得分:1)

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