Wicket Reference Manual Update
I've begun work on a (long overdue) reference manual for Wicket, and wanted to get some initial feedback. Is this the kind of manual everybody is longing for?
PS. This is a direct paste from the generated DocBook version, so the markup is not 'optimized' for display within this JRoller template.
Abstract
This chapter shows how you can process input from users using forms, inputs and
other form related components. We show how to add validation to your fields and
give your users feedback when they entered wrong input.
Many webapplications need some sort of user input. This is an integral part
of dynamic web applications. Many web applications don't take localization and
internationalization into account. Often you have to write a lot of code for parsing
request parameters, validating and converting them into the actual types your
POJOs use.
With Wicket things will be different: you won't have to write lots and lots of
scaffolding: Wicket takes care of a lot of things: updating your POJOs,
converting the user input taking localization into account, validating the input,
displaying internationalized messages when the input isn't valid, etc.
Most, if not all, Wicket form components will be discussed in the following sections. The
goal is not to be complete and show all possible uses of each component, but to give an
initial outline of how such a component can be used. For more information, please look in
the JavaDoc of the components.
The
Form
component is a container for your form components. In the markup a form looks
like this:
To implement a form, subclass the
Form
, add FormComponents (such as CheckBoxes, ListChoices or TextFields) to the form
and override the
onSubmit
method. You can nest multiple buttons if you want to vary submit behaviour.
However, it is not necesary to use Wicket's button class, just putting e.g.
suffices.
public class MyForm extends Form { public MyForm(String id, IModel model, IFeedback feedback) { // add (form) components } public void onSubmit() { // handle the submission of the form. } public void onError() { // do something special when an error occurs, // instead of displaying messages. } }
Wicket will call onSubmit
when the user input is
valid. When one or more child components of the form fails to validate,
Wicket will call onError
. Wicket also supports
multiple buttons, which are discussed later.
A TextField
receives user input in possibly its
most basic form: a single line of text. The markup for a
TextField
component must be a
<input type="text" ... />
tag, otherwise Wicket
will throw an exception.
The markup for a textfield looks as follows:
In the Java code you can use several constructors for the component, depending
on the type of model you want to bind to the textfield.
public MyForm(String id, IModel model, IFeedback feedback) { super(id, model, feedback); add(new TextField("textField", new PropertyModel(model, "name"))); ... }
It is possible to use the true types of the fields in the textfield component.
Say that you need an integer value. Instead of performing the dataconversion
yourself, you specify for Wicket that you require an integer value by adding
an extra parameter to the TextField
constructor: the
type of the property.
new TextField("integerField", new PropertyModel(model, "myInt"), Integer.class)
More information on this subject is provided in the validation section later on.
The PasswordTextField
component is used for users to enter
their passwords. The field can be used in both login forms and registration forms.
In login forms it is usually required that the password is always cleared when the
page is rendered. When editing account information, this is not the case, so the
password field can be told not to clear the field when rendered. The markup looks
like this (the type="password"
is required!):
The corresponding Java declaration looks like this:
PasswordTextField pwd = new PasswordTextField("pwd", new PropertyModel(model, "myPwd"));
When you want to use the field in a registration form, or some other form where the
password should not be reset on each render, you have to set the reset password flag
to false
:
pwd.setResetPassword(false);
The default is to clear the field each time it is rendered.