Sunday, May 29, 2011

Playing with Wicket 1.5

This weekend I played with the new wicket 1.5 and saw some improvements that I'd like to share. These improvements are about page mounting and page parameter translation, it turns out that we don't choose anymore how parameters will be passed to the page when we mount the page but just simply mount the page and then you can read parameters in a new way.

Mounting a Page.
To mount a page we just need to (as always) call the mount method on our application class, but we can note that the wicket folks have removed (not even deprecated) the mount methods that take IRequestTargetUrlCodingStrategy as a parameter and instead created a simple mountPage method.

Here is an example of how pages will be mounted from now on:
   @Override  
protected void init() {
super.init();
mountPage("/home", HomePage.class);
}

Reading the Parameters.
Some of you may ask, if the URL coding strategies have been removed, now how can we read the page parameters?
The answer is simple! From the PageParameters object we get on the page constructor, this class has been rebuilt and moved into another package and no longer extends ValueMap, this change is HUGE and it will impact lots of my code for sure, but anyway, I think this change is for good since now I can query lots of things to read the parameters and something really ugly is now removed from our lives: We now can read indexed parameters with numbers and not with string-numbers. We also have the chance to read the parameters converted as different types in a much more elegant way.

Here is an example of how parameters can be read:
 public class HomePage extends WebPage {  
private static final long serialVersionUID = 1L;

public HomePage(PageParameters params) {

//build a list of the page parameters:
ArrayList<String> stringList = new ArrayList<String>();

int indexed = params.getIndexedCount();

//go through the indexed page parameters
for (int i = 0; i < indexed; i++) {
stringList.add(i + ": " + params.get(i).toString());
}

//go through the named page parameters
for (String key : params.getNamedKeys()) {
stringList.add(key + ": " + params.get(key).toString());
}
...
}
}

Servlet 3 Integration.
Wicket 1.5 wont have integration with servlet 3 but I've managed to use servlet3-style configuration for the wicket filter, the way it's done is less than ugly and I hope they add servlet 3 support in the future at least in the form of a separate project.

Here's how I did it:
 package com.juancavallotti.wicket15;  

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebFilter;
import org.apache.wicket.protocol.http.WicketFilter;

/**
* This is a dummy subclass of filter to allow servlet 3.0 style config.
* @author juancavallotti
*/
@WebFilter(urlPatterns = "/*", initParams = {
@WebInitParam(name = "applicationClassName",
value = "com.juancavallotti.wicket15.DemoWicketApplication"),
@WebInitParam(name = "filterMappingUrlPattern",
value = "/*")
})
public class MyWicketFilter extends WicketFilter {
}



No comments:

Post a Comment