Posted: January 10th, 2012 | Author: Christopher Vigliotti | Filed under: Groovy/Grails, Regular Expressions | 4 Comments »
The other day I had a Groovy/Grails bug fix whose solution was to break large non-space ‘words’ (or rather ‘substrings that do not contain spaces’) into smaller words by inserting spaces every X characters.
My regex is not the best, so after a bit of tinkering I reached out to the ever-helpful community over at Stack Overflow. My question was answered within an hour by Nik. I wrapped the solution in a simple function, and modified it to allow you to specify the character length…
static String breakLongWordsInString(str, charLength = 60) {
def response = (str =~ /(\w{${charLength}})/).replaceAll("\$1\n")
return response
} |
And here’s how it was implemented…
<%@ page import="your.package.path.and.ObjectName" %>
${TextUtil.breakLongWordsInString(variableName, 60)} |
Thanks Stack Overflow and Nik!
Posted: December 15th, 2011 | Author: Christopher Vigliotti | Filed under: Groovy/Grails | No Comments »
In the search for the perfect, lightweight Grails IDE I’ve temporarily abandoned the feature-rich and bloated Eclipse variant SpringSource. One of SpringSource’s features that I like is the ability to associate a specific version of Grails with each project. Since my requirement is to run a specific application with a specific version of grails, I created this simple batch file to run ‘ApplicationX’ with Grails 1.3.7.
@echo off
SET GRAILS_HOME=C:\grails-1.3.7
SET PATH=%GRAILS_HOME%\bin;%PATH%
CD C:\ApplicationX
grails -Dgrails.env=local -Dgrails.gsp.enable.reload=true run-app
pause |
Windows batch files FTW!
Posted: November 28th, 2011 | Author: Christopher Vigliotti | Filed under: Groovy/Grails | No Comments »
My fun with Grails continues. Today I was having a bit of difficulty wrapping my head around the concept of dumping and logging. Dumping variables to the screen or to a log file is easy in ColdFusion thanks to the CFDUMP and CFLOG tags.
One way to dump variables in Grails is to output them to the console. After reading through this and asking a co-worker for assistance I still had to tinker a bit before I could get log.trace() statements from displaying in the console.
So as it turns out, I had to modify the log output level for an entity in Grails modify the log4j code block in your application’s Config.groovy file.
trace 'grails.app.controller.chapter4.SongController' |
In this example “grails.app.controller” refers to ‘one or more controllers’ and ‘chapter4.SongController’ is the classpath and controller name. The code basically says ‘display all log messages at or above the trace level in SongController’. You can read more about logging levels here.
Here is a slight modification to the above code. This code allowed me to change the log settings for multiple controllers.
trace 'grails.app.controller.chapter4.SongController',
'grails.app.controller.chapter4.AlbumController' |
Note that you can apply the same log settings to all controller files…
Example C: trace 'grails.app.controller' |
And for reference here is SongController.groovy…
package chapter4
class SongController {
def index = {
log.trace("I can see log.trace")
log.warn("I can see log.warn")
log.error("I can see log.error")
}
} |
OK, back to the code. I’m deep into Chapter 4 of The Definitive Guide to Grails.
Posted: November 28th, 2011 | Author: Christopher Vigliotti | Filed under: Groovy/Grails, MVC | No Comments »
I’m fortunate enough to be working on three projects at the moment (I perform best when multitasking). One of these projects is ColdFusion-based (CF for life!), the second is rooted in Java and Selenium (test-driven development FTW) and the third project is written in Grails.
I chose the popular Eclipse-based IDE, SpringSource Tool Suite (STS). EClipse/CFEclipse is my preferred IDE for ColdFusion development, and I found the interface to be almost idenfical.
At present I’m bouncing back-and-forth between the book The Definitive Guide to Grails and Grails STS Integration and am really impressed with the language. In particular I like how controllers and unit tests are created in tandem. When creating a controller via the “grails create-controller” command both a controller class and a unit test are created.
I’ll be posting more about Grails in the coming weeks/months.
Big thanks to long-time friend and fellow ColdFusion (and Grails) developer Mike Copeland for helping me to get my development environment set up and for sending me a good list of Grails developers to follow (Mike, please don’t ever delete that list!).