Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

15 September 2009

Cool things with Grails URL mapping

In Grails you have a lot of nice features for doing web development. One of them is URL mapping with the link tag.
Grails default mapping is this:
"/$controller/$action?/$id?" {}
This will map the URL 'http:localhost:8080/myapp/book/show/1' to the action show in the BookController class. To create such links you can use the link tag like this:
<g:link controller="book" action="show" id="book.id">
  ${book.title}
</g:link>
Sure this is the standard way. But now lets change the URL mapping by replacing the default mapping:
"/$book/$id?"(controller: "book", action: "show")
This will tell Grails to map the URL 'http:localhost:8080/myapp/book/1' to the same show action as above, but what's happening with our link?
It's stil working! Grails knows, that URLs, which are pointing to BookControllers's show action have to be of the declared structure and thus, the link tag will create a URL like 'http:localhost:8080/myapp/book/1'.

Ok, let's go on. What else can we do with this?
By default the URL 'http:localhost:8080/myapp/book/list?year=2009' will point to the list action of BookController and the URL parameter year will be available with
params.year
in the action. To create this URL you can use the link tag again:
<g:link controller="book" action="list" params="[year: "2009"]">
  Books released in 2009
</g:link>
Now let's change our mapping again by adding:
"/$books/$year?"(controller: "book", action: "list")
Well, now the above link will create the URL 'http:localhost:8080/myapp/books/2009' and everything else will stil work as expected.

11 November 2008

Now it's official

Graeme has just posted it.
Congratulations to the G2One guys.

SpringSource has bought G2One

I just read it, but I'm not sure if it is realy true, what The Register writes today. G2One is the company of Graeme Rocher and the other guys behind Groovy and Grails. SpringSource is the company behind the awesome Spring framework, which is also the base of Grails.
Hopefully there will come a lot of benefit from this acquisition for Grails. My biggest wish is, that they will bring Grails to Spring Dynamic Modules for OSGi and thus onto a great OSGi infrastructure. This would also fix the most voted issue for Grails.

01 November 2008

Groovy for Java Testing

One thing I like so much on YouTube is the mass of cool videos of talks, provided there. So have a look to this cool video:



It gives a little introduction to Groovy, it's integration to Java and it's awesome usabillity as a language for testing your Java code.
The speaker is Andres Almiray and you'll find his own blog here:
Andres Almiray's Weblog

05 June 2008

Grails/GWT Tutorial I - The first steps.

After someone was asking for a basic tutorial and samle application for using the Grails GWT plugin on the Grails mailing list, I've just written one.
You can read it under http://code.google.com/p/derjanandhisblog/wiki/GWTGrailsTutorial.
It should show you, how to create a domain model, a service for RPC calls and a very basic GWT client, which calls data from the Grails app and displays it.

Hopefully I'll find enough time to extend the tutorial and the application in the next weeks, to make it real book store ;-)

22 May 2008

Grails is only Java EE with Spring

Grails is defintivly a cool framework for developing nice Java EE web apps. you can do a lot of tasks with just one step and the rest ist automatically done by Grails.
But using your existing Java code in a Grails application is no problem. Grails is based on Spring and Groovy and thus there are some easy ways to embbed Java code to your app.
  1. You can create a normal Grails controller or service and import your Java classes the normal way. This works pretty easy, if you're developing your app mainly with Groovy.
  2. If your app is allready based on Spring and thus provides allready Spring services, you can embbed'em directly and reference them in your Grails controller or services.
The second way can easyly be done, using some XML for configuration, but there is also the Groovy way to do, by using Grails' Spring Bean Builder.
To use it, you can put some Groovy code to the PROJECT_HOME/grails-app/conf/spring/resources.groovy file.
The following code shows you an example resources.groovy file:
import com.duckmaps.server.imagerendering.services.ImageServiceImpl

beans = {

imageService(ImageServiceImpl) {bean ->
bean.scope = "session"
}

}
Here you're seeing, that a bean with name imageService and the class ImageServiceImpl is defined. By setting the property scope to session, Spring instantiates this class for each session. It is important to put the import statement to the top. If you forget it, your code won't work.
Referencing this bean is the same as XML declared beans. Just put a
def imageService
to your controller and Grails will inject it like a normal Grails controller.