I thought i would just start a new post about some of my thoughts on why i am using Groovy and Grails. This has been spun out from a few comments on a previous post of mine from Ricky and Greg.
So Admittedly i am fairly ignorant of other languages outside those with a C lineage. Eg C,C++,Java,C#,groovy
I generally don't get excited about just a language alone. Because the language for me is just one part of the puzzle to my whole development experience.
If i was building a house i see the language as the cement. I could (maybe) build a whole house out of cement but that would be crazy. Rather i use the cement with existing bricks (components) and other things putting them together in a away unique to my application.
For me language along is not enough. If the language does not have support tools, existing pre-build components and a vibrant community of users then already it is already no go...
So really i am limiting scope to evolutionary languages that build on existing tools, frameworks, communities. And that is Groovy and that is Grails.
Both Groovy and Grails stand on the shoulders of Giants.
If we go back to the house analogy then they are the SUPER CEMENT i use them to pull together existing bricks (Spring, Hibernate, Jetty, HSQL, sitemesh). In fact i would say that they are both actually closer to a magic wand rather than cement :)
So that fact at a language can do something cool is for me "not cool enough"
Building something in a language without tools, without existing component and with out a vibrant community is Yak Shaving
But hey that is what some people like doing :)
Software engineer by day, web ideas maniac by night. This Blog is about my experience as a web entrepreneur. The tools i use (grails, groovy, java, css, ec2), the problems i solve, the books i read. Basically an over all brain dump... enjoy :)
Thursday, November 27, 2008
Tuesday, November 25, 2008
Adding your own error message to a Grails Command Object
Here is another little trick I got to use in Grails recently:
Before I started using Grails I used Spring MVC and in fact i still do i my day job. Any way one of the things Spring provided was the Errors interface. This allowed you to add your own error message that would end up available to your view layer. By using methods such as
reject() and rejectValue()
So as Grails is built on top of Spring it also makes use of the Errors interface and it automatically injects an instance of this into your domain objects and command objects.
So in your controller if you want add an error message to your command object (eg HolidayQueryCommand)
Then all you have to is this:
holidayQueryCommand.errors.reject("message.code","default error message");
If you wanted to attach and error message to a particular property then do this:
holidayQueryCommand.errors.rejectValue("property","message.code","default error message");
These error messages are then all available in your GSP:
<g:rendererrors bean="${holidayQueryCommand}" as="list"></g:rendererrors>
That also goes for domain objects too not just command objects.
If this is useful to even 1 person then i am glad i posted it :)
Before I started using Grails I used Spring MVC and in fact i still do i my day job. Any way one of the things Spring provided was the Errors interface. This allowed you to add your own error message that would end up available to your view layer. By using methods such as
reject() and rejectValue()
So as Grails is built on top of Spring it also makes use of the Errors interface and it automatically injects an instance of this into your domain objects and command objects.
So in your controller if you want add an error message to your command object (eg HolidayQueryCommand)
Then all you have to is this:
holidayQueryCommand.errors.reject("message.code","default error message");
If you wanted to attach and error message to a particular property then do this:
holidayQueryCommand.errors.rejectValue("property","message.code","default error message");
These error messages are then all available in your GSP:
<g:rendererrors bean="${holidayQueryCommand}" as="list"></g:rendererrors>
That also goes for domain objects too not just command objects.
If this is useful to even 1 person then i am glad i posted it :)
And another book Groovy and Grails Recipes
Hi All
I also found another book that is available for download called "Groovy and Grails Recipes" you can also download it here
I have not yet downloaded this one myself yet but it is next on my list :)

Enjoy !!!
I also found another book that is available for download called "Groovy and Grails Recipes" you can also download it here
I have not yet downloaded this one myself yet but it is next on my list :)
Enjoy !!!
Definitive guide to grails 2nd ed available for early download
Hi All
Just wanted to let all the early birds know that the early access to Grahams "Definitive Guide to Grails second edition" is available for download here . It says that it is alpha book but most of the chapters are available. So anyway i bought and downloaded what was available and so far it looks really good. I already have the first edition in "dead tree" format so hopefully it lives up to the first one.
Anyway congrats to Graham for completing it and i will post a review once i have read what i have.

Peace
Just wanted to let all the early birds know that the early access to Grahams "Definitive Guide to Grails second edition" is available for download here . It says that it is alpha book but most of the chapters are available. So anyway i bought and downloaded what was available and so far it looks really good. I already have the first edition in "dead tree" format so hopefully it lives up to the first one.
Anyway congrats to Graham for completing it and i will post a review once i have read what i have.
Peace
Labels:
grails groovy book Graeme Rocher
Monday, November 24, 2008
Groovy's Design by capabilty or (duck typing) to the rescue
One of the many cool features available in Groovy is "Duck Typing". The phrase comes from "If it walks like a duck, if it talks like a duck then it is probably a duck".
Because groovy is dynamic, it allow you to call methods on an object without knowing until runtime if that method exists. So this has helped me this weekend on one of my Grails projects.
I have two domain objects.
Stamp and CustomStamp
They are similar but different. Anyway they currently both have a method called:
createStampText(String name,String email)
I then have a method on a Grails Service class that takes a Stamp and some other parameters to construct the desired output.
This service method should not need to care if a Stamp or a CustomStamp is passed in. So how to do this?
Well in Java i would introduce an interface that contains the method:
createStampText(String name,String email) maybe called IStamp
Then both Stamp and CustomStamp could implement it. Then my service method could be like this:
service.doSomething(IStamp stamp,String other,String more){
stamp.createStampText(name,email)
}
But already this is maybe the norm in the pure java world but it is NOT the groovy way and is quite frankly "Yak Shaving" just to get the method called polymorphically.
But using Groovy which my service object are thanks to Grails i can use Duck Typing. Now to use this i simply remove the type. So my service method becomes something like this:
service.doSomething(stamp,String other,String more){
stamp.createStampText(name,email)
}
This way i do not have to create a new interface and i do not have to modify my two domain objects. The service method will just try to call createStampText on any object that is passed in as the first parameter.
Now that is shweet !!
Because groovy is dynamic, it allow you to call methods on an object without knowing until runtime if that method exists. So this has helped me this weekend on one of my Grails projects.
I have two domain objects.
Stamp and CustomStamp
They are similar but different. Anyway they currently both have a method called:
createStampText(String name,String email)
I then have a method on a Grails Service class that takes a Stamp and some other parameters to construct the desired output.
This service method should not need to care if a Stamp or a CustomStamp is passed in. So how to do this?
Well in Java i would introduce an interface that contains the method:
createStampText(String name,String email) maybe called IStamp
Then both Stamp and CustomStamp could implement it. Then my service method could be like this:
service.doSomething(IStamp stamp,String other,String more){
stamp.createStampText(name,email)
}
But already this is maybe the norm in the pure java world but it is NOT the groovy way and is quite frankly "Yak Shaving" just to get the method called polymorphically.
But using Groovy which my service object are thanks to Grails i can use Duck Typing. Now to use this i simply remove the type. So my service method becomes something like this:
service.doSomething(stamp,String other,String more){
stamp.createStampText(name,email)
}
This way i do not have to create a new interface and i do not have to modify my two domain objects. The service method will just try to call createStampText on any object that is passed in as the first parameter.
Now that is shweet !!
Wednesday, November 19, 2008
Great little Grails Gem
Hi All
I have not blogged for ages. Been really busy trying to get a large retail web site live for the x-mas. Any way here is little gem i found.
I need to have a drop down list of accounts a user has access to. This needs to show on every page so they can quickly change accounts. Now there are many way i could do this in grails but i thought a tag lib would fit quite well.
So basically i want a drop down select list but i want to transparently get the list of accounts from info in the session. So i figure all i have do is wrap the existing grails select taglib. Grails allows you to call existing tags from within a controller or in my case another taglib. So i simply call the grails select tag with the correct parameters and send it to the out stream.
Here is the tag lib.
Then in my GSP
<g:accountListSelect id="acountId" name="id"/>
So easy i am writing this blog with the time i saved.
Enjoy :)
I have not blogged for ages. Been really busy trying to get a large retail web site live for the x-mas. Any way here is little gem i found.
I need to have a drop down list of accounts a user has access to. This needs to show on every page so they can quickly change accounts. Now there are many way i could do this in grails but i thought a tag lib would fit quite well.
So basically i want a drop down select list but i want to transparently get the list of accounts from info in the session. So i figure all i have do is wrap the existing grails select taglib. Grails allows you to call existing tags from within a controller or in my case another taglib. So i simply call the grails select tag with the correct parameters and send it to the out stream.
Here is the tag lib.
class GlobalCommonTagLib {
def accountListSelect = {attrs,body ->
User user = User.get(session.userId)
def accountList = user.accountAccesses.collect {it.account};
attrs.from = accountList
attrs.optionKey = 'id'
attrs.optionValue = 'name'
out << g.select(attrs)
}
}
Then in my GSP
<g:accountListSelect id="acountId" name="id"/>
So easy i am writing this blog with the time i saved.
Enjoy :)
Subscribe to:
Posts (Atom)