Playframeworkのデモを試してみるその7 - CRUDモジュール

モジュールとはまぁ要するにプラグイン的なアレ
CRUD モジュールとは、 CRUD (Create, Read, Update, Delete)
の機能を簡単に作り上げる素敵な機能。

Tasks コントローラを作成

新しい Controller (クラス) を作成する

app/controllers/Tasks.java

package controllers;

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

import java.util.*;

import models.*;

public class Tasks extends CRUD {

}

CRUD ってクラスを継承したコントローラ、特に実装はない。
eclipse
CRUD cannot be resolved to a type
ってエラー出してるけど無視(後で解決する)

ルーティングの設定

http://localhost:9000/admin ってリクエストがあったときに
CRUD 機能を利用した画面に飛ばしたいので URL ルーティングの設定を書く
conf/routes

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                                       Application.index
GET     /admin                                  Tasks.list

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}
実行確認 (失敗)

とりあえずデモどおりに実行してみる
eclipseeclipse/Tasks.launch を右クリック => Run As => Tasks

「エラーがあるけど実行して良い?」と聞いてくるがとりあえず無視して Proceed

ブラウザで http://localhost:9000/admin を実行してみる・・・・とエラー。
案の定
The file /app/controllers/Tasks.java could not be compiled. Error raised is : CRUD cannot be resolved to a type

CRUD モジュールの有効化

デモでは特に設定していないが、 conf/application.conf を見ると
crud モジュールの有効化がされていないのが確認できる。
Additional modules の辺り

# Additional modules
# ~~~~~
# A module is another play! application. Add a line for each module you want
# to add to your application. Modules path are either absolutes or relative to
# the application root.
#
#module.crud=${play.path}/modules/crud
#module.secure=${play.path}/modules/secure
#module.ecss=${play.path}/modules/ecss

これを有効化(コメントアウトと取る)しておく

# Additional modules
# ~~~~~
# A module is another play! application. Add a line for each module you want
# to add to your application. Modules path are either absolutes or relative to
# the application root.
#
module.crud=${play.path}/modules/crud
#module.secure=${play.path}/modules/secure
#module.ecss=${play.path}/modules/ecss
再び実行確認

conf/application.conf の変更は Web サーバの再起動が要るので
一旦 Webサーバを終了させる、そして再び実行を確認する。
eclipseeclipse/Tasks.launch を右クリック => Run As => Tasks
未だに「エラーがあるけど実行して良い?」と聞いてくるが無視して Proceed

ブラウザで http://localhost:9000/admin を実行してみる・・・・と今度は OK!
Administration ってタイトルの Tasks 画面が表示されている

ちなみに、事前に Web サーバを再起動させていると、1件も表示されていなくて寂しいので
http://localhost:9000 の画面でいくつかタスクを登録しておくと幸せになれる。

しかし気分が悪い

この時点で正常に機能しているのに、 eclipse の方ではエラーが出っぱなし
これを解決する

再度 eclipsify

eclipse の package explorer から Tasks を右クリック => Open External => Command Prompt
コマンドラインプロンプトで play eclipsify を実行

# play eclipsify
~        _            _
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/
~
~ play! 1.0.1, http://www.playframework.org
~
~ OK, the application is ready for eclipse
~ Use File/Import/General/Existing project to import C:\eclipse\workspace\Tasks
into eclipse
~
~ Use eclipsify again when you want to update eclipse configuration files.
~ However, it's often better to delete and re-import the project into your workspace since eclipse keeps dirty caches...
~

コマンド発行後、 eclipse の package explorer から Tasks を右クリック => Refresh
しばらく経つと Tasks.java にあったエラーは消えている。

要するに eclipse 側の Build Path 設定、 Source folder で CRUD のソースが足らなかった模様。

CRUD機能に手を加える

表示が変

http://localhost:9000/admin 画面で表示されているタスクの一覧は
各タスクが Task[1] とか Task[2] とか残念な名前で表示されている。
せめて、タスクのタイトルが表示されていて欲しい

Tasks モデルで toString メソッドをオーバーライド

app/models/Task.java

package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;

@Entity
public class Task extends Model {

    public String title;
    public boolean done;

    public Task(String title) {
        this.title = title;
    }

    public String toString() {
        return title;
    }
}

toString メソッドを追加した。
先ほどの CRUD 画面で表示されている文字列の正体は
toString メソッドの結果値なのでこれをオーバーライドする

確認

http://localhost:9000/admin を再度確認する
先ほどの Task[1] とか Task[2] とかの残念な名前が
My first task とか My second task とかの名称で表記されて入れば成功。