Juzu Framewokを 使ってみた 第四回 渋谷java 2013.11.16 Saturday, November 16, 13
May 28, 2015
Juzu Framewokを使ってみた第四回 渋谷java2013.11.16
Saturday, November 16, 13
自己紹介• 芹沢和洋
• 株式会社ビズリーチ
• web屋(java歴1年ぐらい)
• @seri_k
• id:serihiro
• blog:http://serihiro.hatenablog.com/
• 最近はrubyに浮気中
Saturday, November 16, 13
第一回渋谷javaにて@hotchemiさんのLTにて紹介されていた。「play1.xライク」らしい
https://speakerdeck.com/hotchemi/ruo-zhe-tojava
Saturday, November 16, 13
実際に使ってみたJuzu Web Framework概要
• ViewとController機能のみを提供
• Templateはgroovyベース
• Annotationで機能や設定を指定
• フレームワークの機能をpackage名やclass名で指定しない。classpathが通ってる場所ならOK
• DIコンテナはSprint,Guice,Weldに対応Saturday, November 16, 13
ベースを作る• Mavenでサクっと(Tomcat + springを使う場合)
mvn archetype:generate \ -DarchetypeGroupId=org.juzu \ -DarchetypeArtifactId=juzu-archetype \ -DarchetypeVersion=0.7.0 \ -DgroupId=org.example \ -DartifactId=myapp \ -DpackageName=org.example.myapp \ -Dversion=1.0.0-SNAPSHOT \ -DjuzuServer=servlet \ -DjuzuInject=spring
Saturday, November 16, 13
起動<Tom猫> mvn tomcat7:run -Plive
<JBoss>mvn jboss-as:run
Saturday, November 16, 13
Tomcat7で起動してみる
Saturday, November 16, 13
Eclipseの開発環境構築• Juzu Frameworkは開発者が編集したControllerクラスやTemplateファイルから別ファイル(例:Controller_.java)を自動生成し、自動生成されたファイルが実行される仕組み
• ので、importした後に自動生成を実行する為の設定が必要
Saturday, November 16, 13
Eclipseの開発環境構築1.Annotation Processingを有効にして出力先を指定
Saturday, November 16, 13
Eclipseの開発環境構築2.Factory Pathにjuzu-core.jarを
M2リポジトリから自分で探してきてセットする
Saturday, November 16, 13
Rebuildすると
←自動生成されたクラス一式
←元ファイル
Saturday, November 16, 13
元ファイルの更新を検知して自動更新されると書いてあるが• 自分の環境だとBuild Automatically入れても自動更新されない
• むしろControllerかtemplateを更新するとファイルが消えて動かなくなる(Cleanすると再生成される)
←Controllerとtemplateが消失する
Saturday, November 16, 13
Controllerpublic class Controller {
@Inject @Path("index.gtmpl") Template index;
@Inject WeatherService weatherService;
@View @Route("/show/{location}") public Response.Content index(String location) throws IOException { HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put("location", "marseille"); parameters.put("temperature", weatherService.getTemperature("marseille"));
return index.with(parameters).ok();//response codeを指定 }
各アクションメソッドが使うTemplateファイルを指定してヒモ付
@Viewアノテーションが付いているとControllerとして認識されて
自動生成されるクラスに含まれる
Saturday, November 16, 13
Template
The weather temperature in ${location} is ${temperature} degrees.
<ul><% ["red","green","blue"].each {color -> out.print("<li>The sky is ${color}</li>") } %></ul>
<a href="@{Controller.purchase(product=1)}">Purchase</a>
groovyベースなのでgroovyのコードがそのまま実行できる
プロパティ差し込みはjspとかvmとかと一緒
urlは@{Controller名.メソッド名}で生成できる
Saturday, November 16, 13
TemplateコンパイルされたTemplateファイルはこんな感じのjavaクラスになる
package org.example.templates;import juzu.impl.plugin.template.metadata.TemplateDescriptor;import juzu.impl.plugin.template.TemplatePlugin;@javax.annotation.Generated({})public class index extends juzu.template.Template{@javax.inject.Injectpublic index(TemplatePlugin templatePlugin){super(templatePlugin, "/org/example/templates/index.gtmpl");}public static final juzu.impl.plugin.template.metadata.TemplateDescriptor DESCRIPTOR = new juzu.impl.plugin.template.metadata.TemplateDescriptor("/org/example/templates/index.gtmpl",0x3cf81bf9780efd8bL,org.example.templates.index.class,juzu.impl.template.spi.juzu.dialect.gtmpl.GroovyTemplateStub.class);public Builder builder() {return new Builder();}public Builder with() {return (Builder)super.with();}public class Builder extends juzu.template.Template.Builder{}}
Saturday, November 16, 13
Assets - js,cssの読み込み
@Assets({ @Asset( id = "myscript", value = "javascripts/myscript.js") })package my.application;
@View@Route("/hoge/")@WithAssets("myscript")public Response.Content hoge() throws IOException { return hoge.ok();}
server起動時に指定されたcssやjsファイルを読みに行くのでパスを間違えると全Controllerでエラーになる(静的ファイルをタイプセーフに扱う為)
使用するコントローラーのメソッドで使用するAssetのidを指定するとtemplateで書かなくても読み込まれる
Saturday, November 16, 13
Assets - js,cssの読み込み
Saturday, November 16, 13
まとめ• Annotationで全て機能を指定できる
• パッケージ名やクラス名に規約がないので作るアプリに応じて好きに設計できる
• クラス自動生成は若干難有り?
• 覚えなきゃいけないことが少ないので学習コストは低め
Saturday, November 16, 13
【告知】エンジニア募集中• ビズリーチでは幅広くエンジニア募集中です
• webサービス開発に携わりたい方
• 新規事業を興したい方
• awsをガシガシいじりたい方
• ピンと来たら
http://www.bizreach.co.jp/recruit/
Saturday, November 16, 13
ご清聴ありがとうございました
Saturday, November 16, 13