Writing and injecting dynamic plugins into your Apache Wicket application


2009-12-04 23:35 | Author: vytautas.racelis

Let's say you have a written a web portal/application. You already have layout of your site. But there is a new requirement - you have to write independent component, which should be injected into your existing application. Application does not know about this component anything; component does not know about application anything. How to make them work together? To make it more complext - add another requirement - every component should have it's own link and should be injected into existing menu.

Introduction

This task maybe seems very easy at first glance, but if we look a little bit deeper we may find some issues on this case:
- Writing WebPage will finalize html layout;
- Panel is not mountable (if we decide that extending a panel is enough).

So, the whole idea is very simple:
- Develop component as a Panel;
- Create virtual page for this component;
- Mount virtual page;
- Register virtual page as menu item using PageAnnotationScanner;
- Injecting Panel content into existing application layout using org.xaloon.wicket.component.application.VirtualPageFactory.


Developing component

This part is easy: you do what you need to do, except one thing: Panel constructor should accept id as first attribute and PageParameters as second attribute; This property will be required if your component works with page parameters.

public MyPanel(String id, PageParameters parameters) {
super(id);
...
}

Creating virtual page

Virtual page is required:
1. to be able to generate bookmarkable page;
2. to be able to create menu item.

@MountPage(path="/my-page")
@MountPanel(panel=MyPanel.class)
public class MyPage extends WebPage {
...
}

@MountPage will be required to mount page as a bookmarkable page, also it will be required to generate dynamic menu item

@MountPanel defines real component class which will be injected into application layout.

So far so good, right? :) Remember, MyPage also might be used as a single page.

Prepare layout application

Here you will need to extend org.xaloon.wicket.component.application.AbstractWebApplication and implement one method:

 
@Override
public Class<? extends Page> getLayoutPageClass() {
return MyLayoutBasePage.class;
}

LayoutPage html code should contain such tag:
<div wicket:id="content"/>.
Your component will be placed here.

init method also should contain such line:

getSessionSettings().setPageFactory(new VirtualPageFactory());

This is an instruction to use VirtualPageFactory to inject virtual page content into application's layout.

VirtualPageFactory

VirtualPageFactory takes your panel from virtual page (@MountPanel annotation) and injects it into your web application layout.

Menu component

If you want to use dynamic menu, generated by mounting component, just add this line into your WebPage:


add (new MenuPanel("menu", true));

HTML code:
<div wicket:id="menu"/>

Summary

Once you've created wicket layout page, you should be able create dynamic components very easy. They should be injected into your layout page and visible via dynamic menu.

Ok, have a nice time with Wicket and i am going for a summer vacation :)

 

P.S. Required annotations, VirtualPageFactory, AbstractWebApplication might be found here.

 
 
 
 
 
 
 
<< < > >>
 
About xaloon.org

xaloon.org provides apache wicket based components for web and business solutions.

Learn more »
Follow Us (RSS)
Help & Support

Contact us in order to get help and support.

Online contact form »
Get in touch
Online contact form »