LoginSignup
2

More than 5 years have passed since last update.

Wicket-Stuff Annotation の使い方

Last updated at Posted at 2013-11-29

Wicketでは、CleanURLのマウント設定をWebApplicationのサブクラスに書くのだけど、Wicket-Stuff Annotationを使うと、各WebPageにAnnotationで定義できるようになって素敵。ただし要Spring。

pom.xml

  <dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-annotation</artifactId>
    <version>6.12.0</version>
    <!-- 6.12.0だとspring-core, spring-asmの 3.0.6.RELEASE が入るので、
      既存のSpringのバージョンと異なる場合はexclusionsなどで調整 -->
  </dependency>

WicketApplication.java
public class WicketApplication extends WebApplication {

  /*中略*/

  @Override
  public void init() {
    super.init();
    // com.example.page パッケージ以下のクラスのAnnotationでマウントパスを設定
    new AnnotatedMountScanner().scanPackage("com.example.page").mount(this);
  }

}
FooPage.java
package com.example.page;

import org.apache.wicket.markup.html.WebPage;
import org.wicketstuff.annotation.mount.MountPath;

// このページを /path/to にマウント.
@MountPath("/path/to")
public class FooPage extends WebPage {
  /* 中略 */
}

パラメータ付きの時は、@MountPath("/path/to/param1/") の様に指定する。
また複数のパスを指定したいときは、valueとaltに分ける。実例としては以下の様な感じ。

注意
Wiki には@MountMixedParamが使えるように書いてあるけど、現在は消滅している。Wicket 1.5のMountedMapperの仕様変更時に変わったのかなー。

TestPage.java
package com.example.page;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.wicketstuff.annotation.mount.MountPath;

@MountPath(value="/test", alt={"/test/${param1}", "/test/${param1}/${param2}"})
public class TestPage extends WebPage {

  public TestPage(PageParameters params) {
    this.add(new Label("param1", params.get("param1").toString("unKnown.")));
    this.add(new Label("param2", params.get("param2").toString("unKnown.")));
  }

}
TestPage.html
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
</head>
<body>
    <p>param1 : <span wicket:id="param1"></span></p>
    <p>param2 : <span wicket:id="param2"></span></p>
</body>
</html>

なお従来通り、WebApplicationのサブクラスで設定されたCryptoMapperなどとの共存も可能。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2