2016-06-07 49 views
1

我想制作play2-auth标准样本,不包括授权 - 只是验证/登录。制作工作play2-auth标准样本

我的形式收到以下错误在@helper.form行:

类型不匹配;实测值:play.api.mvc.Action [play.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) 
    ) 
    } 

} 

回答

1

您当前传递给表单模板帮助器Action实例为authenticate,而不是Call实例,它应该由reverse route生成。 A Call描述了您的路由文件中定义的操作的URL和方法,因此您无需在任何地方对其进行硬编码。

首先,把你的ManageSession控制器在controllers包(根据播放的默认设置 - 你可以将它后来却我认为反向路径产生依赖于他们在那里,或子包):

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> 

:在播放2.5及以上,静态(对象)控制器气馁并且不与默认工作(喷射)路线发生器。在这种情况下,最简单的方法是只是为了让ManageSession一类,而不是一个对象:

class ManageSession() extends Controller with LoginLogout with AuthConfigImpl { 
    ... 
} 

或者,你可以指定你想要在你的build.sbtStaticRoutesGenerator

routesGenerator := StaticRoutesGenerator 
+0

感谢。我得到一个错误'价值ManageSession不是对象controllers.routes'的成员,任何想法可能是什么原因? ManageSession是在包控制器 – ps0604

+0

@ ps0604您使用哪个版本的Play? – Mikesname

+0

2.5.2使用eclipse scala ide – ps0604