sohatach's blog

http://github.com/soha

play framework 2.0.3が自動生成するファイル考察

1.testprojという名前で新規プロジェクトを作成してみる。

play new testproj

C:\Scala\workspace>play new testproj
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.0.3, http://www.playframework.org

The new application will be created in C:\Scala\workspace\testproj

What is the application name?
> testproj

Which template do you want to use for this new application?

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application testproj is created.

Have fun!

以下のファイルが生成されていました。

.gitignore
README
app\controllers\Application.scala  //アプリケーションコントローラ
app\views\index.scala.html  //indexテンプレートファイル
app\views\main.scala.html  //mainテンプレートファイル
conf\application.conf  //アプリケーション設定ファイル
conf\routes  //URLルーティング定義ファイル
project\Build.scala  //sbtビルド定義ファイル
project\build.properties  //使用するsbtのバージョン指定ファイル
project\plugins.sbt  //sbtビルド定義の拡張プラグイン定義ファイル
public\images\favicon.png
public\javascripts\jquery-1.7.1.min.js
public\stylesheets\main.css


playでは、sbtというScalaのビルドシステム(JavaMavenみたいなもの)が使われています。
参考 始めるsbt:http://scalajp.github.com/sbt-getting-started-guide-ja/

生成されたファイルの中身は以下のようなものでした。

app\controllers\Application.scala

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {
  
  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }
  
}

app\views\index.scala.html

@(message: String)

@main("Welcome to Play 2.0") {
    
    @play20.welcome(message)
    
}

app\views\main.scala.html

@(title: String)(content: Html)

<!DOCTYPE html>

<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>

conf\routes

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

# Home page
GET     /                           controllers.Application.index

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

 

2.Eclipse用プロジェクトファイルの生成

1で生成されたtestprojディレクトリに移動し、

play eclipsify

を実行する。

.classpath
.project
.settings\org.scala-ide.sdt.core.prefs

が生成される。

ここでEclipse用のビルドパスの設定ファイル.classpathに様々な依存ライブラリのjarファイルに加え

<classpathentry path="target\scala-2.9.1\src_managed\main" kind="src"></classpathentry>

というクラスパスエントリーがある。
これはプロジェクトディレクトリのtarget下にあるファイルのため、不用意に消してしまうとEclipse上ビルドエラーになってしまいます。
ただし、このtarget下ファイルは、.gitignoreでコミット対象ファイルからも外されています。
どうもplay runでプロジェクト実行時に自動生成されているようです。

 

3.プログラムの処理の流れ

 1.ブラウザからのリクエストを受け付ける。

 2.conf/routesの定義に基づきアプリケーションコントローラのメソッドが呼ばれる。

  例:GET     /                           controllers.Application.index

  /へのリクエストについては、controllers.Applicationクラスのindexメソッドが呼ばれる。

 3.アプリケーションコントローラメソッド内でレスポンスを返す。

  例:Ok(views.html.index("Your new application is ready."))

  上記の例では、index.scala.htmlテンプレートファイルが出力される。
  index.scala.htmlテンプレートファイルでは、

  @main("Welcome to Play 2.0") {

  により、さらにmain.scala.htmlテンプレートファイルを画面の部品としてインクルードしている。

  @main("Welcome to Play 2.0") {
     @play20.welcome(message)
  }

についてもう少し。
@mainは、main.scala.htmlというファイル名のテンプレートファイルをインクルードするという意味で
("Welcome to Play 2.0")は、、main.scala.htmlに渡す第1引数title、{}の中で定義されているものが第2引数contentに対応します。