In our last article about Grails we promised some tutorial showing how easy and simple it is to create web application in this framework. So – let’s do it!
From this place you can download project files ready-to-use.
In this little ‘How-To’ I’m going to use NetBeans, since it has an excellent support for Grails applications. First of all – let’s create new Grails project: ‘File’ -> ‘New Project’ -> ‘Groovy’ -> ‘Grails Application’. NetBeans will ask for a folder placement of our newly created project and will automaticly build whole project folder structure for us. Actually, we can even run our project right now – and we can see in the browser that it works.
After a quick look on available Grails plug-ins I have decided, that it would be really nice to create some kind of a Twitter Client application. There is a nice plug-in, which will help us with Twitter API. Let’s download it. Right-click on our project name and choose ‘Grails Plugins…’, then select ‘New plugins’ and search for
twitter(0.2). Click ‘Install’. This plugin is based on JTwitter Java library which is responsible for Twitter API handling. It also contains basic controller, service and views. Is MVC model on your mind? That’s correct. So now let’s browse our plug-in location – its’ written in the console during installation. Unzip it. Now I’m going to set up our needed files, don’t worry if you have troubles understanding their purpose. I will explain all later step by step.
You can copy provided folders to your project and start using them immediately. ‘Src’ and ‘lib’ folders contain java files and jars responsible for Twitter API handling. OK, we are almost set up now! Our back-end files are ready, and thanks to dependency injection we have separated logic of our application from presentation layer.
TwitterController.groovy is responsible for redirecting to specific views and it should not contain any core logic, as it not promote re-using and clean coding.
Ok, let’s create some views, so we can actually see how does our application work. All we need is to create some GSP – Groovy Server Pages. Expand ‘Views and Layouts’ in NetBeans, right-click on ‘layouts’ and select new GSP file (sometimes you must go to ‘Other’ -> ‘Groovy’ -> ‘GSP File’). Name it ‘twitter’. I’m going to just explain some snippets of the code:
these lines tells our app, where to find CSS files and images.
Now right-click on ‘Views and Layouts’ and create New folder, which will be named ‘twitter’. Here we place all our pages – GSP files. Let’s create our ‘index.gsp’ (right-click on ‘twitter’ -> ‘new’ -> ‘GSP file’, name it ‘index’).
Adding this line:
will tell Grails to use our template which we just created. Now let’s add the rest of the code (just paste it from provided files).
Above snippet of code is an example of using custom Grails tags. You can open ‘TwitterTagLib.groovy’ from package placed in ‘taglib’ folder. And you can see, that there is declaration of twitter namespace and definitions of tags, that can be used in our app pages. Sort of shortcuts – instead of writing annoying scriptlets, you can create and use custom tags like the one above responsible for displaying Twitter user avatar.
Our index page allow us (after succesful login) to update Twitter status and displays timeline of our friends. I have used JQuery for some graphical effects and to add some functionalities –
yellotweet.js. To see the results – run your project, browser will be opened and you will be able to select all your controllers. This time there will be just our TwitterController. In my next tutorials I will explain some files we have just created in more detail and of course enrich our YELLOtweet client with some additional functionalities.
So we just used Grails plug-in to create basic Twitter client. Not much work of course, but it is an excellent example how can we build web applications without re-inventing the wheel. And it is good for beginners to just feel more confident about each file or folder purpose, isn’t it?
Ok guys, stay tuned. In my next articles I will try to explain some things in more detail and I quite fancy Google App Engine. I always say: ”critique is the best motivation’ – so feel free to comment or ask questions.