【发布时间】:2015-08-28 06:45:47
【问题描述】:
我是 Play 框架的新手,并尝试在本地机器上模仿 helloworld 示例,但遇到了错误:
路线:
# Home page
GET / controllers.Application.index
# Hello action
GET /hello controllers.Application.sayHello
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
控制者:
package controllers
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import views._
class Application extends Controller {
val helloForm = Form(
tuple(
"name" -> nonEmptyText,
"repeat" -> number(min = 1, max = 100),
"color" -> optional(text)
)
)
def index = Action {
Ok(html.index(helloForm))
}
def sayHello = Action { implicit request =>
helloForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.index(formWithErrors)),
{case (name, repeat, color) => Ok(html.hello(name, repeat.toInt, color))}
)
}
}
查看:
@(helloForm: Form[(String,Int,Option[String])])
@import helper._
@main(title = "The 'helloworld' application") {
<h1>Configure your 'Hello world':</h1>
@form(action = routes.Application.sayHello, args = 'id -> "helloform") {
@inputText(
field = helloForm("name"),
args = '_label -> "What's your name?", 'placeholder -> "World"
)
@inputText(
field = helloForm("repeat"),
args = '_label -> "How many times?", 'size -> 3, 'placeholder -> 10
)
@select(
field = helloForm("color"),
options = options(
"" -> "Default",
"red" -> "Red",
"green" -> "Green",
"blue" -> "Blue"
),
args = '_label -> "Choose a color"
)
<p class="buttons">
<input type="submit" id="submit">
<p>
}
}
我安装了 Play 2.4,并通过 activator 模板使用 IntelliJ Idea 14 创建了项目。
【问题讨论】:
标签: forms scala playframework playframework-2.4