Hi, There was a case where i had to encrypt the HTML output of the GSP before displaying it to user. There are a lot of ways to do it, but i wanted to make is as simple as possible. Hence here is the pattern i used: Note: This is just an example, […]
Have you ever wondered how to overload “plus” operator in Groovy ? It is as easy as adding toppings to your favourite pizza. Let me explain. Let us taka a class for Pizza: [java] @ToString class Pizza { String name List<Topping> toppings double price } [/java] And another for the Topping: [java] @ToString class […]
In almost all the applications that we work on, we have to transform elements of a collection in one way or the other. We can do it in different ways. Let there be a domain “Employee” with following attributes: [java] class Employee{ String firstName String lastName Double salary } [/java] And we have 5 Employees: […]
A very useful enhanced method in Groovy 2.0 is inject method whose key purpose is to pass the outcome of first iteration to next iteration and keep on passing till all the elements of collections are processed. Lets start with an example, consider a class Employee with name and salary as attributes: [java] class Employee{ […]
@Log : This annotation provides the log object in groovy class. By using it you don’t need to create the Logger object, it automatically provides log object. Example: [groovy] import groovy.util.logging.Log @Log class Person { String name String address String city public void logDetails(){ log.info("Name:::${name}—>Address ::: ${address}—> city :::${city}") } } new Person(name:"Mohit Garg",address:"Address",city: "city").logDetails() […]
@TupleConstructor annotation provides the classical constructor with default properties. It will create the constructor with first parameter as address, second parameter as name etc. Constructor implementation depends upon the order of the variable declaration. One thing you need to ensure that you need to pass the value in same order as used in variable declaration. […]
Groovy is a dynamic language. We can’t check any typo error, method or property missing error at compile time. To check typo error, method or property missing at compile time, Groovy 2.0 introduces new features named as Static Type Checking which helps to check errors at compile time. To use static type checker in groovy […]
In one of my recent project, i want to profile method execution time. I have used Spring AOP to profile method execution time. It’s very easy to implement AOP profiling in grails 1. Suppose we have below mentioned service class, we want to log execution time of method saveDataStudent,saveDataUpdated methods in service class. We need […]
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 irrespective […]