Let me walk you through a very simple example of using Ratpack inside Grails.
We start by enabling the Ratpack plugin in our application. I called mine app "example" so all the URLs will be prefixed with "/example". To include the plugin you need to add it to
grails-app/conf/BuildConfig.groovy
like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
runtime ":hibernate:$grailsVersion" | |
runtime ":jquery:1.7.1" | |
runtime ":resources:1.1.5" | |
build ":tomcat:$grailsVersion" | |
compile ":ratpack:1.0.1" | |
} |
grails-app/domain/Person.groovy
:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
String firstName | |
String lastName | |
} |
grails-app/ratpack
and inside of it a file called PersonRatpack.groovy
:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonRatpack { | |
def urls = { | |
get("/") { | |
render "list.html", [ people: Person.list() ] | |
} | |
} | |
} |
Config.groovy
:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grails.plugin.ratpack.templateRoot='grails-app/ratpack/templates' |
grails-app/ratpack/templates/list.html
:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>People</title> | |
</head> | |
<body> | |
<h1>People</h1> | |
<ul> | |
<% people.each { person -> %> | |
<li>${person.firstName} ${person.lastName}</li> | |
<% } %> | |
</ul> | |
</body> | |
</html> |
Anyways, with all that in place we can add some people to the database in our
grails-app/conf/BootStrap.groovy
file like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def init = { servletContext -> | |
new Person(firstName: "John", lastName: "Doe").save(flush: true) | |
new Person(firstName: "Jane", lastName: "Smith").save(flush: true) | |
} |
Needless to say you can accomplish the exact same thing using a controller and a view. The idea here however is to show you that there is life beyond the standard artifacts and that the beauty lies in diversity!
Hope this will help someone get started with this awesome piece of code!
5 comments:
This solution doesn't work when deploying the application as WAR file. Does anyone knows how to make it work in deployment?
Maybe a build dependency instead of compile one? But it's just a wild unverified guess.
I wanted to tell something else. I've learned about this integration today too and as far as I see the case for using Ratpack with pure Groovy to build a very simple webapp exposing for example 1 or 2 services through the REST API and not needing the full Grails stack, at the same time I don't actually get why would I need that in Grails?
I don't find diversity as an advantage. Much more I prefer convention especially if using separate URL mappings and templates instead of views may cause unwanted mess in the project.
I can actually think about one case which is exposing a REST API through Ratpack for a fully featured application where base controllers provide normal workflow based on views and Ratpack can provide a eparate RESTfull layer in the /api/* URL space and reuse the common services.
But I'm not yet sure if this would be the best solution in Grails. Anyway all the lightness of Ratpack is lost in Grails. What do you think? I'd love to hear about hat is it good for in Grails?
See there are different technologies available on the OpenSource market today. There's Wicket, JSF(2), Sinatra and imitators. I think they all have their place in the overall landscape. What I find particularly interesting is having one base to build upon (Grails) with additional technologies supporting all the goodness whatever way they can - and wherever it makes sense.
Like I said in the blog at first you could easily do the exact same thing using a standard controller and a view - but somehow I find Ratpack amusing enough to try it out...
Great post - thanks for doing the writeup. I released version 1.0.1 which fixes the problem with templates in a war file - see the plugin page for how to configure it: http://grails.org/plugin/ratpack
@Burt Thanks! I've updated the gist with example.
Post a Comment