Wicket AjaxButton不会更新FeedbackPanel

时间:2018-03-23 12:06:09

标签: java wicket

如果单击了AjaxButton,我正在尝试向Wicket中的FeedbackPanel发送成功消息。然而,我收到以下错误消息:

  

Wicket.Ajax.Call.processComponent:尝试执行标记更新时找不到具有id [[065DFC0DD1DC51103CC5FFEE61F2AE12]]的组件。

我已经知道,在使用AjaxButton并在onSubmit中设置目标之前,我必须在Java代码中输出markupID。我也设置setOutputMarkupPlaceholderTag一次,但它没有改变任何东西 我还创建了一个小测试页面,成功将成功消息发送到FeedbackPanel。

因此我开始调试项目,发现从DefaultMarkupIdGenerator.generateMarkupId返回的markupID确实与HTML中显示的不同。此外,所有FeedbackPanels在ajax调用期间都会收到此markupID。

  

feedbackPanelWartung12 vs. 065DFC0DD1DC51103CC5FFEE61F2AE12

为了进一步复杂化,我的FeedbackPanel位于已包含FeedbackPanel(继承自其超类)的页面中的Panel中包含的RepeatingView中。结构如下:

Java代码

我的页面

public MyPage extends MySuperPage{

    //Constructors were left out

    //this method is called in the onInitialize of some
    //super class.
    @Override
    protected Component newContent() {
        setFeedbackPanelFilter();
        return new MyContainerPanel();
    }

    //this FeedbackPanel shall not recieve messages from child containers
    private void setFeedbackPanelFilter() {
        ((FeedbackPanel) this.get("feedback"))
            .setFilter( new ContainerFeedbackMessageFilter(this) {
                @Override
                public boolean accept(FeedbackMessage message) {
                    return !super.accept(message);
                }
        });
    }
}

MyContainerPanel

public MyContainerPanel extends Panel {

    //Constructors were left out

    @Override
    protected void onInitialize() {
        super.onInitialize();
        RepeatingView repeater = new RepeatingView("repeater");
        add(repeater);
        for (Object o : objects) {
            String id = repeater.newChildId();
            repeater.add(new MyPanel(id,o));
        }
    }
}

MyPanel

public MyPanel extends Panel {    
    private Object o;
    private CheckBox chkBox;
    private DateTimeField startDate, endDate;
    private TextField<String> message;
    private Form<Object> form;
    private AjaxButton button;
    private Label name;
    private FeedbackPanel feedbackPanel;

    //Constructors were left out

    @Override
    protected void onInitialize() {
        super.onInitialize();

        initFields();
        addFields();
        addAjaxButton();
        addAjaxFormComponentUpdatingBehavior();

        if (!o.isMaintenance())
            disableFields();
        }

    private void addAjaxButton() {
        button = new AjaxButton("submit") {
            @Override
            public void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                //do something
                feedbackPanel.success("Speichern erfolgreich");
                target.addChildren(AppWartungPanel.this.getPage(), FeedbackPanel.class);
            }
        };
        form.add(button);
    }

    private void addAjaxFormComponentUpdatingBehavior() {
        chkBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                switchVisibility();
                target.add(startDate);
                target.add(endDate);
                target.add(message);
                target.add(feedbackPanel);
            }
        });
    }

    private void switchVisibility() {
        if (o.isMaintenance())
            enableFields();
        else
            disableFields();
        }

    private void addFields() {
        add(form);
        form.add(feedbackPanel);
        form.add(chkBox);
        form.add(startDate);
        form.add(endDate);
        form.add(message);
        form.add(name);
    }

    private void initFields() {
        form = new Form<WartungDAO>("form", AppWartungPanel.this.getModel());
        chkBox = new CheckBox("wartung", new PropertyModel<Boolean>(o, "wartung"));
        chkBox.setOutputMarkupId(true);
        startDate = new DateTimeField("startDate", new PropertyModel<Date>(o, "startDate"));
        startDate.setOutputMarkupId(true);
        startDate.setRequired(true);
        endDate = new DateTimeField("endDate", new PropertyModel<Date>(o, "endDate"));
        endDate.setOutputMarkupId(true);
        endDate.setRequired(true);
        message = new TextField<String>("message", new PropertyModel<String>(o, "message"));
        message.setOutputMarkupId(true);
        message.setRequired(true);
        name = new Label("name", new PropertyModel<String>(o, "portal"));
        feedbackPanel = new FeedbackPanel("feedbackPanelWartung");
        feedbackPanel.setOutputMarkupId(true);
        feedbackPanel.setFilter(new ContainerFeedbackMessageFilter(this));
    }

    private void disableFields() {
        startDate.setEnabled(false);
        endDate.setEnabled(false);
        message.setEnabled(false);
    }

    private void enableFields() {
        startDate.setEnabled(true);
        endDate.setEnabled(true);
        message.setEnabled(true);
    }
}

HTML标记

我的页面

<!DOCTYPE html
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <body class="BG_MAIN">
           <div class="grid_16 BG1_GLOBAL">
               <div style="margin:10px;">
                   <div class="grid_3 alpha">
                    <wicket:container wicket:id="menu"></wicket:container>
                </div>
            <div class="grid_12 omega">
        <div class="INHALT">
                <h1 wicket:id="appHeader" style="margin-top: 0;"></h1>
                <div wicket:id="wartung" />
                <div wicket:id="feedback"></div>
                <wicket:container wicket:id="content"></wicket:container>
            </div>
        </div>
    </body>
</html>

MyContainerPanel

<html xmlns:wicket="http://wicket.apache.org">
    <wicket:panel>
        <div wicket:id="repeater">
        </div>
    </wicket:panel>
</html>

MyPanel

<html xmlns:wicket="http://wicket.apache.org">
    <wicket:panel>
        <div style="margin-left: 25px; margin-top: 25px;">
            <form wicket:id="form">
                <h2 wicket:id="name"></h2>
                <div wicket:id="feedbackPanelWartung"></div>
                <table style="border: none;">
                    <tbody>
                        <tr>
                            <td style="border: none;">Wartung aktiv?</td>
                            <td style="border: none;">
                                <input wicket:id="wartung" type="checkbox" />
                            </td>
                        </tr>
                        <tr>
                            <td style="border: none;">Nachricht zum Anzeigen:</td>
                            <td style="border: none;">
                                <input wicket:id="message" type="text" size="50" />
                            </td>
                        </tr>
                        <tr>
                            <td style="border: none;">Startdatum</td>
                            <td style="border: none;">
                                <div wicket:id="startDate"></div>
                            </td>
                        </tr>
                        <tr>
                            <td style="border: none;">Enddatum</td>
                            <td style="border: none;">
                                <div wicket:id="endDate"></div>
                            </td>
                        </tr>
                        <tr>
                            <td style="border: none;"></td>
                            <td style="border: none;">
                                <input wicket:id="submit" type="submit" value="Speichern" style="float: right;" />
                            </td>
                        </tr>
                    </tbody> 
                </table>
            </form>
        </div>
    </wicket:panel>
</html>

我只能猜测错误是由于页面的复杂性造成的。

我还禁用AjaxFormComponentUpdatingBehavior以检查它是否与AjaxButton混淆 - 它没有。
我还使用普通Button实现了所有内容,以检查ContainerFeedbackMessageFilter和FeedbackPanels是否有效 - 他们确实如此。

AjaxButton对Page并不重要。但我真的很想理解,所以如果有人有另一个好主意试试......

1 个答案:

答案 0 :(得分:0)

FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanelWartung") {
    @Override
    public String getMarkupId() {
        return "feedbackPanelWartung." + someUniqueValuePassedToThePanel;
    }
};
feedbackPanel.setOutputMarkupId(true);

幸运的是,我在面板的一个modelObject中有一个独特的值。然后我覆盖了Component.getMarkupId()方法,只需设置我的Id 工作就像一个魅力。

我还发现,方法feedbackPanel.setOutputMarkupId(true);与我的错误信息无关 换句话说:即使我尝试为我的组件设置标记ID,它也从未在第一个位置设置,因此ajax组件理所当然地永远无法找到目标。