Enter your email address to subscribe to this blog.

Recent Comments

Reversing words in a string
Ranjeet_Great said: public class Reversing_String_by_words { public static void main(String args[]) { String str[]... [More]

Create SQL Insert statements from a spreadsheet
Rob said: Really cool trick, 4 years and it still stands! [More]

Grails views: Controller and Action name
Grant said: Nice! I really like this solution. Keeps the navigation gsp nice and DRY. [More]

ColdBox Contest Entry
Ken Caldwell said: Hi Dan, I'm new to coldbox and new to frameworks. I'm trying to include your module into an app I'm... [More]

Intro to Spring Security Core for Grails
Eric Pierce said: Thanks for these screencasts, Dan! You made it crazy simple to get up and running w/security core h... [More]

Gravatars on a Grails Application

Building out my new site I needed the ability to show a Gravatar in the comments. I know there are usually plugins out there but I there are a lot of things I am going to do on my own just so I have an excuse to blog about them. First off visit the Gravatar docs to get an idea of how to request an image. This is what the finished product is going to look like.

The most basic image request URL looks like this:

where HASH is replaced with the calculated hash for the specific email address you are requesting. For example, here is my base URL:

If you look at the docs there are ways to customize the size and default image if an account is not found. There are a couple different approaches we can take but this is the route I decided to go. Since a comment is just going to be inserted why not store the email hash in the db. We have an easy way of doing this with GORM events. On insert I am going to get the MD5hash of the email address and store it.

That is really all there is to it. Now we can call ${comment.emailHash} and grab that value right from the db. Here is what the final comments template looks like.


Grails: Generating links in your domain class

I am working on coverting this site over to a grails applicaiton. While there are some good plugins out there I could of taken advantage of I decided to use this as a learning experience and write a ton of it from scratch.

For the blog post itself we have some unique URLs that look like this

This converts to /blog/year/month/day/slug. So first thing I needed to was setup a url mapping for this path. I also put some constraints on the parameters so we get exactly what we are looking for.

Continue Reading >>


Grails views: Controller and Action name

In a project I am working on i have a navigation bar and depending on what "page" I am on I need to have the class set as active.

If you look at the web layer documentation you will see that within the scope of a GSP there are a number of pre-defined variables, including:

There are also two variables that are available that will help us out with our little problem. These variables will give us the current controller and action.

  • controllerName
  • actionName
Now in our navigation bar code we can do a simple check to see if this menu item should have the active class.

In my case I actually have a home controller. When you fire up a default grails application you won't. This is perfectly fine and thankfully the controllerName variable will return null if this is the case. So we can modify our code to test for that.


Grails Spring Security Plugin - Logout postOnly setting

I had a question come in about a setting in Spring Security so I thought I would take a quick minute and explain it in case anyone else also has the same question. There is a setting

'grails.plugin.springsecurity.logout.postOnly = true'
that is true by default. If you look at the LogoutController's index action this make a little more sense.

All this is saying is that to Logout we must have that request made in the form of a post. An easy way to do that is create a link to the logout controller (remember index is our default action).

If you try and just visit the URL http://localhost:8080/{your_context}/logout you can tell by the code that this should throw a 405 error, and it does.


Creating and testing your first Grails Tag Library

I created a quick screencast to walk you through creating and testing your first tag library



More Entries