Scala Play:对象索引不是包views.html的成员

时间:2016-08-31 15:15:32

标签: scala playframework

我正在

object index is not a member of package views.html

但与此主题上打开的所有其他线程不同,我的问题似乎与IDE完全无关。无论我多少尝试通过运行

来清理和重建,我都从命令行(没有IDE)得到这个问题
activator clean compile run

或只是

sbt clean compile

这是我的conf / routes:

GET      /                   controllers.Application.index
GET      /books              controllers.Application.listBooks
POST     /books              controllers.Application.upload

GET     /assets/*file        controllers.Assets.at(path="/public", file)

这是我的观点/ index.scala.html:

@import play.api.data.Form
@import models.Book

@(form: Form[Book])(implicit messages: Messages)

<!DOCTYPE html>
<html>
    <head>
        <title>xxx</title>
        <link rel="stylesheet" type="text/css" media="screen" href='@routes.Assets.at("stylesheets/main.css")'>
        <script type="text/javascript" href='@routes.Assets.at("javascripts/jquery-1.9.0.min.js")'></script>
    </head>
    <body>

        <div class="screenshot">

            <div class="navbar navbar-fixed-top">
                <div class="navbar-inner">
                    <div class="container">
                        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>
                        </a> <a class="brand" href="#">Workday Dublin CTF</a>

                        <div class="nav-collapse">
                            <ul class="nav">

                            </ul>
                        </div>
                    </div>
                </div>
            </div>

                <h1>All Your Books Are Belong To Us</h1>
                <div class="container">

                    <h2>Add Book</h2>
                    @helper.form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {

                        @helper.inputText(form("title"))
                        @helper.inputText(form("author"))
                        @helper.inputFile(form("myUpload"))

                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary">Create Book</button>
                        </div>
                    }

                </div>
        </div>
    </body>
</html>

最后,在我的controllers / Application.scala:

中,抛出错误的位置
package controllers

import models.Book
import models.Book._
import play.api.mvc._

class Application extends Controller {

  def index = Action {
    Ok(views.html.index(Book.form))
  }

  def listBooks = Action {
    Ok(books.head.myUpload)
  }

  def upload() = Action(parse.multipartFormData) { request =>
    request.body.file("myUpload").map { myUpload =>
      import java.io.File
      val filename = myUpload.filename
      val contentType = myUpload.contentType
      myUpload.ref.moveTo(new File(s"/tmp/$filename"))
      addBook(Book("xxxtitle", "xxxauthor", filename))
      Ok("File uploaded at /tmp/"+filename)
    }.getOrElse {
      Redirect(routes.Application.index).flashing(
        "error" -> "Missing file")
    }
  }
}

抛出错误(views.html.index(Book.form))引用models / Book.scala:

package models

import play.api.data.Form
import play.api.data.Forms._

case class Book(title: String, author: String, myUpload: String)

object Book {

  var books = List(Book("title test 1", "author test 1", "filename test 1"))

  def addBook(book: Book) = books = books ::: List(book)

  val form = Form(mapping(
    "title" -> text,
    "author" -> text,
    "myUpload" -> nonEmptyText)(Book.apply)(Book.unapply))
}

由于我已经对此进行了很多研究,到目前为止还没有其他解决方案可行,所以任何帮助都会受到极大的赞赏。非常感谢你!

2 个答案:

答案 0 :(得分:1)

Apparently, even in this case the issue was related to the IDE. Despite seemingly unrelated, given it occurred from the command line and even after applying all the changes suggested by others.

I created a new project -> Play 2.x in IntelliJ IDEA 2016.2.2. Then I copy/paste all the content from the old faulty project, within in the new project structure. And everything worked straight away, although I had to add

import play.api.Play.current
import play.api.i18n.Messages.Implicits._

to Application.scala in order to pass the implicit messages (as @curious spotted in the comments). I did not mark his comment as the answer because when I tried to add these imports in the faulty project, I was still getting the same error. So nope, the fix would be something to do wth the IDE settings which I still am not too sure about. Surely my workaround is good enough for me, though.

答案 1 :(得分:1)

删除文件夹 / project / target 中的所有内容并在终端上构建: sbt compile