阿贾克斯不在我的检票口工作

时间:2013-03-19 17:33:28

标签: java wicket wicket-6

我想在wicket 6.5中测试一些AJAX DropDown(试过wicket 6.6并遇到同样的问题)。

我使用quickstart wicket页面创建了我的wicket测试项目 - http://wicket.apache.org/start/quickstart.html

  

mvn archetype:generate -DarchetypeGroupId = org.apache.wicket -DarchetypeArtifactId = wicket-archetype-quickstart -DarchetypeVersion = 6.6.0 -DgroupId = net.betlista -DartifactId = tests.wicket-6.6 -DarchetypeRepository = {{3} } -DinteractiveMode = false

我将HomaPage更改为生成的LoadableDropDownTestPage课程getHomePage()中的WicketApplication

LoadableDropDownTestPage的Java代码是:

package net.betlista;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxIndicatorAware;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

public class LoadableDropDownTestPage extends WebPage implements IAjaxIndicatorAware {

    public LoadableDropDownTestPage() {
        addComponents();
    }

    private void addComponents() {
        add(new FeedbackPanel("feedback"));

        FormObject formObject = new FormObject();
        Form<FormObject> form = new Form<FormObject>("loadableDropDownTestForm", Model.of(formObject));
        form.setOutputMarkupId(true);
        form.setOutputMarkupPlaceholderTag(true);

        final DropDownChoice<String> countryDD = new LoadableDropDown("countryDD", new PropertyModel<String>(formObject, "country"));
        countryDD.setChoices(new CountryLoadableModel());
        countryDD.setOutputMarkupId(true);
        countryDD.setRequired(true);
        countryDD.setOutputMarkupPlaceholderTag(true);

        final DropDownChoice<String> cityDD = new LoadableDropDown("cityDD", new PropertyModel<String>(formObject, "city"));
        cityDD.setChoices(new CityLoadableModel());
        cityDD.setOutputMarkupId(true);
        cityDD.setRequired(true);
        cityDD.setOutputMarkupPlaceholderTag(true);

        countryDD.add(new OnChangeAjaxBehavior() {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println("country DD changed");
                target.add(cityDD);
            }
        });

        form.add(countryDD);
        form.add(cityDD);

        form.add(new AjaxButton("ab") {} );

        add(form);
    }

    static class LoadableDropDown extends DropDownChoice<String> {

        public LoadableDropDown(String id, IModel<String> model) {
            super(id);
            setModel(model);
        }

    }

    static class FormObject implements Serializable {
        String country;
        String city;
    }

    class CountryLoadableModel extends LoadableDetachableModel<List<String>> {

        @Override
        protected List<String> load() {
            System.out.println("loading CountryLoadableModel");
            List<String> result = Arrays.asList(new String[] { "CR", "SR" } );
            return result;
        }

    }

    class CityLoadableModel extends LoadableDetachableModel<List<String>> {

        List<String> choices = new LinkedList<String>();

        @Override
        protected List<String> load() {
            System.out.println("loading CityLoadableModel");
            if (choices.isEmpty()) {
                choices.add("1");
            } else {
                int size = choices.size();
                choices.add(Integer.toString(size+1));
            }

            return choices;
        }

    }

    @Override
    public String getAjaxIndicatorMarkupId() {
        return "ajaxIndicator";
    }

}

和页面的HTML是:

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">

    <div wicket:id="feedback"></div>

    <form wicket:id="loadableDropDownTestForm">
        Countries: <select wicket:id="countryDD"></select><br>
        Cities: <select wicket:id="cityDD"></select><br>
        <!-- input type="submit"-->
        <button wicket:id="ab"></button>
    </form>


</html>

我的第一个问题是,我在页面上看不到AJAX调试链接。但我认为虽然OnChangeAjaxBehavior我应该看到它。

接下来的问题是,当我在国家/地区改变价值DropDown时没有任何反应,我不知道我做错了什么。

在我的代码中,您可以看到,我也尝试过使用AjaxButton,但这也不起作用。

编辑:

日志的一部分(它在DEV模式下运行)

********************************************************************
*** WARNING: Wicket is running in DEVELOPMENT mode.              ***
***                               ^^^^^^^^^^^                    ***
*** Do NOT deploy to your live server(s) without changing this.  ***
*** See Application#getConfigurationType() for more information. ***
********************************************************************

2 个答案:

答案 0 :(得分:5)

我认为问题在于您没有在HTML中使用<body>标记。这打破了HTML解析器,这就是为什么你看不到AJAX调试窗口(以及任何其他javascript行为)的原因。

有一个很好的例子,你在Wicket例子中尝试做什么,看看它: http://www.wicket-library.com/wicket-examples/ajax/choice

答案 1 :(得分:0)

请尝试使用AjaxFormComponentUpdatingBehaviour。以下是我的代码中的一部分 -

DropDownChoice<String> actionSelect = new DropDownChoice<String>("myDropdown", updateActions, choiceRenderer);
actionSelect.add(new AjaxFormComponentUpdatingBehavior("onchange") {

  @Override
  protected void onUpdate(AjaxRequestTarget target) {
    ...
  }
});