Step by Step – creating a new webapp using Grails

These steps walk you through creating a new web app using the Grails framework.

Installation

Install Groovy and Grails as per instructions:

App Generation

Generate a new app from grails using grails create-app, and give it a name, say test_app

From the location where you ran the create-app target, an application directory with the name of your new application was created. cd into this directory, and create the entities for the application: grails create-domain-class and specify the entity name when prompted. Repeat for as many entities as you have in your model.

Database setup

To connect the app to a MySQL db, cd into grails-appconf (under your application directory), and edit the ApplicationDataSource.groovy file:

<code>
class ApplicationDataSource {
   @Property boolean pooled = true
   @Property String dbCreate = "create-drop" // one of 'create', 'create-drop','update'
   @Property String url = "jdbc:mysql://localhost/grails_test1"
   @Property String driverClassName = "com.mysql.jdbc.Driver"
   @Property String username = "grails"
   @Property String password = "grailspass"
}
</code>

Connect to your MySQL server with the command line mysql client, and create a new database:
create database grails_test1

Create the user and grant access to this new database:

<code>
grant all on grails_test1 to 'grails' identified by 'grailspass';
flush privileges;
</code>

Drop a copy of the MySQL JDBC connector jar into the lib directory under your application directory.

Generate scaffolding code

Generate the app: grails generate-all. Give the entity name when prompted, and repeat this step for each entity in the application.
Und

Now start the test server: grails run-app

Bring up a browser and point it to http://localhost:8080/APPNAME – where APPNAME is the name of the application you just created.

You’ll be greeted by the application’s default index page, with a list of the Controllers, one per Entity in your application. Click on one and test the sample CRUD pages that were created.

Adding Attributes to your Entities

At this point your Entities only have default properties because we didn’t change them. To be of any use you’ll need to edit them and add additional properties to describe them.

Stop the webserver. I’ve found that modifying the entities while the app is running does not automatically update your tables in your database.

Under your app directory, go into grails-appdomain. You’ll find the source for your Entities here. Assuming you have an Enity called Category, lets add a couple of properties:

<code>
class Category { 
	@Property Long id
	@Property Long version

    String toString() { "${this.class.name} :  $id" }
}	

</code>

Underneath the version property, add a name and description properties:

<code>
	@Property String name
	@Property String description
</code>

Now rebuild with grails generate-all, then restart your app with grails run-app.

At this point go back to your MySQL client and describe your tables to see their structure (desc table_name). Notice that due to the ‘create-drop’ property in your datasource config that we configured earlier, when you start your app Grails is automatically creating your tables for you, based on your Entities (this behavior is configurable).

JavaOne 2006 roundup

java.sun.com has a roundup of the key messages from the Key Note presentations and the main action items for the community. Most of these revolve around rallying the troups to get involved with the Sun sponsored open source projects, for example Glassfish and Netbeans subprojects (Profile, Mobility, and Matisse).

OnJava.com also have an ‘Executive Summary’ on what was hot and not this year: What’t hot: AJAX, EE5.0 and EJB3.0, Mobile apps. What’s not: preregistration for session seats (personally I thought this was a neat idea, until I changed my mind a couple of times on what session I wanted to attend I found myself with the red ‘denied’ screen at the door, and was escorted to the overflow room where I was subjected to watching a streaming webcast of the session that kept cutting out every minute or so and you kept loosing 10 seconds of the presentation, so you never heard the presenter complete a sentence – now that was annoying), the Sun opensource annoucement, with no details of when, how and who is involved.

My own impressions this year (from only attending on Wednesday) – there was a noticable absence of Sun self-promoting technical sessions, which has been a major critisim from previous years (and I think one of the main reasons for the appearance of JavaOne alternative conferences, like No Fluff Just Stuff). The sessions I attended were all presented by individuals from other organizations and about technologies they were working with or on – I find this perspective more interesting than hearing self-promotion. My main critisim – the sessions I attended gave me a high level overview of some new feature, but I came away feeling I had to now go and do some reasearch on my own to get the real nitty-gritty inside info – the sessions didn’t go into details deep enough for me. Still, I did get to find out about some technologies (Groovy) I knew relatively little about, so I came away with some new skills and interest in new areas that I will be spending some time investigating further, so it was worthwhile for me.