HTML and Java: Two Sides of the Same Wicket Coin
By Geertjan on Jul 13, 2005
Each web page in Wicket is like a coin. It has two sides -- a Java class and an HTML file.
- Setting Up the Coin: HelloWorldApplication.java. The application object creates the application that contains the web pages. The application object is a Java class. The absolute minimum content of the application object is the definition of the home page. The home page is the first web page displayed by the application object.
Here the compiled HelloWorld.class web page is set as the home page:
The web page consists of two sides -- the Java component (the back or tails side) and its HTML rendering (the front or heads side). Importantly, since they are two sides of the same coin, the two sides have the same name and are stored in the same folder structure. Normally, this means that they are stored in the same package. So, in this case, the two sides are called HelloWorld.java and HelloWorld.html and are stored together in the same package.
- Tails: HelloWorld.java. Here a Label component is created:
add(new Label("message", "Hello World!"));
There are two parameters: the component identifier ("message") and the content that the Label component should render ("Hello World!").
- Heads: HelloWorld.html. A Java component is used in an HTML file:
<span wicket:id="message">Message goes here</span>
The <span> element has one attribute, in the wicket namespace: the identifier ("id") which is defined as "message". Note that the Wicket identifier in the HTML file must match the component identifier in the Java component. The "Message goes here" text is a placeholder. You could write anything you like there -- it will be replaced by the Java component.
- Flipping the Coin. A web.xml file specifies the Wicket servlet wicket.protocol.http.WicketServlet that handles requests for the application object. A server, such as Tomcat or Jetty, is needed in order to deploy the application.
To see all of the above in practice, within the context of the two Java classes and HTML file that make up the "Hello World" application, use yesterday's blog entry to set everything up in NetBeans IDE. Alternatively, use another IDE. All Wicket applications are based on the above principles -- except that most Wicket applications have more than one coin. If you have enough coins, you can create a really rich application...