Wicket label + Ajax not working

I have a simple, cryptic problem with a label and using ajax to display it.

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

}

The funny thing is that canSave is false, System.out.print works, but cansavelabel never becomes visible. What am I missing?

+5
source share
2 answers

You cannot update Label via Ajax as it is not on the render page.

cantSaveLabel.setVisible(false); 

makes the label not in HTML. You will need to surround the Label with another component (WebMarkupContainer), call setOutputMarkupId (true) and add this container to the AjaxRequestTarget set on the label.

+2
source

, - , , .

cantSaveLabel.setOutputMarkupId(true);
cantSaveLabel.setOutputMarkupPlaceholderTag(true);
+13

All Articles