13. Controller
public class Controller {
@Inject
@Path("index.gtmpl")
Template index;
@Inject
WeatherService weatherService;
各アクションメソッドが使う
Templateファイルを指定してヒモ付
@Viewアノテーションが付いていると
Controllerとして認識されて
@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"));
}
Saturday, November 16, 13
return index.with(parameters).ok();//response codeを指定
14. Template
プロパティ差し込みはjspとかvmとかと一緒
The weather temperature in ${location} is ${temperature} degrees.
groovyベースなのでgroovyのコードが
そのまま実行できる
<ul>
<%
["red","green","blue"].each {color -> out.print("<li>The sky is ${color}</
li>") }
%>
</ul>
urlは@{Controller名.メソッド名}で生成できる
<a href="@{Controller.purchase(product=1)}">Purchase</a>
Saturday, November 16, 13
15. 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.Inject
public 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.G
roovyTemplateStub.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
16. Assets - js,cssの読み込み
server起動時に指定されたcssやjsファイルを読みに行くので
パスを間違えると全Controllerでエラーになる
@Assets({ @Asset(
(静的ファイルをタイプセーフに扱う為)
id = "myscript",
value = "javascripts/myscript.js")
})
package my.application;
使用するコントローラーのメソッドで
@View
使用するAssetのidを指定するとtemplateで書かなくても読み
@Route("/hoge/")
込まれる
@WithAssets("myscript")
public Response.Content hoge() throws IOException {
return hoge.ok();
}
Saturday, November 16, 13