Tag: Grails
Grails on JBoss AS 5.1.0 GA
by RJ Salicco on Nov.03, 2009, under Development
Grails is changing the way Java developers approach Web development. If you are not familiar with Grails, it is a Web application platform for the agile and dynamic language, Groovy, that runs on the Java Virtual Machine. There are plenty of available books on Groovy and Grails and there is a plethora of blog posts and online documentation to help you get started with your first Groovy and Grails project. This article will discuss creating a simple Grails application for JBoss AS 5.1.0 GA and demonstrate how simple Java Web development can be.
I have taken some time to explore developing a simple Grails 1.2-M3 application and deploying it on JBoss AS 5.1.0GA. Grails is all about productivity. In a few short steps, I create a simple Web application, generate a Web archive (.war) file and deploy it on JBoss AS 5.1.0 GA.
After Grails 1.2-M3 is installed and properly setup, I open a command prompt and enter
> grails create-app
and when I am prompted, I enter a name for my application.
> demo
A directory is created with the same name as my application. I then navigate to that directory.
> cd demo
Next, I create two domain classes to represent the solution to my business problem, storing company information in a database.
> grails create-domain-class org.axiomaticit.model.Company > grails create-domain-class org.axiomaticit.model.Address
The above commands create two .groovy files in the directory grails-app/domain/org/axiomaticit/model/, Company and Address. Here is what the Company.groovy file looks like:
package org.axiomaticit.model
class Company {
static constraints = {
}
}
Next I open the Company.groovy and Address.groovy files in a text editor or IDE and add some detail. Here is the Company.groovy file after I add some fields and constraints:
package org.axiomaticit.model
class Company {
String name
String website
static hasOne = [address:Address]
static constraints = {
name(blank:false)
website(blank:false, url:true)
address(nullable:true)
}
String toString() {
"$name"
}
}
And here is the Address.groovy file:
package org.axiomaticit.model
class Address {
Company company
String street1
String street2
String city
String state
String postalCode
static constraints = {
company(nullable:false)
street1(blank:false)
street2(blank:true)
city(blank:false)
state(blank:false)
postalCode(blank:false)
}
String toString() {
"$street1 $city, $state"
}
}
If you are not familiar with Groovy and the conventions of Grails, this might be a little weird to look at. If you are a Java developer, some of the above code looks a lot like what you already know, right? Let’s talk about what I just did. I created two classes to represent company information. First, I created a company class with a few important fields like ‘name’ and ‘website’.
String name
String website
Then I created another class, Address, to represent standard US address data.
String street1
String street2
String city
String state
String postalCode
I also added an Address to the Company class,
static hasOne = [address:Address]
and a Company to the Address class.
Company company
Don’t get too wrapped up in all the details if you are new to Grails, but what you should understand is that I am building a domain model and creating properties and relationships between persistent entities. The Grails documentation is quite detailed and the user community is also very helpful if you get stuck with the Grails conventions and semantics of Groovy.
Now that I have my domain model, I want to create the controllers and views for my Web application. Controllers and views? Think MVC (Model, View, Controller). So, I go back to my command prompt and enter each command:
> grails generate-all org.axiomaticit.model.Company
> grails generate-all org.axiomaticit.model.Address
The commands above will create a few things based the domain model objects; they create default controllers and views that will support basic CRUD (Create, Read, Update, Delete) functionality. The controllers will be in grails-app/controllers/org/axiomaticit/model/ and the views will exist in grails-app/views/company and grails-app/views/address respectively for each domain model object Company and Address. Let’s go ahead and see what I have created and run the application from the command line.
> grails run-app
Once the application starts up, I browse to http://127.0.0.1:8080/demo and test out the application. It might not be everything I wanted, but I have a great starting point, right? Let’s deploy this on JBoss AS 5.1.0 GA. Back to the command line to create a Web archive (.war) file.
> grails war
If everything builds properly, I have just created a file, demo-0.1.war, that I can drop into the JBoss deploy directory. I can start up JBoss and point the browser at http://127.0.0.1/demo-0.1 and test out the application. Wow, that took about 10-20 minutes and now I have a fully-functional Web application. Grails is a great platform for proof-of-concept work but because it is based on proven technologies like Spring and Hibernate, many developers are moving Grails applications right into production. Combine Grails with an enterprise level application server like JBoss AS 5.1.0 and you have productivity built on top of dependability.
This is a copy of what I posted at Jboss.org.
Simplifying Java EE with Grails
by RJ Salicco on Oct.29, 2009, under Commentary
I usually don’t like to just post a link to another blog post or article on the web, but I couldn’t help myself here. If you or a colleague haven’t seen a presentation on Grails by the project lead, Graeme Rocher, you should definitely check this out.
http://www.infoq.com/presentations/Web-Development-Grails-Graeme-Rocher
This is a great presentation to get you started with Grails. Watch this presentation, download Grails and create your own application in about 1.5 hours.
SpringOne 2GX 2009 Day Four
by RJ Salicco on Oct.23, 2009, under Commentary
Unfortunately, all good things must come to end and that means it is time to pack my things, check-out of my hotel room in NOLA and catch a flight back to Tampa. The good thing is that the excitement of SpringOne / 2GX doesn’t have to end. I am little late to press with completing this post (MacBook battery, sleep, travel, work), but anyone who works with me or who lives with me can tell you that I am still amped about what I saw and experienced at SpringOne / 2GX.
At the beginning of day four, I checked out Hamlet D’Arcy’s presentation on Groovy and OSGI. I have seen a presentation at the Tampa JUG on OSGI, courtesy of my colleague, Vladimir Vivien and Hamlet’s presentation really helped me get a hold of the technology again. He is a wealth of knowledge and a talented software developer / presenter.
One of my favorite presenters, Venkat Subramaniam, discussed design patterns in Java and Groovy. It was a very useful presentation. Venkat is an incredible presenter and the content of his presentations are right on point. He knows what he is doing!
After lunch, I checked out Jeff Brown’s (SpringSource / Grails) presentation on using Grails without a browser. I really enjoyed Jeff’s presentations all week. It is awesome to hear about a technology that I feel passionate about from the source. As Jeff said, Grails is a Web framework but Grails really offers more that just rapid Web development, it is also a platform. He proved that in his presentation.
SpringOne / 2GX 2009 was a great event. My Rod Johnson bobble-head is already on my desk solving enterprise Java problems. The No Fluff Just Stuff crew and SpringSource hosted a great event, at a great venue, in a beautiful city. Looking forward to the 2010 event.
SpringOne 2GX 2009 Day Three
by RJ Salicco on Oct.22, 2009, under Commentary
Day three was an exciting day. I attended an awesome presentation about DSL’s in Groovy presented by Guillaume LaForge. I got to speak with him a bit, he is a great guy. I attended Christophe Coneraets and Jeremey Grelle’s presentation on the technical pieces of Flex and Spring. Flex is great (already knew that), but being able to make remote calls to Spring is where it’s at. Spring’s BlazeDS support is very Spring-like and that means you an leverage your spring knowledge to work with Flex remoting. I checked out Keith Donald’s presentation, “Working with Spring Web Flow”. I really like Web Flow. I like Web Flow with JSF because it makes sense when you have to use JSF and I also like the Web Flow hooks inside of Grails. Hamlet D’Arcy’s presentation, “Legacy Code, Groovy and You”, really spoke to me. Some of us work in environments that need to pay attention to some of his key points on re-factoring/re-writing and TESTING. At the end of the day, I attended “Groovy AST Transformations” with Venkat Subramaniam. First, he is a great speaker/presenter and I look forward to attending most of his presentations because he knows what he is doing. Second, AST transformations look really cool, but I need to let it digest a bit before I dig in. It is a great feature of Groovy, like MOP, but it can also be dangerous, like MOP, if you don’t know what you are doing. I am looking forward to attending “OSGI and Groovy Jump Start”, “Design Patterns in Java and Groovy” and “Grails Without a Browser” on the final day of the event.
