我刚开始玩 Play 2。我最近学习了如何创建模型 ( User
) 以及如何在视图中显示它(如果有任何不好的做法,请告诉我):
模型/用户.scala:
package models
case class User(id: Long, name: String)
object User {
var user = User(
id = 1L,
name = "Mark"
)
def greeting = TODO
}
控制器/Application.scala:
package controllers
import play.api._
import play.api.mvc._
import models.User
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def hello = Action {
Ok(views.html.hello(User.user))
}
}
你好.scala.html:
@(user: User)
@main("Welcome to Play 2.0") {
<h2>@user.id</h2>
<h3>@user.name</h3>
}
现在,我想在视图中显示函数的输出。
如何做到这一点?