开发者

How do I implement back button support on a form in Wicket?

开发者 https://www.devze.com 2023-02-23 08:45 出处:网络
I have a search page with a tabbed panel, with a form within each tab (see code below), that when submitted searches the database for hits. However, back button support on this form is not working (us

I have a search page with a tabbed panel, with a form within each tab (see code below), that when submitted searches the database for hits. However, back button support on this form is not working (user supplied text in textField is lost). How do I implement back button support for this form? I have been playing around with issuing setVersioned(true) on the page, panel, form and textField in combination with modelChanging() and modelChanged() on the textField and the form, but no开发者_StackOverflow中文版ne of those have worked. I have also tried overriding method newLink from TabbedPanel as detailed here, but that has not worked either. I'm at a loss...

Any help would be greatly appreciated.

public class TextSearchPanel extends Panel {
    CompoundsDataTablePanel hitsPanel;

    @SpringBean
    ICompoundDAO            compoundDAO;

    public TextSearchPanel(final String id, final FeedbackPanelWrapper feedbackPanel) {
        super(id);
        setOutputMarkupId(true);

        // Text Search Form
        Form<TextSearchPanel> textform = new Form<TextSearchPanel>("textForm");

        // Add textField
        TextField<String> textField = new TextField<String>("query", new Model<String>());
        textField.setRequired(true);
        textField.add(new StringValidator() {
            @Override
            protected void onValidate(final IValidatable<String> validatable) {
                String query = Name.replaceApostropheVariantsWithApostrophe(validatable.getValue());
                try {
                    // Ascertain parsing query does not throw an exception
                    compoundDAO.parseQuery(query);
                }
                catch (ParseException e) {
                    // Otherwise: Display the error back to the user
                    validatable.error(new ValidationError().setMessage(e.getMessage()));
                }
            }
        });
        textform.add(textField);

        // Add submit button
        textform.add(new IndicatingAjaxButton("ajaxsubmit") {
            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                target.addComponent(feedbackPanel);
                // Show hits panel
                hitsPanel.setVisible(true);
                target.addComponent(TextSearchPanel.this);
            }

            @Override
            protected void onError(final AjaxRequestTarget target, final Form<?> form) {
                target.addComponent(feedbackPanel);
                // Hide hits panel
                hitsPanel.setVisible(false);
                target.addComponent(TextSearchPanel.this);
            }
        });
        add(textform);

        // Text Hits Panel
        hitsPanel = new CompoundsDataTablePanel("hits", new TextHitsProvider(textField.getModel()));
        hitsPanel.setVisible(false);
        add(hitsPanel);
    }
}

Page class:

public class SearchPage extends BasePage {
public SearchPage() {
    super("Search");
    setOutputMarkupId(true);

    List<ITab> tabs = new ArrayList<ITab>();
    tabs.add(new AbstractTab(new Model<String>("Spectrum search")) {
        @Override
        public Panel getPanel(final String panelId) {
            return new SpectrumSearchPanel(panelId, getFeedbackPanelWrapper());
        }
    });
    tabs.add(new AbstractTab(new Model<String>("Mass search")) {
        @Override
        public Panel getPanel(final String panelId) {
            return new MassSearchPanel(panelId, getFeedbackPanelWrapper());
        }
    });
    tabs.add(new AbstractTab(new Model<String>("Formula search")) {
        @Override
        public Panel getPanel(final String panelId) {
            return new FormulaSearchPanel(panelId, getFeedbackPanelWrapper());
        }
    });
    tabs.add(new AbstractTab(new Model<String>("Text search")) {
        @Override
        public Panel getPanel(final String panelId) {
            return new TextSearchPanel(panelId, getFeedbackPanelWrapper());
        }
    });

    // Add tabs to page to switch between search modes
    add(new TabbedPanel("tabs", tabs));
}
 }


I haven't had chance to try your code out, but could it be a problem with the use of ajax. The back button is not fully supported with ajax in wicket yet: https://issues.apache.org/jira/browse/WICKET-271. You may want to check the versions of the pages you are viewing when pressing the back button when compared with viewing it the first time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号