Products Docs & Support Community

Introduction to the Wicket Web Framework

This document takes you through the basics of creating reusable components and assembling them into a web application. Each component consists of a Java class and an HTML file. The framework that allows you to develop your application in this way is called Wicket. In addition to its component-based approach, a distinguishing feature of Wicket is the absence of XML configuration files. Instead of an XML configuration file, you use a Java class for application-wide settings, such as the identification of a home page.

Each widget in your web application is created in a Java class and rendered in an HTML page. The Java class and HTML page must have the same name and exist in the same source structure. They are linked to each other via a Wicket identifier. You will be shown how the IDE supports the development of component based applications so that you can quickly and efficiently create reusable components that can give your web application a consistent look and feel without very much work on your part.

The following topics are covered below:

This tutorial can be completed in 20 minutes.

For more information on Wicket. For details on support for Wicket in NetBeans IDE. If you are familiar with Wicket, you are welcome to contribute code to the Wicket Support module for NetBeans IDE.

Note: The Wicket Support module for NetBeans, which you will use extensively in this tutorial, is incomplete. However, more than enough functionality is provided for you to get started developing Wicket applications. Where there are gaps in the functionality, they will be mentioned in the instructions below.

Setting Up the Environment

Before you start writing your component based application, you have to make sure you have all of the necessary software and that your project is set up correctly. Once you have installed the Wicket Support module for NetBeans IDE, you will have a wizard that sets up all the basic files needed for a Wicket application.

Installing the Software

Before you begin, you need to install the following software on your computer:

  • Wicket 1.2 or above (download)
  • Wicket Support module for NetBeans IDE (click here to download from Plugin Portal)
  • NetBeans IDE 5.0 or above. (This tutorial was written and tested under NetBeans IDE 5.5.)
  • Java Standard Development Kit (JDK™) version 1.4.2 (download) or 5.0 (download)

Creating the Source Structure of a Component Based Application

The source structure of our application must include the Wicket JAR files, the registration of the Wicket servlet in the web.xml file, as well as some standard artifacts such as the application class and a home page. Since we are using an IDE, we shouldn't need to create all these files by hand. Instead, we have a wizard to do the work for us. Specifically, the final panel of the Web Application wizard will be very useful in the context of our component based application.

  1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application. Click Next.
  2. In the Name and Location panel, type MyFirstWicketApp in Project Name. Change the Project Location to any directory on your computer.

  3. Leave all the other settings unchanged. Or, if you like, you can change them. Wicket supports J2EE 1.4 as well as Java EE 5. A Wicket application can be deployed to any server. Click Next.

  4. In the Frameworks panel, choose Wicket. You will be prompted to browse to the top level of your Wicket download:

    For example, browse to "wicket-1.2.2", if that is the top level of your Wicket download:

  5. When the top level is correctly selected, the following panel appears:

  6. Leave all the defaults unchanged. The fields in the panel above provide the following:

    • Wicket URL Pattern. Adds the relative URL pattern to the web.xml file.
    • Wicket Application Class. Specifies name of the class where the application-wide settings, such as the home page, are set.
    • Wicket Home Page. Specifies the name of the home page, which will consist of a file called xxx.java and xxx.html.
    • Dummy POJO. POJOs are common artifacts in Wicket. Here, you can let the IDE generate a simple, empty, JavaBeans file for you.
    • Main Package. The Java package in which all the generated artifacts will be put by the IDE.
    • Add Example Formatting. Adds a reusable component, consisting of a Java class and HTML file, as well as a CSS stylesheet, for defining a common header for all your web pages.

    Click Finish.

The IDE creates the MyFirstWicketApp project. The project contains all of your sources and project metadata, such as the project's Ant build script. The project opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1):

In the next section, we will explore each of these files in detail.

Examining the Source Structure of a Component Based Application

The IDE's Web Application wizard created a lot of files for us. Here, we look at the files and see how they relate to each other within the context of component based development.

  1. Let's begin by looking in the web.xml file, which is the general deployment descriptor common to all web applications that comply with the Servlet specification. Expand the WEB-INF folder or the Configuration Files folder, open the file in raw XML view, and notice the definition of the servlet:

    <servlet>
        <servlet-name>WicketApplication</servlet-name>
        <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.myapp.wicket.WicketApplication</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

    Notice the value of the application class name. In the next step, we will open the application class file and inspect its content.

  2. Open the com.myapp.wicket package in the Source Packages folder and then open the WicketApplication.java file. It looks like this:

    package com.myapp.wicket;
    import wicket.protocol.http.WebApplication;
    public class WicketApplication extends WebApplication {
        public WicketApplication() {
        }
        public Class getHomePage() {
            return Home.class;
        }
    }

    This is the Java file that provides application-wide settings, comparable to struts-config.xml in the Struts framework and faces-config.xml in the JSF framework. Notice the definition of the getHomePage() method. This method is the minimum requirement of the application-wide class. It specifies the first page (the home page) that will be displayed when you deploy the application. Notice that Home.class is returned. In the next step, we will open the Home.java file and inspect its content.

  3. Open Home.java. It looks like this:

    package com.myapp.wicket;
    public class Home extends WicketExamplePage {
        public Home() {
        }
    }

    The file is empty. Rendering of Wicket widgets created in this file is done in a file with the same name in the same source structure, which can only be Home.html, which looks as follows, currently:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <title></title>
            <link rel='stylesheet' type='text/css' href='style.css'/>
        </head>
        <body>
            <span wicket:id='mainNavigation'/>
        </body>
    </html>

    Notice that in Home.java we are extending WicketExamplePage. In Home.html we have a wicket:id attribute, which tells us that this is a placeholder for something created somewhere by a Java file. Also, we have a reference to the CSS stylesheet that the IDE generated for us. You can find it in the Web Pages folder, in the Projects window. In the next step, we will open WicketExamplePage and examine its content.

  4. Open WicketExamplePage.java. This is what it looks like:

    package com.myapp.wicket;
    import wicket.markup.html.WebPage;
    import wicket.model.IModel;
    import wicket.util.string.Strings;
    public class WicketExamplePage extends WebPage {
        public WicketExamplePage() {
            this(null);
        }
        public WicketExamplePage(IModel model) {
            super(model);
            final String packageName = getClass().getPackage().getName();
            add(new WicketExampleHeader("mainNavigation", Strings.afterLast(packageName, '.')));
        }
    }

    This is the class that we want all our web pages to extend. For exxample, notice the line in bold above. Every class extending WicketExamplePage will inherit an instance of WicketExampleHeader. The Wicket id is "mainNavigation", which is the Wicket id we saw in the previous step, in the Home.html file. We can refer to the "mainNavigation" Wicket id at the top of all our HTML pages. This ensures that all our web pages will have the same header. In the next step, we will open WicketExampleHeader.java and inspect its content.

  5. Open WicketExampleHeader.java. This is what it looks like:

    package com.myapp.wicket;
    import wicket.markup.html.basic.Label;
    import wicket.markup.html.panel.Panel;
    public class WicketExampleHeader extends Panel {
        public WicketExampleHeader(String componentName, String exampleTitle)
        {
            super(componentName);
            add(new Label("exampleTitle", exampleTitle));
        }
    }

    Notice the line in bold above. Here, we create a Wicket Label widget. The WicketExampleHeader is a reusable component. This is the Java side, where widgets are created. Lets look at the HTML side, which is where we can expect the Wicket Label widget to be rendered. In the next step, we will open the WicketExampleHeader.html file and inspect its content.

    Now change the second argument to "My Very First Component Based Application", so that the definition of the Label is now as follows:

    add(new Label("exampleTitle", "My Very First Component Based Application"));

  6. Open WicketExampleHeader.html. Notice that it has the same name as the Java file we have just looked at. It is found within the same package structure. This is what it looks like:

    <html xmlns:wicket>
    <body>
      <wicket:panel>
        <h1>Wicket Example</h1>
        <p id="titleblock">
            <b><font size="+1">Start of
            <span wicket:id="exampleTitle">Example Title Goes Here</span></font></b>
        </p>
      </wicket:panel>
    </body>
    </html>

    Notice the line in bold above. This is how you specify where a widget should be rendered in the HTML side of a web page. Hold down the Ctrl key and move your mouse over the value of the wicket:id attribute in the span tag. Notice that the value turns into a hyperlink:

    Click the hyperlink and notice that the Java side of the web page opens and that the cursor lands to the left of the opening quotation of the Wicket identifier:

    Now click the left arrow at the top of the Source Editor to return to the HTMl page. In this way, you can navigate quickly and efficiently between the two sides of Wicket components.

  7. Right-click the project and choose Run Project. The IDE compiles the application, creates a WAR file, sends it to the deployment server, opens the IDE's default browser, and displays the application:


Adding a Widget

In this section, we create our first widget in Wicket. Just like most other artifacts in Wicket, a widget has a Java side and an HTML side. On the Java side, the widget is created. On the HTML side, it is rendered. In NetBeans IDE, tools are provided for you to generate stubs on both the Java side and HTML side simultaneously. As shown previously, navigation between the two sides is made possible via a hyperlink.

  1. Open Home.html. Notice the "Wicket" category in the Component Palette:

    Note: At the time of writing, the Border item in the Wicket palette does not work.

  2. Drag the Label item into the HTML file, anywhere between the HTML file's BODY tags, below the SPAN tag. When you release the mouse, the following dialog box appears:

    Note: Parts of this dialog box do not work yet.

  3. Leave all the defaults exactly as they are and click OK.

  4. Notice that the IDE generates a new HTML tag for you:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <title></title>
            <link rel='stylesheet' type='text/css' href='style.css'/>
        </head>
        <body>
            <span wicket:id='mainNavigation'/>
            <span wicket:id="message1">this text will be replaced</span>
        </body>
    </html>

  5. Hold down the Ctrl key and move your mouse over the value of the Wicket id, as shown below:

  6. The Home.java file opens and the cursor lands at the start of the Wicket identifier on the Java side:

    Note: The Wicket Label, with the Wicket identifier, was created on the Java side when you dropped the Wicket Label on the HTML side.

  7. Right-click the project and choose Run Project. When the application is deployed again, you will see the Wicket Label, rendered in the Home.html file:

  8. Add H2 tags around the SPAN tag in the Home.html file and deploy the application again. Notice the result:

    Note: You can create placeholders, just like the SPAN tag generated in the Home.html file, and then hand the HTML file to your web designer. While the web designer designs the web page, you can work on the Java side and create the widgets completely independently. Since the HTML tags are not embedded in the Java file, you and the web designer can reap the rewards of Wicket's central focus of "separation of concerns".

Adding a Reusable Component

One of strengths of Wicket is the concept of "reusable components". Here, in this section, we use a wizard to generate a panel, which again has a Java side and an HTML side. We will create this panel so that we have a footer that we will reuse in our web pages, so that the footer is consistent across our web site. We will see how easy it is to add a panel to a web page.

  1. Right-click the MyFirstWicketApp project node and choose New > File/Folder. Under Categories, choose Wicket. Under File Types, notice the following templates:

    Note: At the time of writing, the BodyBorder template does not work.

    Choose Panel and click Next.

  2. Type FooterPanel in Class Name, select com.myapp.wicket in the Package drop-down list, and notice the superclass we are using is wicket.markup.html.panel.Panel. You should now see the following:

    Click Finish.

    Notice that we now have two new files in our package, FooterPanel.html and FooterPanel.html.

  3. Open FooterPanel.html and notice that the content of the file is as follows:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
      </head>
      <body>
         <wicket:panel>
            <span wicket:id="message">this text will be replaced</span>
         </wicket:panel>
      </body>
    </html>

    Between wicket:panel tags, a Wicket placeholder is found. Hold down the Ctrl key, move the mouse over the value of the Wicket id, and click the hyperlink that appears. The FooterPanel.java file opens, the cursor landing at the start of the Wicket identifier. Notice that a Wicket label has already been defined for us:

    package com.myapp.wicket;
    import wicket.markup.html.panel.Panel;
    import wicket.markup.html.basic.Label;
    public class FooterPanel extends Panel {
        /** Creates a new instance of FooterPanel */
        public FooterPanel(String id) {
            super(id);
            add(new Label("message", "I am a reusable component!"));
        }
    }

  4. Our panel, while simple, is actually complete already. Lets add it to the Homepage. Open Home.java and then create a new instance of FooterPanel, by adding the following line to the end of the Constructor:

    add(new FooterPanel("footerPanel"));

  5. Next, we need to render the panel. Open Home.html and add the placeholder tag right above the closing BODY tag, making sure to use the same Wicket identifier as used in the Java file:

    <span wicket:id='footerPanel'/>

  6. Run the project again. Notice that the panel is displayed, exactly where the HTML file specified it should be rendered:

    Note: In Wicket terminology, a panel is a reusable component. Exactly as shown in this section, you can reuse the panel as often as you like and in as many web pages as you fancy.



Next Steps

This is the end of the introduction to component based web development in NetBeans IDE. You are encouraged to continue your journey in the Wicket framework by working through the Pizza Application Sample described in A First Look at the Wicket Framework by David R. Heffelfinger.