-
Notifications
You must be signed in to change notification settings - Fork 298
Annotation
The annotation library is used to mount your pages declaratively via Java annotations instead of programatically via mount() calls in your WebApplication.init() method.
A typical wicket application might have several pages mounted like the following:
public class MyWicketApplication extends WebApplication
{
@Override
protected void init()
{
mountBookmarkablePage("terms", Terms.class);
mount(new MixedParamUrlCodingStrategy("dogs", Dogs.class,
new String[] {"sport", "show"}));
}
}
With annotation, you annotate each page like this:
@MountPath("terms")
public class Terms extends WebPage { ... }
@MountPath("dogs")
@MountMixedParam(parameterNames={"sport", "show"})
public class Dogs extends WebPage { ... }
And use the AnnotatedMountScanner in your init method like this:
public class MyWicketApplication extends WebApplication
{
@Override
protected void init()
{
new AnnotatedMountScanner().scanPackage("com.acme.wicket.pages").mount(this);
}
}
This library was created because of the desire for the following capabilities:
- The ability to encapsulate how a page is mounted with the code for the page itself
- Remove the need to edit the WebApplication class each time a new page is added.
Java annotations and class-path scanning are a good way to achieve these goals.
Note: Not all developers like the use of annotations and not all Wicket applications may benefit from this library. Our goal is to offer a nice alternative way to address the need to mount pages in wicket in order to create nice URLs. How it Works
The AnnotatedMountScanner does all the work. It uses another annotation class, MatchingResources to scan the class path looking for classes annotated with @MountPath. See the MatchingResources section for details on this class.
The entire class path is not necessarily scanned. You can limit what packages to look in. In the example above, only classes under com.acme.wicket.pages are searched. The class path scanning is very efficient by itself and limiting the search only makes it faster.
The @MountPath annotation specifies the primary and alternate paths that the page should be mounted on. The primary path is the one chosen when Wicket maps a page to a path. The primary and alternate paths are used when Wicket maps paths back to pages.
The next step is to determine which URL encoding strategy to use. Unless one is explicitly specified, the default used is BookmarkablePageRequestTargetUrlCodingStrategy. In the example above, the Terms page uses the default.
You can explicitly define one by using one of the strategy-specific annotations. In the example above, the Dogs page uses the @MountMixedParam annotation which is associated with the MixedParamUrlCodingStrategy strategy. There is an annotation for each page-specific strategy that comes with Wicket (see next section). Strategy Annotations
The following table lists the strategy-specific annotation in annotation and the corresponding strategy class: Annotation Strategy
-
@MountBookmarkablePageRequestTarget BookmarkablePageRequestTargetUrlCodingStrategy
-
@MountHybrid HybridUrlCodingStrategy
-
@MountIndexedHybrid IndexedHybridUrlCodingStrategy
-
@MountIndexedParam IndexedParamUrlCodingStrategy
-
@MountMixedParam MixedParamUrlCodingStrategy
-
@MountQueryString QueryStringUrlCodingStrategy
You can define your own annotation. The Javadoc explains this in more detail. The best way to implement your own is to follow the example of an existing annotation.
Matching is done via Spring's PathMatchingResourcePatternResolver class. This means that annotation has a dependency on Spring. When we build, we use spring-core 2.5.2, but this class has been part of Spring since 1.0.2, so MatchingReources should work with almost any version of Spring.
The MatchingResources class is useful for finding any resources on the path. It was adapted from Donohoe Digital's core architecture and contributed as part of this library. Technically, it does not depend on wicket, so it could at some point be moved into another package.
The annotation project uses the getAnnotatedMatches() method to find classes with the @MountPath annotation. Note that Spring does not load the class to determine this information. Instead, it uses a meta-data reader to determine this (which is faster than going through class loading).
- annotation
<dependency>
<groupId>org.wicketstuff</groupId>
<artifactId>annotation</artifactId>
<version>1.4.17</version>
</dependency>
<dependency>
<groupId>org.wicketstuff</groupId>
<artifactId>annotation</artifactId>
<version>1.4-SNAPSHOT</version>
</dependency>
<repository>
<id>wicketstuff-core-snapshots</id>
<url>ttps://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
Path is: https://github.com/wicketstuff/core/tree/core-1.4.x/jdk-1.5-parent/annotation
Note Annotation has not yet been ported to wicket 1.5
Just like Wicket itself, the annotation library is licensed under Apache 2.0.
Doug Donohoe of Donohoe Digital LLC is the author of annotation. He can be reached at doug (at) donohoe (dot) info.