我想在 wicket 6.5 中测试一些 AJAX DropDown(尝试了 wicket 6.6 并遇到了同样的问题)。
我使用快速启动检票口页面创建了检票口测试项目 - 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= https://repository.apache .org/ -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
我应该看到它。
下一个问题是,当我更改国家下拉菜单中的值时,什么也没有发生,我不知道我做错了什么。
在我的代码中,您可以看到,我也尝试使用 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. ***
********************************************************************