wicketstuff-annotationのまとめ
wicketstuff-annotationを使えば「きれいなURL」とか「NiceURL」とか言われる部分の設定をアノテーションで指定することが出来るようになります。自分は最近までURLの綺麗さとかはあまり気にしないプロジェクトばかりやってたのですが、最近使い始めたのでメモ。
Mavenを使っている場合、pom.xmlにrepositoryとdependencyの要素を追加。
<repositories> <repository> <id>wicket-snaps</id> <url>http://wicketstuff.org/maven/repository</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <dependencies> <dependency> <groupId>org.wicketstuff</groupId> <artifactId>wicketstuff-annotation</artifactId> <version>1.1</version> </dependency> </dependencies>
まず、WebApplicationのサブクラスにおけるinitメソッド内は下記のようにします。
public class MyApplication extends WebApplication{ //いろいろ省略 @Override protected void init() { super.init(); //この1行を追加 new AnnotatedMountScanner().scanPackage("com.mycompany").mount(this); } }
AnnotatedMountScanner#scanPackageの引数にパッケージを指定するとそのパッケージ以下のPageクラスがアノテーションによる指定の対象になります。
そして下記のようなPageクラスを作成します。
@MountPath(path = "test") pulic class TestPage extends WebPage{ }
こうすると下記URLにてアクセス可能になります。(Context Rootがhogeの場合)
http://localhost:8080/hoge/test
パラメータがある場合はMountMixedParamアノテーションを使い、parameterNames要素にてStringの配列でパラメータ名を指定します。
@MountPath(path = "test") @MountMixedParam(parameterNames={"param1", "param2"}) pulic class TestPage extends WebPage{ }
また、Wicket自体にはURLの表示方法(パラメータの表示方法とか)を決定するクラスが複数あるのですが、それに対応するアノテーションもそろっています。上記のwicketstuff-annotationのページに対応表が載っているのですが、一応引用しておきます。
http://wicketstuff.org/confluence/display/STUFFWIKI/wicketstuff-annotation
Annotation Strategy @MountBookmarkablePageRequestTarget BookmarkablePageRequestTargetUrlCodingStrategy @MountHybrid HybridUrlCodingStrategy @MountIndexedHybrid IndexedHybridUrlCodingStrategy @MountIndexedParam IndexedParamUrlCodingStrategy @MountMixedParam MixedParamUrlCodingStrategy @MountQueryString QueryStringUrlCodingStrategy
各strategyクラスに関しては矢野さんの本か、id:Kishiさんの
WicketのURLをcoolにする - public static void main
で詳しく説明されてます。
使い方は↓のような感じでアノテーションを追加するだけ。
@MountPath(path = "test") @MountHybrid pulic class TestPage extends WebPage{ }
あと、リンクを作成する側ではBookmarkablePageLinkを使わずにPageLinkとかLinkを使っているとリンクを張っている側のページでは今までどおりの長いURLとか意味不明なURLになるので注意しましょう、っていうか自分はそのせいで2時間くらいハマりましたw情けないww
あと、Wicket側のバージョンはに関しては
「1.3.3でテスト済み。1.4でのテストはまだだけど動くよ!」
ってwicketstuff-annotationの中の人は言ってます。自分の環境は1.4rc2ですが今のところ問題ないです。
また、このライブラリはSpring(正確にはspring-core、バージョンは2.5.2)に依存してます。ただ、下位互換等は問題無いようですのでDIコンテナでSpringを使用している場合はpom.xmlで下記のようにしておけば問題ないと思います。
<dependency> <groupId>org.wicketstuff</groupId> <artifactId>wicketstuff-annotation</artifactId> <version>1.1</version> <!-- wicketstuff-annotationにおけるSpringの依存を排除することで、 別で定義してあるSpringのライブラリを使用させる --> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency>
wicketstuff-annotationに関してはこんなところでしょうか。使った感想は「使いやすい」の一言。initメソッド内がバカでかくならなくて済むし、Pageクラスを見ただけでURLがわかるのはいいことだなぁと思います。