Contenu connexe Similaire à 2018 8 18_play_framework Similaire à 2018 8 18_play_framework (20) 2018 8 18_play_framework3. 它是?
Play Framework 是一套 Web Framework
主要用 Java8 + Scala 語言所建構而成
最新版本 : 2.6.17
https://www.playframework.com/
9. M V C - 2
-app/controllers
(根據play的routes設定,去找尋要呼叫的controllers,回傳的
資料或資訊。)
-app/models
(商業邏輯部分)
-app/views
(頁面呈現結果,基本上使用scala所編譯)
12. Json & Form - 1
Play Json的library核心是Jackson,方便使用者直接對
Json資料格式轉換或印出
public Result nameJson(String name) {
Map<String, String> user = new HashMap<String, String>();
user.put("name", “TWJUG");
return ok(Json.toJson(user));
}
{
"name" : “TWJUG”
}
13. Json & Form - 2
Views
Controller
<form action="@controllers.routes.WebController.login.url"
method="post" id="loginForm">
<div class="field-wrap">
<label class="lable-field-wrap">電子信箱</label>
<input type="email" required autocomplete="off"
name="email"/>
</div>
</form>
// Step 2 : 取得登入請求
private AuthRequest getAuthRequest() {
try {
AuthRequest authRequest =
formFactory.form(AuthRequest.class).bindFromRequest().get();
play.Logger.info("authRequest = "+ Json.toJson(authRequest));
}
17. Views templates
Play views是由Scala所編譯,@代表是Play Scala作用的範
圍,可以import,引用public檔案,也可以使用寫好的Java
or Scala程式碼,直接在頁面上使用。
@views.html.web.header()
@views.html.web.headerLibs()
@(customer: Customer, orders: List[Order])
<h1>Welcome @customer.name!</h1>
<ul>
@for(order <- orders) {
<li>@order.title</li>
}
</ul>
@views.html.web.footer()
18. Dependency Injection
Out of the box, Play provides dependency injection support
based on JSR 330. The default JSR 330 implementation that
comes with Play is Guice.
Ch 9 Play MyBatis & MariaDB (DB) : https://bit.ly/2jIYPFz
Ch11-3 Login (AOP) : https://bit.ly/2I52lV7
使用JSR 330標準註釋 : https://bit.ly/2PacjIP
Key word : Guice AOP , Guice moudle