Monday, October 19, 2009

Simple Ajax sample

Using Ajax in Grails is quite simple. Grails have the prototype library making it easy to use Ajax in Grails.

You can try it out by creating a Grails-project. Here is an example ant some explanation of what is nescessary to think of.

1. Create a grails project:
2. Create a controller:

3. Create 2 .gsp-files in the /views/ajax folder; index.gsp and _itemList.gsp (_ means that it is a template)

4. Change the /views/layout/main.gsp by adding the following row

5. In the index.gsp in your ajax folder, put in this code:

6. In your _itemList.gsp put this code:

7. Now the only thing we need is some code in the controller. Open the AjaxController and put this code:

So what makes this work?

First of all, Grails supply the tags submitToRemote which is used for the "Add" button and remoteLink which is used for the "remove" link. These are used to use Ajax from grails and the only thing needed to make them work is to include the "prototype" javascript library, which we did in the layout to make it available on all pages. If you look on these tags you see the property action="" and update="listItems" this properties tells the framework what action to call and what to do with the result when the action is finished. Look at the index.gsp and you will see a <div> with the id="listItems", if you remember this is the same id that you saw as text in the update-property, this i the glue between the action pointed out and where to put the result. Pretty easy and straightforward, to make it short you need this
  1. Add prototype JavaScript library to the layout
  2. Add a DIV for the result
  3. Add some remote call and point it to the action you want to happen and where to put the result
Note : Ajax is used for enhanced functionality and to not to have to reload pages. Therefor using templates with small fragments of code is a convenient way to reuse code-blocks and functionality. It is better to think a bit ahead of what is needed to make nice solutions and reuse of code.

Wednesday, October 7, 2009

Making SSL calls with Grails

All of a sudden I realised that I have to call a service I have behind a SSL connection.

Using grails, this is easy to set up. Just create a couple of Groovy-classes and use it from the controller. It may be nescessary though to add the certificat of the SSL site to your JVM.

Adding the certificate
  • download  the certificate you need, name it to something usefull, for example theSite.cer.
  • use keytool to add it. (needs JAVA_HOME/bin in the path)
keytool -import -keystore /pathToJava/jre/lib/security/cacerts -file theSite.cer 

Class to make the call
this is the class to actually do the work, calling the SSL url you want to reach.
Class for password authentication
this class is needed to authenticate you at the remote site.

In the controller you can then use the class to get a responsestring from the remote server.


Saturday, October 3, 2009

Grails and EJB

Read Jason Rudolphs article about grails and EJB a while ago. Grails + EJB Domain Models Step-by-Step. It is a nice article but not what I want.

However it seamed a little bit of inventing the wheel again to import the classes into Grails in the same package structure, and I also wanted some other things that EJB3 gave me.

  1. I want the EJB level to handle application rules. In that way, I can reuse the EJBs from other places as well
  2. I have information i two different schemas in Oracle and grails was not supporting more than one.
  3. I like "hit and run" session beans to provide services. which means that I use interface in the sessionbeans to provide CRUD and listing methods. 

So I started to elaborate with putting the EJB.jar file in Grails library folder to try it out. It worked out nice, the only thing I have to do is to put some lookup code and start using the session beans. I also get access to all model POJOs.

I like this approach because I can use grails rapid application development with the robustness of EJB3 without doing very much extra.

The downside is that as I am a little bit lazy and avoid writing too much code, is that I have to run it on the same glassfish and I have to copy the jar-file every time I change the interface. I also have to make a war of the grails application and run it on glassfish.