I wanted to test some AJAX DropDown in wicket 6.5 (tried wicket 6.6 and had same problems).
I created my wicket test project using quickstart wicket page - 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
I changed HomaPage
to my LoadableDropDownTestPage
in getHomePage()
of generated WicketApplication
class.
Java code for LoadableDropDownTestPage is:
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";
}
}
and HTML for page is:
<!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>
My very first problem is, that I couldn't see AJAX debug link on my page. But I think that while there is OnChangeAjaxBehavior
I should see it.
Next problem is that when I change value in country DropDown nothing happens and I have no idea what am I doing wrong.
In my code you can see, that I tried also with AjaxButton, but that didn't work too.
edit:
part of the log (it's running in DEV mode)
********************************************************************
*** WARNING: Wicket is running in DEVELOPMENT mode. ***
*** ^^^^^^^^^^^ ***
*** Do NOT deploy to your live server(s) without changing this. ***
*** See Application#getConfigurationType() for more information. ***
********************************************************************
I think that the problem is that you are not using the <body>
tag in your HTML. This breaks the HTML parsers and this is why you cannot see the AJAX debug window (and any other javascript behaviour).
There is a nice example of what you are trying to do at Wicket Examples, have a look at it: http://www.wicket-library.com/wicket-examples/ajax/choice