5. Wicket as page layout manager

Before going ahead with more advanced topics, we will see how to maintain a consistent layout across our site using Wicket and its component-oriented features. Probably this is not the most interesting use we can get out of Wicket, but it is surely the simplest one so it’s the best way to get our hands dirty with some code.

5.1. Header, footer, left menu, content, etc…​

There was a time in the 90s when Internet was just a buzzword and watching a plain HTML page being rendered by a browser was a new and amazing experience. In those days we used to organize our page layout using the HTML tag. Over the years this tag has almost disappeared from our code and it survives only in few specific domains. For example is still being used by JavaDoc.

With the adoption of server side technologies like JSP, ASP or PHP the tag has been replaced by a template-based approach where we divide our page layout into some common areas that will be present in each page of our web application. Then, we manually insert these areas in every page including the appropriate markup fragments.

In this chapter we will see how to use Wicket to build a site layout. The sample layout we will use is a typical page layout consisting of the following areas:

  • a header which could contain site title, some logos, a navigation bar, etc…​

  • a left menu with a bunch of links to different areas/functionalities of the site.

  • a footer with generic informations like web master’s email, the company address, etc…​

  • a content area which usually contains the functional part of the page.

The following picture summarises the layout structure:

layout

Once we have chosen a page layout, our web designer can start building up the site theme. The result is a beautiful mock of our future web pages. Over this mock we can map the original layout areas:

layout mock

Now in order to have a consistent layout across all the site, we must ensure that each page will include the layout areas seen above. With an old template-based approach we must manually put them inside every page. If we were using JSP we would probably end up using include directive to add layout areas in our pages. We would have one include for each of the areas (except for the content):

layout include

For the sake of simplicity we can consider each included area as a static HTML fragment.

Now let’s see how we can handle the layout of our web application using Wicket.

5.2. Here comes the inheritance!

The need of ensuring a consistent layout across our pages unveiled a serious limit of the HTML: the inability to apply inheritance to web pages and their markup. Wouldn’t be great if we could write our layout once in a page and then inherit it in the other pages of our application? One of the goals of Wicket is to overcome this kind of limit.

5.2.1. Markup inheritance

As we have seen in the previous chapter, Wicket pages are pure Java classes, so we can easily write a page which is a subclass of another parent page. But in Wicket inheritance is not limited to the classic object-oriented code inheritance. When a class subclasses a WebPage it also inherits the HTML file of the parent class. This type of inheritance is called markup inheritance. To better illustrate this concept let’s consider the following example where we have a page class called GenericSitePage with the corresponding HTML file GenericSitePage.html. Now let’s create a specific page called OrderCheckOutPage where users can check out their orders on our web site. This class extends GenericSitePage but we don’t provide it with any corresponding HTML file. In this scenario OrderCheckOutPage will use GenericSitePage.html as markup file:

markup inheritance

Markup inheritance comes in handy for page layout management as it helps us avoid the burden of checking that each page conforms to the site layout. However to fully take advantage of markup inheritance we must first learn how to use another important component of the framework that supports this feature: the panel.

If no markup is found (nor directly assigned to the class, neither inherited from an ancestor) a MarkupNotFoundException is thrown.

5.2.2. Panel class

Class org.apache.wicket.markup.html.panel.Panel is a special component which lets us reuse GUI code and HTML markup across different pages and different web applications. It shares a common ancestor class with WebPage class, which is org.apache.wicket.MarkupContainer:

page panel hierarchy

Illustration: Hierarchy of WebPage and Panel classes

Subclasses of MarkupContainer can contain children components that can be added with method add(Component…​) (seen in chapter 3.3). MarkupContainer implements a full set of methods to manage children components. The basic operations we can do on them are:

  • add one or more children components (with method add).

  • remove a specific child component (with method remove).

  • retrieve a specific child component with method get(String). The string parameter is the id of the component or its relative path if the component is nested inside other MarkupContainers. This path is a colon-separated string containing also the ids of the intermediate containers traversed to get to the child component. To illustrate an example of component path, let’s consider the code of the following page:

  1. MyPanel myPanel = new MyPanel ("innerContainer");
  2. add(myPanel);

Component MyPanel is a custom panel containing only a label having “name” as id. Under those conditions we could retrieve this label from the container page using the following path expression:

  1. Label name = (Label)get("innerContainer:name");
  • replace a specific child component with a new component having the same id (with method replace).

  • iterate thought children components. This can be done in the old way (pre-Wicket 8) using method iterator or using visitor pattern with method visitChildren. Starting from Wicket 8 the same task can be accomplished using the stream object returned by methods stream (which contains only the direct children) and streamChildren (which contains all children).

Both Panel and WebPage have their own associated markup file which is used to render the corresponding component. If such file is not provided, Wicket will apply markup inheritance looking for a markup file through their ancestor classes. When a panel is attached to a container, the content of its markup file is inserted into its related tag.

While panels and pages have much in common, there are some notable differences between these two components that we should keep in mind. The main difference between them is that pages can be rendered as standalone entities while panels must be placed inside a page to be rendered. Another important difference is the content of their markup file: for both WebPage and Panel this is a standard HTML file, but Panel uses a special tag to indicate which part of the whole file will be considered as markup source. This tag is . A markup file for a panel will typically look like this:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. ...
  5. </head>
  6. <body>
  7. <wicket:panel>
  8. <!-- Your markup goes here -->
  9. </wicket:panel>
  10. </body>
  11. </html>

The HTML outside tag will be removed during rendering phase. The space outside this tag can be used by both web developers and web designers to place some mock HTML to show how the final panel should look like.

5.3. Divide et impera!

Let’s go back to our layout example. In chapter 5.1 we have divided our layout in common areas that must be part of every page. Now we will build a reusable template page for our web application combining pages and panels. The code examples are from project MarkupInheritanceExample.

5.3.1. Panels and layout areas

First, let’s build a custom panel for each layout area (except for ‘content’ area). For example given the header area

header area

we can build a panel called HeaderPanel with a related markup file called HeaderPanel.html containing the HTML for this area:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. ...
  5. </head>
  6. <body>
  7. <wicket:panel>
  8. <table width="100%" style="border: 0px none;">
  9. <tbody>
  10. <tr>
  11. <td>
  12. <img alt="Jug4Tenda" src="wicketLayout_files/logo_jug4tenda.gif">
  13. </td>
  14. <td>
  15. <h1>Gestione Anagrafica</h1>
  16. </td>
  17. </tr>
  18. </tbody>
  19. </table>
  20. </wicket:panel>
  21. </body>
  22. <html>

The class for this panel simply extends base class Panel:

  1. package helloWorld.layoutTenda;
  2. import org.apache.wicket.markup.html.panel.Panel;
  3. public class HeaderPanel extends Panel {
  4. public HeaderPanel(String id) {
  5. super(id);
  6. }
  7. }

For each layout area we will build a panel like the one above that holds the appropriate HTML markup. In the end we will have the following set of panels:

  • HeaderPanel

  • FooterPanel

  • MenuPanel

Content area will change from page to page, so we don’t need a reusable panel for it.

5.3.2. Template page

Now we can build a generic template page using our brand new panels. Its markup is quite straightforward :

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. ...
  5. <!--Include CSS-->
  6. ...
  7. </head>
  8. <body>
  9. <div id="header" wicket:id="headerPanel">header</div>
  10. <div id="body">
  11. <div id="menu" wicket:id="menuPanel">menu</div>
  12. <div id="content" wicket:id="contentComponent">content</div>
  13. </div>
  14. <div id="footer" wicket:id="footerPanel">footer</div>
  15. </body>
  16. </html>

The HTML code for this page implements the generic left-menu layout of our site. You can note the 4

tags used as containers for the corresponding areas. The page class contains the code to physically assemble the page and panels:

  1. package helloWorld.layoutTenda;
  2. import org.apache.wicket.markup.html.WebPage;
  3. import org.apache.wicket.Component;
  4. import org.apache.wicket.markup.html.basic.Label;
  5. public class JugTemplate extends WebPage {
  6. public static final String CONTENT_ID = "contentComponent";
  7. private Component headerPanel;
  8. private Component menuPanel;
  9. private Component footerPanel;
  10. public JugTemplate(){
  11. add(headerPanel = new HeaderPanel("headerPanel"));
  12. add(menuPanel = new MenuPanel("menuPanel"));
  13. add(footerPanel = new FooterPanel("footerPanel"));
  14. add(new Label(CONTENT_ID, "Put your content here"));
  15. }
  16. //getters for layout areas
  17. //...
  18. }

Done! Our template page is ready to be used. Now all the pages of our site will be subclasses of this parent page and they will inherit the layout and the HTML markup. They will only substitute the Label inserted as content area with their custom content.

5.3.3. Final example

As final example we will build the login page for our site. We will call it SimpleLoginPage. First, we need a panel containing the login form. This will be the content area of our page. We will call it LoginPanel and the markup is the following:

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <wicket:panel>
  6. <div style="margin: auto; width: 40%;">
  7. <form id="loginForm" method="get">
  8. <fieldset id="login" class="center">
  9. <legend >Login</legend>
  10. <span >Username: </span><input type="text" id="username"/><br/>
  11. <span >Password: </span><input type="password" id="password" />
  12. <p>
  13. <input type="submit" name="login" value="login"/>
  14. </p>
  15. </fieldset>
  16. </form>
  17. </div>
  18. </wicket:panel>
  19. </body>
  20. </html>

The class for this panel just extends Panel class so we won’t see the relative code. The form of this panel is for illustrative purpose only. We will see how to work with Wicket forms in chapters 11 and 12. Since this is a login page we don’t want it to display the left menu area. That’s not a big deal as Component class exposes a method called setVisible which sets whether the component and its children should be displayed.

The resulting Java code for the login page is the following:

  1. package helloWorld.layoutTenda;
  2. import helloWorld.LoginPanel;
  3. import org.apache.wicket.event.Broadcast;
  4. import org.apache.wicket.event.IEventSink;
  5. public class SimpleLoginPage extends JugTemplate {
  6. public SimpleLoginPage(){
  7. super();
  8. replace(new LoginPanel(CONTENT_ID));
  9. getMenuPanel().setVisible(false);
  10. }
  11. }

Obviously this page doesn’t come with a related markup file. You can see the final page in the following picture:

final login page

5.4. Markup inheritance with the wicket:extend tag

With Wicket we can apply markup inheritance using another approach based on the tag . This tag is used inside the parent’s markup to define where the children pages/panels can “inject” their custom markup extending the markup inherited from the parent component. An example of a parent page using the tag is the following:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. This is parent body!
  7. <wicket:child/>
  8. </body>
  9. </html>

The markup of a child page/panel must be placed inside the tag . Only the markup inside will be included in final markup. Here is an example of child page markup:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. <wicket:extend>
  7. This is child body!
  8. </wicket:extend>
  9. </body>
  10. </html>

Considering the two pages seen above, the final markup generated for child page will be the following:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. This is parent body!
  7. <wicket:child>
  8. <wicket:extend>
  9. This is child body!
  10. </wicket:extend>
  11. </wicket:child>
  12. </body>
  13. </html>

5.4.1. Our example revisited

Applying tag to our layout example, we obtain the following markup for the main template page:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. <div id="header" wicket:id="headerPanel">header</div>
  7. <div id="body">
  8. <div id="menu" wicket:id="menuPanel">menu</div>
  9. <wicket:child/>
  10. </div>
  11. <div id="footer" wicket:id="footerPanel">footer</div>
  12. </body>
  13. </html>

We have replaced the

tag of the content area with the tag . Going forward with our example we can build a login page creating class SimpleLoginPage which extends the JugTemplate page, but with a related markup file like this:

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <wicket:extend>
  6. <div style="margin: auto; width: 40%;">
  7. <form id="loginForm" method="get">
  8. <fieldset id="login" class="center">
  9. <legend >Login</legend>
  10. <span >Username: </span><input type="text" id="username"/><br/>
  11. <span >Password: </span><input type="password" id="password" />
  12. <p>
  13. <input type="submit" name="login" value="login"/>
  14. </p>
  15. </fieldset>
  16. </form>
  17. </div>
  18. </wicket:extend>
  19. </body>
  20. </html>

As we can see this approach doesn’t require to create custom panels to use as content area and it can be useful if we don’t have to handle a GUI with a high degree of complexity.

5.5. Summary

Wicket applies inheritance also to HTML markup making layout management much easier and less error-prone. Defining a master template page to use as base class for the other pages is a great way to build a consistent layout and use it across all the pages on the web site. During the chapter we have also introduced the Panel component, a very important Wicket class that is primarily designed to let us divide our pages in smaller and reusable UI components.