I went through this tutorial today. Nice example, but there was something I did not like. As a geek I sit on a sunday and try out a tutorial on my home computer. Lazy as I am I do not want to start my MySql instance, since adding more running components tend to slow down my almost four year old Toshiba laptop.
Since I was going to run Glassfish v3 anyway I was thinking a better idea is to have the database in the form of JavaDB, so I tweaked the script to fit into Derby instead, not much tweaking to do actually and doing it made me happy to not be forced to start MySql (My mysql is by the way a bit old and installed the same time as I installed InstantRails long time ago, and probably needs to be updated). The insert script in the tutorial work more or less as it is in JavaDB just remove the 3 SET-clauses in the top and the COMMIT in the end and it should work.
The script can be found at my google-site http://sites.google.com/site/ironicprogrammer/home/jsf-2-0-tutorial-db-script
Programming in (mostly) Java, Grails, Ruby and Rails. Sharing thoughts, solution ideas and tricks to work around. Maybe also giving .NET and Microsoft some ironic comments. And ironic, yes often if it is called for.
Sunday, January 10, 2010
Monday, November 30, 2009
Quote to think about
Started to read the book "Refactoring. Improving the Design of Existing Code" by Martin Fowler the other day. Here is a quotation from that book that is what programming is about:
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand"
So tru, so tru...
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand"
So tru, so tru...
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:
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- Add prototype JavaScript library to the layout
- Add a DIV for the result
- Add some remote call and point it to the action you want to happen and where to put the result
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
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.
/Peter
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.
/Peter
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.
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.
/Peter
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.
- I want the EJB level to handle application rules. In that way, I can reuse the EJBs from other places as well
- I have information i two different schemas in Oracle and grails was not supporting more than one.
- 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.
/Peter
Thursday, September 17, 2009
Tweaking grails pdf plugin
I had a little bit of problem when I wanted to use grails PDF plugin. (By the way Aaron Eischeid, thanks for providing this). However some things is not as wanted.
pdfLink: this is a nice feature for generating PDF by providing an url to the page to generate as PDF. But I do not want to provide parameters by adding them to the url in get-form.
pdfForm: Somewhat better, But this method gives some other problems. First of all, pictures is lost since the PDF only generates from string, also the layout is not automaticly included in the render call.
So I did the followin tweaks. Sorry Aron, but I had to hack into the PdfService.groovy and PdfController.groovy to get it work and also set up a template in a specific way.
This is what I did:
In PdfService.groovy I added a method to be able to generate a PDF with images for pdfForm, to do this the renderer need to know the baseUri to get find the image.
I added this code:
In the method pdfForm for the PdfController.groovy, I replaced code with call to the new method.
I then made a template with the following structure to also use the layout, without the applyTemplate, I only got the template code (also note that the template is looked for in views/pdf/):
I have not done this with the options for controller and action, just for template, since the g.include seams to return empty content. But I will continue to explore.
! An update. I realized that the pdf-plugin was build for grails 1.1. maybe this was the reason that the g.render in the plugin gave med nothing as a result. I was actually using grails 1.0.4. Using 1.1.1 however makes the action and controller work better. No pictures though, but they show when I am running pdf-plugin as an application after downloading the source.
I will try to find the proper place to address these issues to improve the plugin.
/Peter
pdfLink: this is a nice feature for generating PDF by providing an url to the page to generate as PDF. But I do not want to provide parameters by adding them to the url in get-form.
pdfForm: Somewhat better, But this method gives some other problems. First of all, pictures is lost since the PDF only generates from string, also the layout is not automaticly included in the render call.
So I did the followin tweaks. Sorry Aron, but I had to hack into the PdfService.groovy and PdfController.groovy to get it work and also set up a template in a specific way.
This is what I did:
In PdfService.groovy I added a method to be able to generate a PDF with images for pdfForm, to do this the renderer need to know the baseUri to get find the image.
I added this code:
code
In the method pdfForm for the PdfController.groovy, I replaced code with call to the new method.
code
I then made a template with the following structure to also use the layout, without the applyTemplate, I only got the template code (also note that the template is looked for in views/pdf/):
code
I have not done this with the options for controller and action, just for template, since the g.include seams to return empty content. But I will continue to explore.
! An update. I realized that the pdf-plugin was build for grails 1.1. maybe this was the reason that the g.render in the plugin gave med nothing as a result. I was actually using grails 1.0.4. Using 1.1.1 however makes the action and controller work better. No pictures though, but they show when I am running pdf-plugin as an application after downloading the source.
I will try to find the proper place to address these issues to improve the plugin.
/Peter
Wednesday, September 9, 2009
Creating jasper reports in Grails
I like to work with Grails, so to continue making useful stuff i elaborated a little bit about how to make reports in Grails using Japser (which is a nice reporting tool)
My aim is to do reports in PDF and Excel. And since it don't think it is the f...ing business of the report file to know anything about the database I intend to use my own models to send into the report.
Create an application:
grails create-app bookshelf
Install the plugin:
grails install-plugin jasper
This usually takes a while since Plugin list cache is almost always out of date when installing plugins. So go take a coffe or something.
First of all I want a domain-class to hold books. So create that one and set up a few books in the BootStrap.groovy to have somthing to test with.
grails create-domain-class Book
Add a few properties to the book, for example:
and add a few books in the BootStrap.groovy to have something
To create a report from Jasper, you need to create a .jrxml and then compile it to.jasper report file.
To create a report for my purpose, I used iReport. this is a tricky one because the jasper plugin which I got when installing is 0.9.7 and has a mistake in it that makes the reports fail. However to solve that look up the lib folder in the plugin-installment (using grails1.0.4 is in the project, but using 1.1.1 it ends up in your home folder) and remove the jasperreports-2.0.5.jar, otherwise you will end up with some NullPointerExceptions when tryring to run the report. Loocking there you see that the other jar file is jasperreports-3.1.2.jar, so the iReport version to download is 3.1.2.
I choosed the standalone version but there is a version that plugs into Netbeans.
Making the following report and compile it makes an .jasper file to use from grails.
Now things is starting to get a little bit interesting. We now need to make a report of this. I genereated all stuff for book by using the command:
grails generate-all Book
we can then use the generated views to present the report.
Add the following line in the view to get a PDF and a XLS report:
(of course they should be tagged, but this stupid blog editor does not want to show it if if make them as tags)
for example under pagination, I used an action in the controller and a little bit of of code, not much really, to to what is needed.
That's it. Try it out yourself!
(For the lazy one, a zip of the project can be found here: http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1 as an attachement)
My aim is to do reports in PDF and Excel. And since it don't think it is the f...ing business of the report file to know anything about the database I intend to use my own models to send into the report.
Create an application:
grails create-app bookshelf
Install the plugin:
grails install-plugin jasper
This usually takes a while since Plugin list cache is almost always out of date when installing plugins. So go take a coffe or something.
First of all I want a domain-class to hold books. So create that one and set up a few books in the BootStrap.groovy to have somthing to test with.
grails create-domain-class Book
Add a few properties to the book, for example:
and add a few books in the BootStrap.groovy to have something
To create a report from Jasper, you need to create a .jrxml and then compile it to.jasper report file.
To create a report for my purpose, I used iReport. this is a tricky one because the jasper plugin which I got when installing is 0.9.7 and has a mistake in it that makes the reports fail. However to solve that look up the lib folder in the plugin-installment (using grails1.0.4 is in the project, but using 1.1.1 it ends up in your home folder) and remove the jasperreports-2.0.5.jar, otherwise you will end up with some NullPointerExceptions when tryring to run the report. Loocking there you see that the other jar file is jasperreports-3.1.2.jar, so the iReport version to download is 3.1.2.
I choosed the standalone version but there is a version that plugs into Netbeans.
Making the following report and compile it makes an .jasper file to use from grails.
Now things is starting to get a little bit interesting. We now need to make a report of this. I genereated all stuff for book by using the command:
grails generate-all Book
we can then use the generated views to present the report.
Add the following line in the view to get a PDF and a XLS report:
(of course they should be tagged, but this stupid blog editor does not want to show it if if make them as tags)
for example under pagination, I used an action in the controller and a little bit of of code, not much really, to to what is needed.
That's it. Try it out yourself!
(For the lazy one, a zip of the project can be found here: http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1 as an attachement)
Subscribe to:
Posts (Atom)