t2 - 関ジャバ1月27日

57
つつつ つつつつつ つつつつつつつつ 、、! Web つつつつつつつ T2 つつつ Copyright 2010 The Team T2 Framework and the others All Rights Reserved. Go Tanaka <[email protected]>

Upload: go-tanaka

Post on 11-May-2015

2.033 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: T2 - 関ジャバ1月27日

つなぐ、つながる、つないでなんぼ!Web  フレームワーク 「 T2  」 の紹介

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

Go Tanaka<[email protected]>

Page 2: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

自己紹介

     田中 豪 (  たなか ごう )

Blog: http://d.hatena.ne.jp/tan_go238/

Twitter: http://twitter.com/tan_go238/

Page 3: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

「T2」って何?

リクエストをかしこく捌く!

つなぐ、つながる!

シンプルでわかりやすい!

他のフレームワークと「つなげて」使うことを前提としたフレームワーク

特徴特徴

Page 4: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

シンプルで分かりやすい

アノテーションベース

@Page(“/hello”)public class Hoge {

@GET@ActionPath(“/world”)public Navigation helloWorld(){

…}

}

http://yoursite.com/app/hello/world

きれいな URL!

Page 5: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

そもそも、そんなにたくさんの機能はない

シンプルで分かりやすい

DI コンテナ(シンプルな内部コンテナをデフォルトで使用)

DB フレームワークValidation フレームワークトランザクション制御  ( t2-ext にもある)

自分の使いやすいものを使ってね!

Page 6: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

つなぐ、つながる!

自分の使いやすいものをつなげて使おう!

DI コンテナ  Seasar, Spring, Guice, Lucy, 内部 DI コンテナ

OR マッパー  S2Dao, DBFlute, Doma, Hibernate, iBatis

テンプレートエンジン  JSP, ZPT, Mayaa...

サンプルもいっぱいあるよ!

Page 7: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

リクエストをかしこく捌く!

様々なリクエスト対応したメソッドを呼び出せる!

AMF や Ajax のリクエストが捌ける

URL パスやリクエストパラメータで切り替えができる

GET や POST でメソッドの切り替えができる

レスポンスの種類も色々あるよ!

Page 8: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

機能紹介

メソッドアノテーション

引数アノテーション

引数の型

T2 の基本的な使い方は以下の3つをおさえておけばOK!

Page 9: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーションとは?

リクエストとメソッドのマッピングを行います

@Page(“/hello”)public class Hoge {

@GET@ActionPath(“/world”)public Navigation helloWorld(){

…}

}

順番も重要

Page 10: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーションとは?

@GET, @POST

@ActionPath

@ActionParam

@Ajax

@Amf

@Default

メソッドアノテーション一覧

Page 11: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@GET と @ActionPath を使ったサンプル

メソッドアノテーション

@Page(“/hello”)public class Hoge {

@GET@ActionPath(“/world”)public Navigation helloWorld(){

…}

}

http://yoursite.com/app/hello/world

Page 12: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーション

@Page(“/hello”)public class Hoge {

@POST@ActionParampublic Navigation add(WebContext context){

…}

}

@POST と @ActionParam を使ったサンプル

<form name="addForm" action="/{cx}/hello” method="post"> <input type="text" name="arg1" /><br /> <input type="submit" name="add" value=" 送信 "/></form>

@ActionParam だけだとメソッド名=ボタンの name 属性と認識

Page 13: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@ActionParam

@ActionParam はサブミットされたボタンを認識するためのアノテーション

@ActionParam("hoge") のように書いている場合、

hoge という name 属性を持つボタンが押されたときに動きます

メソッドアノテーション

@Page(“/hello”)public class Hoge { @POST @ActionParam(“hoge”) public Navigation foo(WebContext context){

… }}

Page 14: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@ActionParam

メソッドアノテーション

@Page(“/hello”)public class Hoge { @POST @ActionParam(“add.x”) public Navigation imgTest(WebContext context){

… }}

T2 のサンプルには下記のように @ActionParam に .x がついたものがあるが

これは画像を使った input type=image のときに IE が name 属性

add の名前に対して .x をつけてポストしてくるため

<input type=“image” name=“add” ~

Page 15: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@Ajax を使ったサンプル(1)

メソッドアノテーション

@Page("/ajaxJQuery")public class AjaxJQueryPage { @Ajax @POST public Navigation execute(WebContext context) {         String hoge = context.getRequest().getAttribute("hoge"); return Json.convert(hoge + " is jQuery."); }}

HTTP ヘッダで Ajax のリクエストを判別

Page 16: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@Ajax を使ったサンプル(2)【 jQuery 】

$(funcion() { $("#submit").click(function() { $.ajax( { "url" : "{cx}/ajaxJQuery/execute", "type" : "post", "data" : { "hoge" : $("#hoge").val() }, "success" : function(response) { var o = eval("(" + response + ")"); $("#foo").text(o); }, "error": function(xmlHttpReq, status, e) { $("#foo").text("error"); } } ); return false; } );});

メソッドアノテーションメソッドアノテーション

Page 17: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーション

@Amf を使ったサンプル(1)

@Page("amftest")public class AmfTestPage {

@Amf public Navigation findAll() { List<Bar> ret = new ArrayList<Bar>(); ・・・ return AmfResponse.to(ret); }}

Page 18: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーション

@Amf    を使ったサンプル (2) 【 AS 】

<mx:RemoteObject id="remote" destination="amftest" endpoint="t2.amf"/>

public function creationCompleteHandler(e:FlexEvent):void { document.button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { var token:AsyncToken = document.remote.findAll(); token.addResponder(new AsyncResponder(resultHandler, faultHandler)); });}public function resultHandler(e:ResultEvent, o:Object=null):void { var bar:ArrayCollection = e.result as ArrayCollection; ・・・}public function faultHandler(e:FaultEvent, o:Object=null):void { Alert.show("error : " + e.message);}

endpoint 属性は特に指定はない

Page 19: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

メソッドアノテーション

@Amf

T2 の AMF 通信は AMF3 かつリモーティングに限定

Messaging( データプッシュ ) などもしたい場合は BlazeDS を使用

AmfContext の切り替えはクラスパス上に blazeds のクラスがあるかどうか

BlazeDS を使う時は blazeds-common.jarblazeds-core.jar および設定が必要だよ!

Page 20: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

どのメソッドの条件にも該当しなかった場合、

@Default がついたメソッドが呼び出されます

メソッドアノテーション

@Default

Page("hoge")public class HogePage {

@Default public Navigation index() { return Forward.to("/jsp/index.jsp"); }}

Page 21: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数アノテーションとは?

リクエストパラメータなどといった特定の値を引数アノテーションによって取得します

引数アノテーションに適切なパラメータ名を書いておけば、 T2 が自動的にセットしてくれます

@Page("requestparam")public class RequestParamPage { @GET @Ajax public Navigation execute( @RequestParam("ff") String foo, @RequestParam("bb") String bar) { return Json.convert( new String[] { foo, bar }); } }}

Page 22: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数アノテーション

@RequestParam

@RequestHeader

@SessionAttr

@Upload

@Form

@Index

@Var

引数アノテーション一覧

Page 23: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@RequestParam

引数アノテーション

リクエスト内にある特定のパラメータを取得することができる

@Page("requestparam")public class RequestParamPage { @GET @Ajax public Navigation execute( @RequestParam("ff") String foo, @RequestParam("bb") String bar) { return Json.convert( new String[] { foo, bar }); } }}

Page 24: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数アノテーション

@RequestHeader

リクエスト内のヘッダーを取得することができる

@Page("/requestheader")public class RequestHeaderPage {

@POST @ActionParam public Navigation execute1(@RequestHeader Map<String,String> map, WebContext context) { }

@POST @ActionParam public Navigation execute2(              @RequestHeader(key = "content-type") String header, WebContext context) { }}

Page 25: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数アノテーション

@SessionAttr

セッション内の指定した属性の値を取得することができる

@Page("session")public class SessionPage { @POST @ActionParam public Navigation message( @SessionAttr("msg1") String message1, @SessionAttr("msg2", nullable = false) String message2, WebContext context) { Request request = context.getRequest(); Session session = context.getSession(); request.setAttribute("message1", message1); request.setAttribute("message2", message2); return Forward.to("/jsp/sessionAttr.jsp"); }}

nullable 属性を false にすると例外が発生し、このメソッドは呼び出されません

Page 26: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数アノテーション

@Upload

ファイルがアップロードされたときに、明示的にどのファイルかを指定できる

@Page("upload")public class UploadPage { @POST @ActionParam public Navigation upload( @Upload("ff") UploadFile file, HttpServletRequest request) {

System.out.println("file:" + file.getName()); System.out.println("size:" + file.getSize()); System.out.println(file.getContentType()); request.setAttribute(file.getName() + " is uploaded."); return Forward.to("/jsp/upload.jsp"); }}

Page 27: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

@Form

@Form は、サブミットされた FORM の値全てを Java のオブジェクトに

マッピングしてもらうためのアノテーションです

引数アノテーション

@Page("/form")public class FormPage { @POST @ActionParam public Navigation withForm( @Form AddForm dto, WebContext context, ErrorInfo errorInfo) { Request request = context.getRequest(); if (errorInfo.hasError()) { request.setAttribute("message",Constants.ERR_MSG); return Forward.to("jsp/error.jsp"); } request.setAttribute("result","success"); return Forward.to("/jsp/form.jsp"); }}

<input type=“submit” name=“withForm” ~

Page 28: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

ForEach のインデックス値を取得することができます

引数アノテーション

@Page("/foreach")public class ForeachPage { @POST @ActionParam("hoge[{index}]") public Navigation hoge(@Index int id, WebContext context) { final Integer i = Integer.valueOf(id); context.getRequest().setAttribute("msg", String.valueOf(i.intValue() + 1) + " submitted."); return Forward.to("/jsp/foreach.jsp"); }}

 引数の型は int  または Integer である必要がある

@Index

Page 29: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

<form method="post" action="${t:url('/foreach')}"> <c:forEach var="e" items="${hogeList}" varStatus="s"> <input type="submit" name="hoge[${s.index}]" value="${e}"/> <br /> </c:forEach> <span>${message}</span></form>

HTML側 (JSP) の記述

引数アノテーション

@Index

Page 30: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

URL の一部を引数として受け取る事ができます

引数アノテーション@Var

@Page("hoge")public class VarPage {

@Default @ActionPath("{foo}") public Navigation index(@Var("foo") String foo) { return NoOperation.noOp(); } @GET @ActionPath(“piyo/{foo}") public Navigation var(@Var("foo") String foo) { return NoOperation.noOp(); }}

ex) http://domain/cx/hoge/123

ex) http://domain/cx/hoge/piyo/123

Page 31: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

Page  に対しても @Var を使用することもできます

引数アノテーション@Var

@Page("/var/{aaa}/{bbb}")public class VarPage { @Default @GET public Navigation index( @Var("aaa") String string, @Var("bbb") String bbb, Request request) { System.out.println("VarPage.index() called"); System.out.println("VarPage.aaa:" + string); System.out.println("VarPage.bbb:" + bbb); request.setAttribute("var", string + " from " + bbb); return Forward.to("/jsp/var.jsp"); }}

ex) http://domain/cx/var/123/456

Page 32: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数の型

欲しいインスタンスをメソッドの引数によって取得します

引数に適切なオブジェクトを書いておけば、 T2 が自動的にセットしてくれます

public Navigation add(HttpServletRequest request, HttpServletResponseresponse)

使用したいオブジェクトを引数に指定するだけ

Page 33: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数の型

HttpServletRequest, HttpServletResponse, HttpSession

ServletContext

Cookie/Cookie[]

WebContext

Request, Response

UploadFile

ErrorInfo

Page 34: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

HttpServletRequest

引数の型

@Page("/request")public class RequestPage { @POST @ActionParam public Navigation message(HttpServletRequest request) { request.setAttribute("hello", "Hello."); return Forward.to("/jsp/request.jsp"); }}

サーブレットリクエスト

Page 35: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数の型

ServletContext

@Page("/hoge")public class Hoge { @Default public Navigation index(ServletContext context) { ・・・ }}

サーブレットコンテキスト

Page 36: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

WebContext

T2のコンテキストオブジェクト。リクエストやレスポンスを丸ごと持つ

T2内部のコンテキストも含まれる

引数の型

@Page("context")public class ContextPage { @Default public Navigation index(WebContext context) { Request request = context.getRequest(); request.setAttribute("message", "hogehoge"); return Forward.to("/jsp/context.jsp"); }}

Page 37: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数の型

UploadFile

アップロードされたファイルを取得することができます

@Page("upload")public class UploadPage { @POST @ActionParam public Navigation upload( UploadFile file, HttpServletRequest request) { System.out.println("file:" + file.getName()); System.out.println("size:" + file.getSize()); System.out.println(file.getContentType()); request.setAttribute(file.getName() + " is uploaded."); return Forward.to("/jsp/upload.jsp"); }}

Page 38: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

UploadFile

引数の型

アップロードされたファイルを配列で取得することもできます

@Page("upload")public class UploadPage { @POST @ActionParam public Navigation upload(UploadFile[] files) { for (UploadFile file : files) { System.out.println("file:" + file.getName()); System.out.println("size:" + file.getSize()); System.out.println(file.getContentType()); } return Forward.to("/jsp/upload.jsp"); }}

Page 39: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

引数の型

ErrorInfo

@Form 使用時型があわないなどの変換エラーが発生した場合、その値は null のままになります

@Form  で変換エラーが出たときの情報を ErrorInfo が保持します

@Page("/hoge")public class HogePage { @POST @ActionParam public Navigation addWithForm( @Form inputForm dto, WebContext context, ErrorInfo errorInfo) { Request request = context.getRequest(); if (errorInfo.hasError()) { request.setAttribute("msg", Constants.ERR_MSG); return Forward.to("jsp/input.jsp"); } return Forward.to("/jsp/result.jsp"); }}

Page 40: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

手順

1. T2core.jar と依存 Jar

2. DI コンテナに必要な Jar と依存 Jar

3. ORM に必要な Jar...

4.ディレクトリ構成は ...

Page 41: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

何?めんどくさい??

Page 42: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

Vili (ヴィリ)があるよ!!

Page 43: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

Vili (ヴィリ)って?

・ Eclipse プラグインによるプロジェクト作成支援

・プロジェクトの雛形(スケルトン)を生成

・スケルトンは Maven のアーティファクトとして管理

・フラグメントを定義することで Vili で作成したプロジェクトに

 後から機能の追加もできる

T2 をはじめよう!

Page 44: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

引用元: http://d.hatena.ne.jp/skirnir/20091021/1256105886

Page 45: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

引用元: http://d.hatena.ne.jp/skirnir/20091021/1256105886

Page 46: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

Gaelyk もやりたい?

Page 47: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

あるよ!!

T2 をはじめよう!

Page 48: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

T2 + Gaelyk の下準備

・ Eclipse 3.5

・ Groovy

・ GAE/J SDK

・ Eclipse Plugin (GAE/J 、 Groovy)

     Google Plugin for Eclipse     http://code.google.com/intl/ja/eclipse/docs/getting_started.html

     GroovyEclipse Plugin     http://groovy.codehaus.org/Install+GroovyEclipse+Plugin

Page 49: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

T2 + Gaelyk のセットアップ

1. T2MeetsGaelykSetup.groovy を DL

2.ダウンロードしたスクリプトを実行する

3. Eclipse にインポート

Page 50: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 をはじめよう!

T2 + Gaelyk のセットアップ

1. T2MeetsGaelykSetup.groovy を DL

以下の URL から T2MeetsGaelykSetup.groovy をダウンロードする

  http://code.google.com/p/t2samples/downloads/list

Page 51: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 + Gaelyk のセットアップ

2.ダウンロードしたスクリプトを実行する

T2 をはじめよう!

groovy T2MeetsGaelykSetup.groovy プロジェクト名

Page 52: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 + Gaelyk のセットアップ

3. Eclipse にインポート

Eclipse のファイルメニューから以下の順で選択していく

T2 をはじめよう!

File -> Import

Existing Projects into Workspace Select root directory -> Browse -> Finish 

Package Explorerからプロジェクト名を右クリック -> Run As -> Web Application 

実行

Page 53: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

まとめ

メリット  敷居はそんなに低くない    ・わかりやすい    ・機能が絞られている    ・プロジェクト生成プラグインがある

   Ajax 、 AMF 通信するときに気軽に使える    ・追加で必要な jar や設定がない    ・通常のリクエストと区別できる  デメリット  単体で使うには機能が少ない   ・ t2-ext もしくは誰かが作ったものが充実する必要がある

  実績が比較的少ない   ・使用ユーザーの介入が不可欠

Page 54: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

まとめ

・もっとプラグインが充実して欲しい!

・もっとサンプルが充実して欲しい!

・もっと使ってくれるユーザーが欲しい!

みんながどんどん使っていくと、面白いぐらい進化しそう!

実はプラグインなど拡張するための仕組みも充実

Page 55: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 プロジェクト

T2 プロジェクト

    http://code.google.com/p/t-2/

Committer

    shot6              http://d.hatena.ne.jp/shot6/

    skirnir             http://d.hatena.ne.jp/skirnir/

    c9katayama http://d.hatena.ne.jp/c9katayama/

    yone098 http://d.hatena.ne.jp/yone098/

Page 56: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

T2 プロジェクト

designed by カネウチカズコ

Page 57: T2 - 関ジャバ1月27日

Copyright  2010  The Team T2 Framework and the others All Rights Reserved.

ご清聴ありがとうございました!!