2
votes

I'm reading the book Learn Java for Web Development and trying to execute code examples from the book.

I have a problem with Play Framework code example (chapter 8).

Exception:

"render(java.util.List,play.data.Form) in views.html.index cannot be applied to (java.lang.String)"

Controller class :

final static Form<Book> bookForm = Form.form(Book.class);  

public static Result books(){       
    return ok(views.html.index.render(Book.all(),bookForm));            
}

View - index.scala.html

@(books: List[Book], bookForm: Form[Book])

@import helper._

@main("books") {

<h1>@books.size() book(s)</h1>

<ul>
    @for(book <- books) {
        <li>
            @book.label

            @form(routes.Application.deleteBook(book.id)) {
                <input type="submit" value="Delete">
            }
        </li>
    }
</ul>

<h2>Add a new book</h2>

@form(routes.Application.newBook()) {

    @inputText(bookForm("label")) 

    <input type="submit" value="Create">

 }

}

Edit:

1) I just installed latest play version from scratch on Windows 8 machine, for the moment it's Play 2.3.1 . I didn't any modifications for Play framework . It's my first play project

2) Full error (in Eclipse): The method render(String) in the type index is not applicable for the arguments (List<Book>, Form<Book>)

3) When I try to refresh my browser , code compiled and I can see Book form on a screen

1
Please clarify: 1) That example is for Play 2.2.0 and your question is about Play 2.3.1. Which modifications have you done to upgrade the version? 2) Please post the complete error message including the file name and line of the error. 3) If the error is in the ApplicationTest file, just delete that file; it seems to be a leftover from the default application template that is not correct in this project. - Fernando Correia

1 Answers

3
votes

Even though you have Play 2.3.1 installed in your machine, this particular project references Play 2.2.0 in this line in the project/plugins.sbt file:

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")

So it's using Play 2.2.0, which will be downloaded by sbt, and not the Play version installed elsewhere in your machine.

In addition to the error message, Eclipse also shows the file name and line number. It should open that file if clicking on the error message. In that project, it's probably the test/ApplicationTest.java file, which has this line:

Content html = views.html.index.render("Your new application is ready.");

This is part of the default new application template and it's supposed to be modified. This particular line does not compile because the index view expects as arguments List<Book>, Form<Book> and not a String. This is stated in this line of the views/index.scala.html file:

@(books: List[Book], bookForm: Form[Book])

For this example project, the solution is to delete the file test/ApplicationTest.java, since it's not really adding value because this test does not work with this project. In a production project, this file should be updated so that it compiles and passes the test.

The project runs despite this error because that code is only executed for the test target. Eclipse will show errors for all targets, though.