Play2:参数flash的隐式值编译错误

时间:2014-04-14 20:51:31

标签: scala playframework-2.0

我从Play 2和Scala开始,我创建了一个非常简单的登录表单。一切都完美无缺,直到我用flash来存储登录结果。我获得的只是错误:

/home/denis/dev/workspace/atabank/app/views/login.scala.html:5: could not find implicit value for parameter flash: play.api.mvc.Flash

根据Play文档,我输入了我的控制器implicit request => ...,我使用Ok(...).flashing("error" -> "wrong password"),但错误仍在此处...

这是我的代码:

登录控制器:

package controllers

import play.api.data.Form
import play.api.data.Forms.{ mapping, nonEmptyText }
import play.api.mvc.{ Action, Controller, Flash }

object Login extends Controller {

def login = Action { implicit request =>
    Ok(views.html.login())
}

def authenticate() = Action { implicit request =>
    {
    val form = loginForm.bindFromRequest()

    form.fold(
        hasErrors = {
        form =>
            Redirect(routes.Login.authenticate()).flashing("error" -> "Connection error")
        },
        success = {
        userInfo =>
            if ("123456".equals(userInfo.password))
                Redirect(routes.Home.show(userInfo.login)).flashing("success" -> "Successfully connected")
            else
                Redirect(routes.Login.login()).flashing("error" -> "Wrong password")

        })
    }
}

val loginForm: Form[LoginInfo] = Form {
    mapping(
        "login" -> nonEmptyText,
        "password" -> nonEmptyText)(LoginInfo.apply)(LoginInfo.unapply)
    }

}

case class LoginInfo(login: String, password: String)

login.scala.html:

@()(implicit flash: Flash, lang: Lang)
@import helper._
@import controllers.Login._

@main("Login") {

    <div class="container">

        @helper.form(action = routes.Login.authenticate(), 'role -> "form", 'class -> "form-signin") {
            <h2 class="form-signin-heading">Please sign in</h2>
            <input type="email" name="login" class="form-control" placeholder="Email address" required autofocus>
            <input type="password" name="password" class="form-control" placeholder="Password" required>
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        }    
    </div>

}

main.scala.html:

@(title: String)(content: Html)(implicit flash: Flash, lang: Lang)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <!-- CSS, JS -->
    </head>
    <body>
        <div class="container">
            @if(flash.get("success").isDefined) {
                <div class="alert alert-success">
                    @flash.get("success")
                </div>
            }
            @if(flash.get("error").isDefined){
                <div class="alert alert-error">
                    @flash.get("error")
                </div>
            }
            @content
        </div>

    </body>
</html>

家庭控制器:

package controllers

import play.api._
import play.api.mvc.{ Action, Controller }
import play.api.mvc.Flash

object Home extends Controller {

    def show(username: String) = Action { implicit request =>
        Ok(views.html.home(username))
    }

}

home.scala.html:

@(username: String)(implicit flash: Flash, lang: Lang)

@main("Home page") {
    <h1>Welcome Mister @username</h1>
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

我发现了这个错误的另一个原因:模板中的右括号后面必须没有空格:

@() (implicit flash: Flash, lang: Lang) - &gt;错误

@()(implicit flash: Flash, lang: Lang) - &gt;没错!

答案 1 :(得分:0)

您在控制器中隐含的是RequestHeader,而不是Flash。您可以通过在视图中执行类似操作来检索请求中的Flash:

@request.flash.get("success")

它应该是一个选项,因此您应该在其上进行映射,以便处理该值(如果它在此处。)

答案 2 :(得分:0)

好的,我正在寻求解决方案。 如果我在表单中添加@request.flash.get("success"),则会收到错误:not found: value request。 经过一些试验和错误后,我解决了我的问题:我必须在我的表单@import controllers.Login._和我的登录控制器中删除,将import play.api.mvc.{ Action, Controller, Flash }替换为import play.api.mvc.{ Action, Controller }。删除这两个导入后,项目将编译并运行。

我不是Scala的专家,但我想这是我隐式导入两次flash,所以编译器无法选择。

感谢。