Learn Java for Web Development

(Tina Meador) #1
CHAPTER 8: Play with Java and Scala 373

To import the project, you can just repeat the same steps you performed earlier when generating the
project configuration for Eclipse in the helloworld Java application.


Now you can modify the index action in the application controller to display “Hello world,” as shown
in Listing 8-6.


Listing 8-6. Modifying the Index Action to Display “Hello world”


def index = Action {
Ok("Hello world")
}


A Basic CRUD Play 2 Java Application


In this section, you will learn to write a simple CRUD application that allows you to create, view, edit,
and delete books. For these operations, you need actions and URLs to invoke these actions. The
code for this application is available in a downloadable archive on the Apress web site.


Defining the Routes

The first step is to define routes for these operations in the conf/routes file, as illustrated in Listing 8-7.


Listing 8-7. Edit the Conf/Routes File




  1. Home page



  2. GET / controllers.Application.index()




  3. Books



  4. GET /books controllers.Application.books()

  5. POST /books controllers.Application.newBook()

  6. POST /books/:id/delete controllers.Application.deleteBook(id: Long)


   Line 5: In line 5 you create a route to list all books.
 Line 6: In line 6 you create a route to handle book creation.
 Line 7: In line 7 you create a route to handle deletion. The route to handle book
deletion defines a variable argument ID in the URL path. This value is then
passed to the deleteBook action.

Now if you refresh your browser, you will see that Play 2 cannot compile your routes file, as
illustrated in Figure 8-21.

Free download pdf