T2入門その2 -@ActionPath-
さて、前回はT2の基本的な@Page、@POST/@GETについて説明しました.
今日はその続き、@ActionPath(と@Default)です.
前回のように物凄いシンプルな画面をつくるのであれば@POST/@GETで
問題ないですが、もう少し気の効いたことをしようとおもうと、あれだけでは足りません.
特にキレイなURLをつかって、メソッドを呼び分けたい場合などを考えると
なにかそこに制約が必要です.このURLを使って制約を与える機能の1つが@ActionPathです.
@ActionPath : 主にGETで使用され、URL指定で呼び出されるアクションメソッドを指定するアノテーション
value : アクセスされたときのURL.省略されると、メソッド名と同一のものが呼ばれる.
また、@DefaultはそのPageクラスのデフォルトでアクセスされるメソッドを決めます.
各Pageクラスにひとつ配置することを推奨してます.
では使い方を見てみましょう.HelloWorld的なもので試してみます.
URLのマッピング例として以下のような場合を考えてメソッドを呼び分けてみましょう.
- 通常は、http://localhost:8080/t2-samples/helloで「Hello」と表示する
- しかし、http://localhost:8080/t2-samples/hello/requestにアクセスした場合は「Hello from request().」と表示する
- 更に、http://localhost:8080/t2-samples/hello/strutsにアクセスした場合は「Hello from likeStrutsType().」と表示する
この場合を考えると、
- http://localhost:8080/t2-samples/helloの場合が基本となるので、これには@Defaultを使ってマッピングする
- http://localhost:8080/t2-samples/hello/requestの場合は、URLにrequestと入った場合だけなので、@ActionPath("request")でマッピングする
- http://localhost:8080/t2-samples/hello/strutsの場合は、URLにstrutsと入った場合だけなので、@ActionPath("struts")でマッピングする
実装コードは以下のような形になります.
(パッケージなど省略) /** * シンプルなHello Worldサンプル. * * @author shot */ /* http://yourdomain/t2-samples/helloでマッピング. */ @RequestScope @Page("hello") public class HelloPage { protected HelloService helloService; @Default public Navigation index(WebContext context) { context.getRequest().setAttribute("greet", helloService.hello()); return Forward.to("/jsp/hello.jsp"); } /** * http://yourdomain/t2-samples/hello/requestというURLで呼ばれる. */ @ActionPath public Navigation request(HttpServletRequest request) { System.out.println("request.getContextPath() : " + request.getContextPath()); request.setAttribute("greet", helloService.hello() + " from request()."); return Forward.to("/jsp/hello.jsp"); } /** * @ActionPathでvalueとして、"struts"としているので、 * http://yourdomain/t2-samples/hello/strutsというURLの場合呼ばれる. */ @ActionPath("struts") public Navigation likeStrutsType(HttpServletRequest request, HttpServletResponse response) { System.out.println("request.getContextPath() : " + request.getContextPath()); request.setAttribute("greet", helloService.hello() + " from likeStrutsType()."); return Forward.to("/jsp/hello.jsp"); } /** * Lucyによってインジェクトされる. * * @param helloService */ @Inject public void setHelloService(HelloService helloService) { this.helloService = helloService; } }
@ActionPathのvalue値は省略できます.省略するとメソッドと同じ名前を使います.
このあたりはT2の数少ない規約のひとつです.詳細はドキュメント参照.
クローズアップすると、以下のrequestメソッドが@ActionPathの省略系にあたります.
/** * http://yourdomain/t2-samples/hello/requestというURLで呼ばれる. */ @ActionPath public Navigation request(HttpServletRequest request) { ......
もちろん明示的に値を指定することも出来ます.
/** * @ActionPathでvalueとして、"struts"としているので、 * http://yourdomain/t2-samples/hello/strutsというURLの場合呼ばれる. */ @ActionPath("struts") public Navigation likeStrutsType(HttpServletRequest request, ......
@ActionPathは通常@GETと一緒に呼ばれることを想定していますが
特に何の制限もしていないので@POSTでも使えると思います.
まとめると、@POST/@GETだけでは機能的にあまりに自由すぎるので、
呼び出すアクションメソッドに対してURLベースで制約を与える機能、それが@ActionPathです.
次回はもうひとつのActionアノテーション、@ActionParamです.