Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Panel

...

borderStyle

...

solid

...

title

...

Table of contents

...

Table of Contents
minLevel2

PanelDate: 27 January 2006<p>
Author: David Leangenhttp://www.leangen.net

Overview

This article shows how to display read-only information from a database. The text is based mostly on information provided on the mailing list by Igor Vaynberg.

Use Case

We want to search a CRM database for contacts. Contacts are provided based on search criteria. In this application, contact information is read-only.

Wicket Solution

The page that we create will contain a repeater of some kind (see also repeater examples). Since the results will be different for each page view, it is not sufficient to initialize the repeater from within the page constructor. Since in Wicket a page is only constructed once, in this case the data would never change.

Rather, what we need is a repeater that refreshes itself each time the page renders. This is exactly what ListView is for!

Panel

When working with a database, it is often a good choice to
use IDataProvider/Dataview repeaters. Such repeaters are
specifically tailored to work with data retrieved from a
database.

However, for the sake of simplicity, in this example we use
the ListView, which is the "simplest" approach.

Panel

Upon each page render, ListView reads a List (which just happens to be its model object) and renders its list items based on that List.

The following is a very basic example of how to go about this.

Code Block

  Note: this code has not been tested. It is only intended to show the concepts.
  
  SearchPage extends WebPage
  {
      String criteria;
      List results;
  
      //getters and setters
  
      public SearchPage()
      {
          ListView view = 
              new ListView ( "list", new PropertyModel(this, "results"))
              {
                   populateItem( ListItem item )
                   {
                       String result = item.getModelObject();
                       add( new Label( "item", result ) );
                   }
               }
          );
  
          Form form = new SearchForm(....);
          form.add( new TextField( "id", new PropertyModel(this, "criteria" ) );
          form.add
          ( 
              new Button()
              {
                  onsubmit()
                  {
                      results=getResultsFromCriteria(criteria);
                  }
              }
          );
      }
  }

The principle is this: every time the page renders, the ListView will populate itself with the inserted List, that also happens to be a page property. Every time the form is submitted (i.e. there is a new request for a search), the List is updated based on the results obtained using the search string.

In other words, all the objects are created once and only once in the Page constructor, but the data is retrieved dynamically upon each Page render.

Panel

For additional discussion on ListView see:
http://thread.gmane.org/gmane.comp.java.wicket.user/7763

...

panel

...

ListView vs DataProvider

DataProvider is made for working with large data sets where it is too expensive to load the enire dataset into memory at once. DataProvider allows you to only retrieve the window of the dataset you are going to display.

...