Grails part 3 – Creating your first webapp

Let me shock you with this: Grails does not need Eclipse. It has a beautiful interactive console that helps you in every task, and sometimes it is the only way to obtain certain results. We’ll be using Eclipse simply because most of us had experience with it and it’s f***ing comfortable.

Start your Grails perspective and, in the project explorer, right click->new->grails project. Sit back, it’ll take some time. During this time, maven will work for you to download any requirement to make the beast come to life.

Once it’s done… we’ll you’re ready to go.
Hint: coherence is nice, so I suggest you to create a run configuration as you’d do for any runnable project in Eclipse. So create a new Grails run configuration and set it up like this.

Please note the “Grails Command” field. That is exactly the command Eclipse is gonna issue to the Grails console.
Once you’re done with that… well, I guess you can launch it. But wait… where’s Tomcat? Well, who cares. What actually happened here is that Grails needed Tomcat and either found it in the Grails distribution or downloaded it as dependency.

It’ll take some time because even here, Grails does a basic check of its dependencies and compiles the code, if needed.

Browse to http://localhost:8080/TestProject (or whatever you project name is). You should be presented with something like this:

and I really hope this shows up, because debugging Grails issues is something that definitely requires some experience!

What you see here is a Grails generated page that lists out all the controllers available in the web app. In this case, there are none.

In the next article we’ll try to create our first controller!

Grails part 1 – basic components and concepts

I feel guilty now. I really acted like a fanboy when, in the previous article, I just proclaimed the superiority of the Grails platform, rather than giving useful information.

So here’s what I’m going to do right now. I’ll try to create a sequence of articles describing the key “start points” of Grails. Hints trying to help you  understanding where to start your study. Each article will have some requirements and difficulty I’ll set at the beginning.

Requirements: basic programming, basic web programming, some database programming
Difficulty: introduction 

But let’s start right now, shall we? What I’d like to do today is to name some key components/concepts that will recur during the series.
If you have no idea of what a concept refers to, you should do a little google search before you proceed to the next step, but do not go too much in detail now, let’s just overview the whole thing.

Model View Controller or MVC
I have some sort of mania for this pattern… If you’ve never heard of the MVC pattern, then you really should start googling around now, but in a few words, a MVC is a pattern that should help you separating the model (data retrieval/modification mechanisms and business logic) from and the controller (describes the interaction between the components and the flows) from the view (presentation of the data) as much as possible. Each section should be able to do their job without being involved in the development of the others.
In this way:
- the model part will be in charge of implementing the business logic that regulates the access to the data, and performs the necessary transformations and calculations;
- the controller describes the flow of the interaction between the external agents (user or program), the model and the view;
- the view will create a proper presentation of the data that the controller made available, describing a paradigm the external agent (user or program) will use to interact back with the underlying controller.
It’s not a feature of a programming language, it’s a strategy and some frameworks (like Grails) not only help you applying it, but kinda force you to do so. Some other frameworks show a very weak separation between the model and the controller .
Some web developers working on small projects might not have used one, but MVCs are slowly being adopted also for tiny applications.
What you normally expect from a MVC framework is a specific way to write the flows in the controller and a specific language to write the view (to express the formatting of a variable, to iterate over a collection etc.).
In the Grails world, the model must be written in Groovy (a highly productive dynamic language, very close to Java) and the view in Groovy Server Pages (GSP), a very expressive “design” language.

Object Relational Mapping or ORM
If you come to me for an advice, make sure you have no line of SQL in your code, that is something that would turn me into a beast. Unless it’s very needed (see performance or some unidentified major reason) developers (and web developers) should have abandoned the direct use of SQL long ago.
ORM  is a technique that should improve the way software interacts with RDBMS, reducing the distance between the code and the data. How does this happen? Well, from the developers point of view, what happens is that you don’t really have to deal with tables, columns etc. anymore as tables are mapped to objects. Developers never actually see tables or the technical aspects of data retrieving, update and insertion.
One beautiful side effect of this approach is that since developers do not care much about the underlying database, multiple “dialect” connectors can be written so that from the software point of view, which database plays with its data, is not-a-software-problem.
This topic really requires some background in relational databases, so if you feel a little weak on this topic and your memory links words like “transactional”  to Skyrim, then some more study could help
Grails offers you a superb integration of a persistence layer named Hibernate (how persistence relates to ORM is something you should concentrate your study on). In very few words, this tool offers you the ability to connect to most databases, define schemas and run queries without really worrying about tables and SQL itself. Hibernate can be used in any Java application, but the Grails integration is something that will shock you, even if you already know the tool.

Test Suite
Unit testing and integration testing is what save you from the worst shit ever. Or at least it tries, since you’ll likely be writing your tests yourself!
Could sound boring (and it is) and some might consider it something “added to” the Grails platform, but believe me, it’s not. In these days I’m discovering myself how immense can be its help in fixing ugly code.
Grails offers you two ways of testing your work.
- Unit testing: a very clean way to test your units of code singularly. In short, imagine you could split your code in units as much independent from each other as possible (aka. good code) . Now imagine that you could associate to every unit, another piece of code that tests it with various input sets knowing already what result to expect (unit test). Now when you make some modifications to that code, you can quite be sure that if it passes the tests, the code is not losing compatibility with the previous version! If you don’t know what I’m talking about, see the links at the end of the article.
- Integration testing: a intriguing way to test entire procedures or sequences in an environment the resembles a full running environment. For example, this allows you not only to verify how the Encoder class encodes the user password, but also how the full login process works in all the various contexts.

These are the concepts behind the major key components of Grails.
If you want to follow the path of Grails, I think you should make sure that you already know what these concepts are about, at least for what concerns the theory.

Here some useful links:

Model-View-Controller
- High level explanation http://en.wikipedia.org/wiki/Model_view_controller  .
…further readings…
- Some more reading on how to use Spring MVC outside the Grails framework http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

ORM
- High level explanation http://en.wikipedia.org/wiki/Object-relational_mapping
- Data modeling http://www.agiledata.org/essays/dataModeling101.html
…further readings
- Hibernate… of course http://www.hibernate.org/

Unit testing

- More than sufficient http://en.wikipedia.org/wiki/Unit_testing
- JUnit http://www.junit.org/

Grails – the search is over

 And it is over for real. Imagine a full blown on-rails platform that…
- runs on almost any servlet container (Tomcat, Jetty, etc.);
- allows you to create web applications using a beautiful integration of Spring MVC
- … using a nice dynamic language (Groovy) that is fully compatible with Java
- … but not stopping you in any way to use Java itself if you like (for most functionalities);
- interacts with the database with the most impressive Hibernate integration (GORM) I’ve ever seen;
- makes it easy to create templates using a very simple yet powerful language (GSP);
- has a huge availability of plugins, from captchas, to optimizations, from LDAP to analytics;
- integrates a powerful testing suite;
- runs ‘convention-over-configuration’ for 99% of the features;
- the Eclipse plugins guide you during the development in the most useful way;
- its learning curve is gentle;
- it’s created and maintained by SpringSource!

Now, I’ve never been much satisfied by on-rails platforms, seriously, it’s not my world. Even more, being more a software engineer than a web engineer, my interests in that kind of products have always been kinda mild.
When I first met Grails (10 months ago) I was skeptical -as always- but it took me less than 1 hour to realize what was going on in my computer, and I was shocked. Everything was over the top. The persistence layer is SUPERB, the separation of the controllers and the UI is perfect as much as the languages needed to develop them (and their organization inside Grails). The time needed to understand all the basics? 1 hour.
Still, I HAD to start a test project and see how much my first feeling was correct and be certain this marvel of technology was ready to suffer the pains of a real full blown production environment. Folks, it really met my expectations. This thing… is amazing.

Of course it has some downsides, in 10 months I had my oh-oh here and there.
First of all, in production you might not need a super powerful machine to run it (unless your traffic needs it), but in development… well get ready to heat your processors and disk.
Secondly (by the time I’m talking, I’ve haven’t seen Grails 2 yet), sometimes the development environment simply stops working. “This is broken, this does not compile” you know, the kind of mysteries that makes you bang on the keyboard like a monkey. You just need to get in touch with Grails common oopses to fix that in an instant, but the very first times, it’s going to be a little troublesome. Note: this kind of issue are likely related to the Eclipse plugin, rather than Grails itself.

I’m no fanboy. I hate fanboys. But this framework makes me very close to one…

GO GRAILS! 

Me against the web #1 – template engines

I hate the web… no, not pretty right, I love the web. I hate making the web.
I hate HTML and CSS as they’re way too uselessly structured markup languages for accomplishing such simple task. They have dialects, browser specific tags… but of course, it’s a designer’s tool, and even if we software developers need to face it from time to time, we rarely go deep into the topic.
But what really makes me go nuts is web scripting languages.
Any web designer/developer could spend hours in telling you a huge list of good reasons to use PHP… but the truth is there’s only one: you can do all by yourself.
By ‘PHP’ I don’t mean just the language itself that is valuable a valorous (still I don’t, like it) but the coding style that naturally evolved from it.
If you can use PHP, you can build a web application from scratch to final product that is:  complex, impossible to read, edit or improve, no evidence of coding standards, no sight of scalability.
So it’s obviously a bad idea to code like this, but if you’re alone working on a project, at least PHP gives you a chance.
Let’s stop telling bad things about others, right? What if you want to do the things the right way?
We’ll be talking about it in the series “Me Against the Web”.
Read more of this post
Follow

Get every new post delivered to your Inbox.