Exploring Liftを写経しつつ読む。その1

1.3までは適当でOK。

1.4 Your First Lift Application

環境構築

書いてあることを参考に環境を作るが、詳細は省略。

私の初めてのLiftアプリ

mavenは使ったことがないのでコマンドラインベースでやってみる。
NetBeansプラグインを使えば楽できるかも。

プロジェクトの雛形を作る。下記をコマンドラインで実行。

mvn archetype:generate -U -DarchetypeGroupId=net.liftweb -DarchetypeArtifactId=lift-archetype-blank -DarchetypeVersion=1.0 -DremoteRepositories=http://scala-tools.org/repo-releases -DgroupId=demo.helloworld -DartifactId=helloworld -Dversion=1.0-SNAPSHOT

とりあえず簡単なサンプルができたようなので、下記を実行してサーバー起動。

cd helloworld
mvn jetty:run

実行中、色々とダウンロードするので気長に待つ。かなり待つ。

そして

[INFO] Started Jetty Server

と出たらhttp://localhost:8080/にアクセス。

”Welcome to your project!”と現在日時が表示されればOK。

ソースを読む

src/main/webapp/index.htmlを見るとこんな感じ。

<lift:surround with="default" at="content">
  <h2>Welcome to your project!</h2>
  <p><lift:helloWorld.howdy /></p>
</lift:surround>

Liftは、liftプレフィックスがついたエレメント()に特殊な処理を施す。詳細は3.6でやるらしい。

LiftがXMLテンプレートを処理する際には、最も外側のエレメントから内側に向かって処理する。
この場合、最も外側のエレメントは

エレメントでは、Liftはwith属性で指定されたテンプレート(この場合、default)を用いて、要素の内容をテンプレートに設定する。
その際、at属性によって、テンプレートのどこの部分に内容を設定するかを指定する。

src/main/webapp/templates-hidden/default.html

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<meta name="description" content="" />
		<meta name="keywords" content="" />
		
		<title>demo.helloworld:helloworld:1.0-SNAPSHOT</title>
		<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>
	</head>
	<body>
		<lift:bind name="content" />
		<lift:Menu.builder />
		<lift:msgs/>
	</body>
</html>

エレメントはindex.htmlファイルがどこに束縛される(挿入される)かを決定する。
name属性は、(src/main/webapp/index.htmlの)エレメントのat属性と一致しなければならない。


などなど。