RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Java Web Development the Wicket Way : Page 3

Wicket, a lightweight, component-oriented web application framework in plain Java and XHTML, stresses separation of concerns, testability, and good object-oriented design practices. Check out Wicket's approach to common implementation scenarios.

Step 3: Markup Nesting Through Inheritance
Going back to the markup for LoginPage for a second, did you notice the "wicket:extend" tags? This is Wicket's object-oriented approach to markup nesting. In this case, the BasePage contains the following markup, which acts as a header and footer for all pages that extend it:

      <small>Created for DevX.com</small>

The "wicket:child" tag is replaced with what's contained in the "wicket:extend" tags of children (see Figure 2).

Click to enlarge

Figure 2. Wicket Markup Inheritance:
Using the OO inheritance principle, Wicket provides a powerful means of nesting pages and components within one another.

Step 4: Form Handling, Component Style
Now that users can reach the FileInfoPage, you specify that they should be able to enter some info and advance to the upload page:

  public void shouldAcceptInfoAndAdvance()
    FormTester formTester = wicketTester.newFormTester("metaDataForm");
    formTester.setValue("title", "Alpine Lakes Trail");
    formTester.setValue("tags", "hike, forest, alpine lakes");

Now you will implement a component of your own: a simple form. Creating reusable components in Wicket is as simple as writing a small Java class that derives from the framework:

public class FileInfoPage extends BasePage
  private class FileInfoForm extends Form


You will need a constructor that accepts a markup ID like SignInPanel does:

    public FileInfoForm(String id)
      setModel(new CompoundPropertyModel(new UserContributedFile()));

      add(new RequiredTextField("title")
      add(new TextField("tags")

Components have backing model objects that generally wrap instances of entity classes (note the call to setModel above). This example has one such class, UserContributedFile, the properties of which the form will allow users to edit. Different types of models facilitate different behaviors for things such as serialization and data access—in this case CompoundPropertyModel is the simplest choice.

Wicket expresses field validation rules as objects. You can see the composite pattern in use above, as you added text fields to your form and then validators to your text fields in chain invocation fashion. You can also imagine how Wicket will use the visitor pattern later to spider the fields and apply the validators on submission.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date