简体   繁体   English

嵌套ModalWindow的Wicket序列化问题

[英]Wicket serialization issue with nested ModalWindow

I've struck a problem with Java and Apache Wicket 1.5 where the identity of the enclosing Java object for two anonymous classes has changed! 我遇到了Java和Apache Wicket 1.5的问题,其中两个匿名类的封闭Java对象的身份已经改变了!

Within one Wicket modal window I want to create a second modal window (for getting a text string prompt) and then update the original modal window with an AJAX refresh of the model (a list of string-integer pairs). 在一个Wicket模态窗口中,我想创建第二个模态窗口(用于获取文本字符串提示),然后使用模型的AJAX刷新(字符串 - 整数对列表)更新原始模态窗口。

Basically I have two anonymous classes created in the same method but the 'this' pointer for the enclosing instance is different between one anonymous class and the other. 基本上我有两个在同一个方法中创建的匿名类,但封闭实例的'this'指针在一个匿名类和另一个匿名类之间是不同的。

This does not seem like normal expected JVM behavior to me but I haven't been able to find any specifics of how this works in the Java Specification either. 这对我来说似乎不是正常的预期JVM行为,但我也无法在Java规范中找到它的工作原理。

public class DropDownChoiceModal extends WebPage {

    public String newTermResponse;

    private void addAddTermModal() {
        addTermModal = new ModalWindow("add_term_modal");
        addTermModal.setPageCreator(new ModalWindow.PageCreator() {
            public Page createPage() {
                PropertyModel pm = new PropertyModel<String>(DropDownChoiceModal.this, "newTermResponse"); 
                System.out.println ("propModel: " + System.identityHashCode(DropDownChoiceModal.this)); 
                return new TextInputModal(addTermModal, "What is the new term you wish to add?", pm);
            }
        });

        addTermModal.setWindowClosedCallback(new WindowClosedCallback() {
        public void onClose(AjaxRequestTarget target) {

            System.out.println ("propModel: " + System.identityHashCode(DropDownChoiceModal.this)); 
            System.out.println ("newTermResponse: " + DropDownChoiceModal.this.newTermResponse);

            // If the value is set then use it 
            if (newTermAvailable()) {

                // Add the new term to the model
                model.add(new StringIntegerPair (newTermResponse, 0));

                System.out.println ("Update view: " + model.size());

                // Update the view
                target.add(wmc);
            }
            System.out.println ("No new term");
        }

        private boolean newTermAvailable() {
            return (newTermResponse != null) && !newTermResponse.isEmpty();
        }
    });

    add(addTermModal);
    }

For the TextInputModal class: 对于TextInputModal类:

public class TextInputModal extends WebPage {

public TextInputModal(final ModalWindow modal, String requestString, final IModel<?> model) {
    Form<String> form = new Form<String>("form") {
        public void onSubmit() {
            System.out.println ("Submitted: " + System.identityHashCode(((PropertyModel)model).getTarget()) + "; " + model.getObject());
        }
    };

    // Add the buttons
    form.add(new AjaxButton("ok") {
        public void onAfterSubmit(AjaxRequestTarget target, Form<?> form) {
            System.out.println ("Submitted 2: " + System.identityHashCode(((PropertyModel)model).getTarget()) + "; " + model.getObject());
            modal.close(target);
        }
    });

    // Add the form
    add(form);
}
}

The output I get: 我得到的输出:

propModel: 698650686
Submitted: 698650686; fdsfds
Submitted 2: 698650686; fdsfds
propModel: 1447892364
newTermResponse: null
No new term

Any ideas why the identity of the enclosing instance (DropDownChoiceModal.this) has changed between anonymous class 1 ( new ModalWindow.PageCreator() {} ) and anonymous class 2 ( new WindowClosedCallback() {} ) when they are created in the same method ( addAddTermModal() )? 当在同一方法中创建匿名类1(新的ModalWindow.PageCreator(){})和匿名类2(新的WindowClosedCallback(){})时,为什么封闭实例(DropDownChoiceModal.this)的身份发生了变化的任何想法(addAddTermModal())?

Thanks in advance... 提前致谢...

Nigel 奈杰尔

Big thanks to biziclop in the comments above... it looks like Wicket was clobbering the object when it was getting serialized - the contents of the "newTermResponse" String were getting lost somewhere during this serialization. 非常感谢上面评论中的biziclop ......看起来Wicket在序列化时破坏了对象 - “newTermResponse”字符串的内容在此序列化过程中丢失了。

I went back to the example of nested ModalWindow's here: 我回到了嵌套ModalWindow的示例:

http://www.wicket-library.com/wicket-examples/ajax/modal-window http://www.wicket-library.com/wicket-examples/ajax/modal-window

Using getPageReference() as shown in the example instead of PropertyModel solved my issue. 使用示例中显示的getPageReference()而不是PropertyModel解决了我的问题。

Cheers, 干杯,

Nigel 奈杰尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM