Skip to content

Instantly share code, notes, and snippets.

@tlberglund
Created December 5, 2013 13:49
Show Gist options
  • Save tlberglund/7805420 to your computer and use it in GitHub Desktop.
Save tlberglund/7805420 to your computer and use it in GitHub Desktop.

Ratpack Workshop

1. Hello World

  • Unzip the hello-world.zip file into your project directory
  • Write the initial Ratpack.groovy

2. Beginning Templates

  • Unzip the public.zip file into src/ratpack.
  • Add a static assets declaration to Ratpack.groovy.
  • Write message.html in the src/ratpack/templates directory. The content should look like this:
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>${model.title}</title>
    <link rel="stylesheet" href="/js/jquery-ui.min.css"/>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/jquery-ui.min.js"></script>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container">
    <h1>${model.message}</h1>
  </div>
</body>
  • Add code to Ratpack.groovy to render the new remplate. Remember to pass in appropriate model data.

3. A Static Model

  • Add a Book.groovy class in src/main/groovy. Follow conventional Java/Groovy packaging structure. Give the class id, title, and content properties.
  • Create a few statically intialized instances of Book in Ratpack.groovy.
  • Pass the static books to the template.
  • Unzip static-book-templates.zip into src/ratpack/templates

4. A Read-Only Book Database

  • Inject the database modules into Ratpack.groovy. The following code snippets may help:
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
import ratpack.example.books.Book
import ratpack.groovy.sql.SqlModule
import ratpack.h2.H2Module
    modules {
        register new H2Module()
        register new SqlModule()
    }
  • Convert the static book model into actual database queries. The follwing two queries will help initialize the data (you can execute them with sql.executeInsert() and sql.executeUpdate()):
create table if not exists books (id int primary key auto_increment, title varchar(255), content varchar(255))
merge into books (id, title, content) key (id) values (0, 'Book 1', 'Stuff')
  • To query books, you can use the sql.rows() method, then turn the result into a collection of Book objects. The SQL query for reading the books is left as an exercise for the student.

5. Creating Books

  • Remove the code that statically initializes the book data.
  • Add a handler block for the create URI path. The create path should respond to GET and POST verbs. Use the byMethod method to contain both of these handlers.
  • The GET handler should render the create.html template.
  • The POST handler should parse the input form, create a new book with sql.executeInsert(), and redirect to a URL that displays the new book. (NOTE: a handler for this URL already exists.)
  • If you get stuck, here is a hint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment