Wicket标签+ Ajax无法正常工作

fre*_*red 5 java ajax label wicket

我有一个简单,神秘的标签问题,并使用ajax来显示它.

public class ChecklistTemplateForm extends Form{
    private static final long serialVersionUID = 1L;
    private Label cantSaveLabel;
    public ChecklistTemplateForm(String id) {
        super(id);
        cantSaveLabel = new Label("cantSaveLabel", "Name is not unique, enter another name and try saving again.");
        cantSaveLabel.setVisible(false);
        cantSaveLabel.setOutputMarkupId(true);
        add(cantSaveLabel);
        add(new AjaxButton("saveButton") {
            private static final long serialVersionUID = 1L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(cantSaveLabel);
                //here i do some stuff to decide if canSave is true or false
                if (canSave){
                    setResponsePage(AdminCheckListPage.class);
                }
                else if (!canSave){
                    cantSaveLabel.setVisible(true);
                    System.out.println(canSave);
                }
            }
        }); 
    }

}
Run Code Online (Sandbox Code Playgroud)

有趣的是,canSave是假的,System.out.print可以工作,但cansavelabel永远不会变得可见.我错过了什么?

Til*_*ill 13

你必须告诉wicket它应该使用占位符标记,因为它无法更新标记中不存在的组件.

cantSaveLabel.setOutputMarkupId(true);
cantSaveLabel.setOutputMarkupPlaceholderTag(true);
Run Code Online (Sandbox Code Playgroud)

  • `setOutputMarkupPlaceholderTag()`调用`setOutputMarkupId()`,所以你不需要同时调用它们. (2认同)

ber*_*ert 2

您无法通过 Ajax 更新标签,因为它不在渲染的页面中。这

cantSaveLabel.setVisible(false); 
Run Code Online (Sandbox Code Playgroud)

使标签不在 HTML 中。您需要用另一个组件 (WebMarkupContainer) 包围 Label,对此调用 setOutputMarkupId(true) 并将该容器添加到 AjaxRequestTarget 中而不是标签。