Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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:

<body> <h1>FileDropoff</h1> <wicket:child/> <p> <small>Created for DevX.com</small> </p> </body>

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:

@Test public void shouldAcceptInfoAndAdvance() { shouldAllowGuestAuth(); FormTester formTester = wicketTester.newFormTester("metaDataForm"); formTester.setValue("title", "Alpine Lakes Trail"); formTester.setValue("tags", "hike, forest, alpine lakes"); formTester.submit(); wicketTester.assertRenderedPage(FileUploadPage.class); }

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) { super(id); setModel(new CompoundPropertyModel(new UserContributedFile())); add(new RequiredTextField("title") .add(StringValidator.maximumLength(32))); add(new TextField("tags") .add(StringValidator.maximumLength(32))); }

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap