The corresponding java code for the class extending wicket.markup.html.WebPage
(import and package name excluded):
public class WicketTestPage extends WebPage
{
PizzaForm pizzaForm = new PizzaForm("pizzaForm");
public WicketTestPage()
{
super();
add(pizzaForm);
}
}
The wicket.markup.html.form.Form
class is meant to be
extended,
since it provides an onSubmit()
method that is
executed when the form is submitted. I wrote the PizzaForm class to map
the form in the html page, here are the relevant segments of the source
for the PizzaForm class:
public class PizzaForm extends Form
{
private DropDownChoice crustDropDown;
private CheckBox pepperoniCheckBox = new CheckBox("pepperoni");
private CheckBox sausageCheckBox = new CheckBox("sausage");
private CheckBox onionsCheckBox = new CheckBox("onions");
private CheckBox greenPeppersCheckBox = new CheckBox("greenPeppers");
private TextField commentsTextField = new TextField("comments");
public PizzaForm(String id)
{
super(id);
PizzaModel pizzaModel = new PizzaModel();
setModel(new CompoundPropertyModel(pizzaModel));
crustDropDown = new DropDownChoice(
"crust",new PropertyModel(pizzaModel, "crust"), Arrays
.asList(new CrustType[]
{ new CrustType("Thin & Crispy"),
new CrustType("Hand Tossed"),
new CrustType("Pan Pizza") }),
new ChoiceRenderer("text", "id"));
add(crustDropDown);
add(pepperoniCheckBox);
add(sausageCheckBox);
add(onionsCheckBox);
add(greenPeppersCheckBox);
add(commentsTextField);
}
}
Basically what I did here was to add all of the necessary Wicket components to the form. The code for the DropDown component might look a little confusing right now if you are not familiar with Wicket, don't worry about it for now, it will be explained later.
Finally, I wrote a PizzaModel
class to hold the
user-entered values on the form:
public class PizzaModel implements Serializable
{
private String crust;
private boolean pepperoni;
private boolean sausage;
private boolean onions;
private boolean greenPeppers;
private String comments;
public String getComments()
{
return comments;
}
public void setComments(String comments)
{
this.comments = comments;
}
public String getCrust()
{
return crust;
}
public void setCrust(String crust)
{
this.crust = crust;
}
public boolean getGreenPeppers()
{
return greenPeppers;
}
public void setGreenPeppers(boolean greenPeppers)
{
this.greenPeppers = greenPeppers;
}
public boolean getOnions()
{
return onions;
}
public void setOnions(boolean onions)
{
this.onions = onions;
}
public boolean getPepperoni()
{
return pepperoni;
}
public void setPepperoni(boolean pepperoni)
{
this.pepperoni = pepperoni;
}
public boolean getSausage()
{
return sausage;
}
public void setSausage(boolean sausage)
{
this.sausage = sausage;
}
}
PizzaModel
class match the <wicket:id>
attributes in the HTML file.