Grails

Inject Methods/Properties to be available in all GSP Pages

Hi,   There could be cases where we would need to inject some methods or properties in all GSP pages.   As we know that the GSP pages extends GroovyPage class, hence injecting properties/methods in GroovyPage class can solve out purpose.   Example: Lets add appVersion property and a method to provide logo link...

by Kushal Likhi
Tag: MetaProgramming
25-Sep-2012

Grails

Enhance HttpServletRequest using metaprogramming

Most of the times we use request.getHeader("user-agent") to get the client browser user-agent details inside the Grails Filters or Controllers. User-agent is one of the examples. There are so many such methods e.g. method for checking whether the request was an ajax call, getting IP address of the client machine, checking whether the...

by Bhagwat Kumar
Tag: MetaProgramming
25-Sep-2012

Grails

MetaProgramming with MetaClass

As we all know that groovy is a dynamic programming language, so we can add any method to any class at runtime. First, let’s see a small and simple example of metaProgamming given below, in which we are adding a method (isEvent) into an Integer class. [groovy] Integer.metaClass.isEven = { -> // only (->) sign indicates that...

by Amit Kumar
Tag: MetaProgramming
24-Sep-2012

Grails

Injecting Method to trim string length on gsp pages

Restricting the length of a string on gsps is a common scenario in most of our projects. Groovy's metaprogramming magic made it too simple for me. I added the following lines of code to Bootstrap.groovy: Object.metaClass.trimLength = {Integer stringLength -> String trimString = delegate?.toString() String...

by Aman Aggarwal
Tag: MetaProgramming
03-May-2010

Grails

Groovy: Create class with dynamic properties

I recently came across a way in Groovy to define a class which can have dynamic getters and setters and properties can be added on the fly. The following code creates a class "MyExpandableClass" and defines two methods in the "getProperty and setProperty". If we define these two methods in a groovy class, then calls to any getter or...

by Himanshu Seth
Tag: MetaProgramming
25-Nov-2009