Learn Java for Web Development

(Tina Meador) #1

378 CHAPTER 8: Play with Java and Scala


In Listing 8-12, the template takes two parameters. @content is a parameter that represents valid HTML
to be written in the body of the document. The type of content is Html, which is the Scala structure
that can be written as HTML when invoked by the template. The imported helper._ provides the form
creation helpers—that is, the form function that creates the HTML

with filled action and method
attributes, and the inputText function that creates the HTML input given as a form field.


Note The play.data package contains several helpers to handle HTTP form data submission
and validation.

Now you can implement the books() action, as illustrated in Listing 8-13.


Listing 8-13. Implementing the Books Action


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


The books() action renders a 200 OK result filled with the HTML, which is rendered by the index.
scala.html template called with the books list and the bookForm form.


You can now try to access http://localhost:9000/books in your browser (see Figure 8-23).


Figure 8-23. Displaying the book creation form


If you submit the book creation form, you will still get the TODO page. You need to implement the
newBook() action to be able to create the book. Listing 8-14 illustrates the implementation of the
newBook() action.

Free download pdf