Refactor Wicket examples

This commit is contained in:
Grzegorz Piwowarek 2016-10-10 06:46:31 +02:00
parent 423dc63cdd
commit 687b7e0044
2 changed files with 14 additions and 22 deletions

View File

@ -1,10 +1,5 @@
package com.baeldung.wicket.examples.cafeaddress; package com.baeldung.wicket.examples.cafeaddress;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.WebPage;
@ -13,29 +8,29 @@ import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.PropertyModel; import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.request.mapper.parameter.PageParameters;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class CafeAddress extends WebPage { public class CafeAddress extends WebPage {
private String selectedCafe;
private static final long serialVersionUID = 1L; private Address address;
private Map<String, Address> cafeNamesAndAddresses = new HashMap<>();
String selectedCafe;
Address address;
Map<String, Address> cafeNamesAndAddresses = new HashMap<>();
public CafeAddress(final PageParameters parameters) { public CafeAddress(final PageParameters parameters) {
super(parameters); super(parameters);
initCafes(); initCafes();
ArrayList<String> cafeNames = new ArrayList<>(this.cafeNamesAndAddresses.keySet()); ArrayList<String> cafeNames = new ArrayList<>(cafeNamesAndAddresses.keySet());
this.selectedCafe = cafeNames.get(0); selectedCafe = cafeNames.get(0);
this.address = new Address(this.cafeNamesAndAddresses.get(this.selectedCafe).getAddress()); address = new Address(cafeNamesAndAddresses.get(selectedCafe).getAddress());
final Label addressLabel = new Label("address", new PropertyModel<String>(this.address, "address")); final Label addressLabel = new Label("address", new PropertyModel<String>(this.address, "address"));
addressLabel.setOutputMarkupId(true); addressLabel.setOutputMarkupId(true);
final DropDownChoice<String> cafeDropdown = new DropDownChoice<>("cafes", new PropertyModel<String>(this, "selectedCafe"), cafeNames); final DropDownChoice<String> cafeDropdown = new DropDownChoice<>("cafes", new PropertyModel<>(this, "selectedCafe"), cafeNames);
cafeDropdown.add(new AjaxFormComponentUpdatingBehavior("onchange") { cafeDropdown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
private static final long serialVersionUID = 1L;
@Override @Override
protected void onUpdate(AjaxRequestTarget target) { protected void onUpdate(AjaxRequestTarget target) {
String name = (String) cafeDropdown.getDefaultModel().getObject(); String name = (String) cafeDropdown.getDefaultModel().getObject();
@ -61,11 +56,11 @@ public class CafeAddress extends WebPage {
this.sAddress = address; this.sAddress = address;
} }
public String getAddress() { String getAddress() {
return this.sAddress; return this.sAddress;
} }
public void setAddress(String address) { void setAddress(String address) {
this.sAddress = address; this.sAddress = address;
} }
} }

View File

@ -4,9 +4,6 @@ import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.basic.Label;
public class HelloWorld extends WebPage { public class HelloWorld extends WebPage {
private static final long serialVersionUID = 1L;
public HelloWorld() { public HelloWorld() {
add(new Label("hello", "Hello World!")); add(new Label("hello", "Hello World!"));
} }