TOTD #86 explained how to get started with deploying a Apache Wicket application on GlassFish. This Tip Of The Day (TOTD) will show how to add pagination to your Wicket application.

The blog entry "JPA/Hibernate and Wicket Repeating Views with Netbeans" Part 1 and 2 explain in detail how to create a CRUD application using NetBeans, JPA, Hibernate and Wicket. This blog uses the data created in TOTD #38 for the database table.
  1. After creating the JPA Controller, adding the IDataProvider and DataView implementations and hooking together, the application is available at "http://localhost:8080/helloworld" and looks like:

    As noticed in the output, all the states are listed in one page. The HTML markup looks like:

            <title>Wicket Quickstart Archetype Homepage</title>
            <strong>Wicket Quickstart Archetype Homepage</strong>
            <span wicket:id="message">message will be here</span>
                <tr wicket:id="rows">
                    <td><span wicket:id="id">ID</span></td>
                    <td><span wicket:id="abbrev">Abbreviation</span></td>
                    <td><span wicket:id="name">Name</span></td>


    The backing POJO looks like:

    package org.glassfish.samples;

    import java.util.Iterator;
    import org.apache.wicket.PageParameters;
    import org.apache.wicket.markup.html.basic.Label;
    import org.apache.wicket.markup.html.WebPage;
    import org.apache.wicket.markup.repeater.Item;
    import org.apache.wicket.model.CompoundPropertyModel;
    import org.apache.wicket.model.IModel;
    import org.apache.wicket.model.LoadableDetachableModel;
    import org.glassfish.samples.controller.StatesJpaController;
    import org.glassfish.samples.model.States;

     * Homepage
    public class HomePage extends WebPage {

        private static final long serialVersionUID = 1L;

        // TODO Add any page properties or variables here

         * Constructor that is invoked when page is invoked without a session.
         * @param parameters
         *            Page parameters
        public HomePage(final PageParameters parameters) {

            // Add the simplest type of label
            add(new Label("message", "If you see this message wicket is properly configured and running"));

            // TODO Add your page's components here

                    // create a Data Provider
            IDataProvider statesProvider = new IDataProvider() {
                public Iterator iterator(int first, int count) {
                    StatesJpaController sc = new StatesJpaController();
                    return sc.findStatesEntities(count, first).iterator();

                public int size() {
                    StatesJpaController sc = new StatesJpaController();
                    return sc.getStatesCount();

                public IModel model(final Object object) {
                    return new LoadableDetachableModel() {
                        protected States load() {
                            return (States)object;

                public void detach() {

            // TODO Add your page's components here

            DataView dataView = new DataView("rows", statesProvider) {

                protected void populateItem(Item item) {
                    States state = (States)item.getModelObject();
                    item.setModel(new CompoundPropertyModel(state));
                    item.add(new Label("id"));
                    item.add(new Label("abbrev"));
                    item.add(new Label("name"));


    and "persistence.xml" looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
      <persistence-unit name="helloworld" transaction-type="RESOURCE_LOCAL">
          <property name="hibernate.connection.username" value="app"/>
          <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
          <property name="hibernate.connection.password" value="app"/>
          <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/sample"/>
  2. Lets add pagination to this simple sample.
So just adding a pagination controls placeholder in the HTML and a corresponding configuration in DataView (in the backing POJO) did the trick for us.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all the tips is available here.

Technorati: wicket glassfish pagination