arj*_*ads 44 forms scala playframework playframework-2.4
我是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)
Run Code Online (Sandbox Code Playgroud)
控制器:
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))}
)
}
}
Run Code Online (Sandbox Code Playgroud)
视图:
@(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>
}
}
Run Code Online (Sandbox Code Playgroud)
我安装了Play 2.4并使用IntelliJ Idea 14通过激活器模板创建了项目.
ps_*_*ttf 65
implicit messages向视图添加参数后,您只需添加以下导入并使用旧的控制器类甚至对象,而无需任何其他更改:
import play.api.Play.current
import play.api.i18n.Messages.Implicits._
Run Code Online (Sandbox Code Playgroud)
Rom*_*man 44
使用视图表单助手(例如@inputText)要求您将隐式play.api.i18n.Messages参数传递给视图.您可以(implicit messages: Messages)在视图中添加到签名中.你的观点变成了这样:
@(helloForm: Form[(String,Int,Option[String])])(implicit messages: Messages)
@import helper._
@main(title = "The 'helloworld' application") {
<h1>Configure your 'Hello world':</h1>
...
Run Code Online (Sandbox Code Playgroud)
然后在您的应用程序控制器中,您必须在范围中隐式提供此参数.最简单的方法是实现游戏的I18nSupport特性.
在您的示例中,这将如下所示:
package controllers
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import javax.inject.Inject
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi
import views._
class Application @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
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))}
)
}
}
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,您当然可以使用自己的实现MessagesApi.因为游戏知道开箱即用,MessagesApi你可以简单地注释你的控制器,@Inject然后让你为你做的工作.
正如Matthias Braun所说,你也必须设定
routesGenerator := InjectedRoutesGenerator
Run Code Online (Sandbox Code Playgroud)
在你的 build.sbt
有关I18n的更多信息,请参见https://www.playframework.com/documentation/2.4.x/ScalaI18N.
| 归档时间: |
|
| 查看次数: |
19962 次 |
| 最近记录: |