Blog

Play Frameworkを使ってみたのでメモ

  • Update2014-08-03
  • Category
    • Backend
  • Other--
Play Frameworkを使ってみたのでメモのサムネイル画像

Play FrameworkはMVCアーキテクチャを採用しているJavaベースのWebアプリケーションフレームワークです。JavaベースのWebアプリケーションフレームワークと言えばStrutsとかが有名ですが、Play FrameworkはRuby on Railsの高い生産性に強く影響されているフレームワークとなっているようです。

(参考)Javaの常識を変えるPlay framework入門

なので、ちょっとMacにインストールしてみたので以下メモ。

目次

インストール

■公式サイト
http://www.playframework-ja.org/

■インストール
brewでインストールできるとのことなのでやってみる。(brewって何?という方はこちら)

brew install play

すると・・・

Error: No available formula for play
Play 2.3 replaces the play command with activator:
  brew install typesafe-activator

You can read more about this change at:
  http://www.playframework.com/documentation/2.3.x/Migration23
  http://www.playframework.com/documentation/2.3.x/Highlights23

お?コマンド変わったのね。
ならば

brew install typesafe-activator

ちょっと時間かかるけど無事インストール完了する。

セットアップ

カレントディレクトリを開発ディレクトリにして以下を実行

activator new

実行すると以下レスポンスが帰ってくる

Fetching the latest list of templates...

Browse the list of templates: http://typesafe.com/activator/templates
Choose from these featured templates or enter a template name:
  1) minimal-java
  2) minimal-scala
  3) play-java
  4) play-scala
(hit tab to see a list of all templates)
> 3
Enter a name for your application (just press enter for 'play-java')
> test
OK, application "test" is being created using the "play-java" template.

To run "test" from the command line, "cd test" then:
/[project dir]/activator run

To run the test for "test" from the command line, "cd test" then:
/[project dir]/activator test

To run the Activator UI for "test" from the command line, "cd test" then:
/[project dir]/activator ui

実行

実行(activator newで作成されたディレクトリをカレントディレクトリにしてから)

activator run #初回はちょっと時間かかる

初回アクセスはコンパイルがあるので時間がかかるがhttp://localhost:9000/にアクセスすると以下Welcomeページが表示される。
Welcome_to_Play

※カレントディレクトリのパスに日本語が入ってるとencording errorになるので注意

activator uiを実行すると・・・

activator ui

おおなんだこれ!ブラウザエディタが立ち上がった。
Typesafe_Activator

Hello World

app/controllers/Application.javaを編集してHello World

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        //return ok(index.render("Your new application is ready."));
        return ok("Hello World!");
    }

}

ブラウザ上でできるので楽ですね。PlayFrameworkを解説している記事は漁ったのですが、ブラウザエディタが出る前のものが多く、アップデートで随分変わったらしい。ちょっと遊んでみようと思う。