2016-06-07 47 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