All I had to do to bind the properties in the PizzaModel
class to the fields in the HTML form was to add following line of code
in the PizzaForm
class:
setModel(new CompoundPropertyModel(pizzaModel));
The
wicket.model.CompoundPropertyModel
class takes care of populating the fields in the Model object
(PizzaModel in this example) with the appropriate user-entered values.
Something that wasn't clear from the Wicket examples was how to
populate the field in the Model object mapping to a drop down component
in the html file. Here is what I had to do to make it work:
I had to write a new Model class to model the options in the drop down,
for the test application, I wrote a new class called CrustType:
public class CrustType implements Serializable
{
String id;
String text;
public CrustType()
{
super();
}
public CrustType(String crustName)
{
setId(crustName);
setText(crustName);
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getText()
{
return text;
}
public void setText(String value)
{
this.text = value;
}
@Override
public String toString()
{
return getText();
}
}
wicket.markup.html.form.DropDownChoice
, which is the Wicket component used to map to an html
<select>
field (more commonly called a drop down), using the following
constructor:
public DropDownChoice(java.lang.String id,
IModel model,
IModel choices,
IChoiceRenderer renderer)
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"));
wicket.markup.html.form.ChoiceRenderer
implements the
wicket.markup.html.form.IChoiceRenderer
interface, the two parameters to its constructor represent the displayed
text and the value of the drop down options, respectively.
After I figured out how to populate the crust property of the PizzaModel, the last step was to display a confirmation page displaying all the user entered values.