我正在尝试使用play2-auth标准示例,不包括授权--只是身份验证/登录。
我在@helper.form行中获得了以下错误:
类型错配;发现: play.api.mvc.Actionplay.api.mvc.AnyContent必需: play.api.mvc.Call
@(form: Form[Option[session2.Account]])(implicit flash: Flash)
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
@helper.form(session2.ManageSession.authenticate) { // <-- here's the error
<h1>Sign in</h1>
@form.globalError.map { error =>
<p class="error">
@error.message
</p>
}
@flash.get("success").map { message =>
<p class="success">
@message
</p>
}
<p>
<input type="email" name="email" placeholder="Email" id="email"
value="@form("email").value">
</p>
<p>
<input type="password" name="password" id="password" placeholder="Password">
</p>
<p>
<button type="submit" id="loginbutton">Login</button>
</p>
}
</body>
</html>这是ManageSession.scala:
package session2
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import jp.t2v.lab.play2.auth._
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> text, "password" -> text)(Account.authenticate)
(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(views.html.login(loginForm))
}
def logout = Action.async { implicit request =>
// do something...
gotoLogoutSucceeded
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(views.html.main())),
user => gotoLoginSucceeded(user.get.email)
)
}
}发布于 2016-06-07 11:52:18
当前,您正在向表单模板助手传递用于Action的authenticate实例,而不是由其反向路径生成的Call实例。Call描述操作的URL和方法,就像在路由文件中定义的那样,所以您不必在任何地方都硬编码它。
首先,将ManageSession控制器放在controllers包中(根据Play的默认值--您可以稍后移动它,但我认为反向路由生成依赖于它们在那里,或者在子包中):
package controllers
...
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
...
}您的conf/routes文件应该如下所示:
POST /authenticate controllers.ManageSession.authenticate然后反向路由Call对象将在controllers.routes.ManageSession.authenticate中生成。
@helper.form(controllers.routes.ManageSession.authenticate) {
...
}注意里面的routes子包!
结果应该是生成如下所示的HTML:
<form method="POST" action="/authenticate">
...
</form>备注:在Play 2.5和更高版本中,静态(对象)控制器被劝阻,并且不使用默认(注入)路由生成器。在这种情况下,最简单的选择是使ManageSession成为类而不是对象:
class ManageSession() extends Controller with LoginLogout with AuthConfigImpl {
...
}或者,您可以指定希望在StaticRoutesGenerator中使用build.sbt。
routesGenerator := StaticRoutesGeneratorhttps://stackoverflow.com/questions/37677960
复制相似问题