Showing posts with label Roo. Show all posts
Showing posts with label Roo. Show all posts

Wednesday, July 22, 2009

Spring Roo vs Grails generated controllers



As I already tweeted earlier today I was trying out Spring Roo 1.0.0.RC1 as it was released today.

After playing a couple of hours with Roo I must say really like Roo using AspectJ inter-type declarations (ITDs). For example Roo will automaitcally generate id and version fields and getters and setters for your persistent fields in your domain class. Roo generates these at development time in separate AspectJ .aj files. As a developer you won't touch them as Roo will manage and update them automatically as you add fields etc. to domain classes. The great thing is the generated code will not interfere with your custom code and you don't have to be scared you custom or overwritten methods are overwritten by Roo again. During compilation all .aj files belonging to the Java class are compiled into just 1 .class file.

What I really liked is that when you are developing in SpringSource Tool Suite (based on Eclipse) code completion works also for methods contained in the AspectJ .aj files. So also debugging your code is a real breech as you can step through the methods generated by Roo easily.

Roo also contains scaffolding. However as a long term Grails user I got scared when I saw the controller code generated by Roo. See the example below:

Roo generated controller code:


@org.springframework.web.bind.annotation.RequestMapping(value = "/nation", method = org.springframework.web.bind.annotation.RequestMethod.GET)
public java.lang.String NationController.list(org.springframework.ui.ModelMap modelMap) {
modelMap.addAttribute("nations", com.footdex.domain.Nation.findAllNations());
return "nation/list";
}


Grails generated controller code:

def list = {
params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
[nationList: Nation.list(params), nationTotal: Nation.count()]
}


OK, the Roo generated code could look better if we remove all the package information before the classes:

@RequestMapping(value = "/nation", method = RequestMethod.GET)
public String list(ModelMap modelMap) {
modelMap.addAttribute("nations", Nation.findAllNations());
return "nation/list";
}


But then we should also just create the same functionality in the Grails controller (without paging):

def list = {
[nationList: Nation.list()]
}


So which one do you prefer?

I really think Spring Roo will have a great future. The use of ITDs for generated code and it's support in SpringSource Tool Suite will really aid adoption. Also in case Grails is not allowed within your company, Roo could really help you to get more productive.

As I expressed already in my example I really like Grails/Groovy effectiveness and readiness of code. Also the deep level of code by convention empowered by Grails is unbeatable.

With Grails and Roo, SpringSource has 2 remarkable frameworks in it's portfolio. Let them be as innovative as always and we as developers are the winners!

Thursday, May 28, 2009

Spring Roo coming alive



Spring Roo was announced just recently at JavaOne, and after the first alpha releases it's now time for M1.

If you are interested in this new "code generating" / productivity framework led by SpringSource you defintely have to read this introduction and example by project lead Ben Alex. Currently there is not much documentation available but this will change in the near future.

Similar to Grails, Roo wil have a plugin architecture using "add-ons". Looks like I've published the first community add-on - integrating SiteMesh - myself according to Ben's blog. Follow the steps below to try out the SiteMesh add-on:


  1. Follow the Roo installation instructions from Ben Alex's getting started blog post

  2. Download spring-roo-addon-sitemesh-1.0.0.M1.jar from http://jira.springframework.org/browse/ROO-41

  3. Copy spring-roo-addon-sitemesh-1.0.0.M1.jar to ROO_HOME\dist

  4. Start Roo and create a project as described in Ben's blog post

  5. After creating the project install SiteMesh to your project. Type 'ins' and press tab. Roo's shell will already display the availability of the install sitemesh command. Finish the command 'install sitemesh'

  6. That's it. The Maven pom file is updated so it contains the SiteMesh dependency. The SiteMesh filter and filter-mapping are added to web.xml. And default sitemesh.xml, decorators.xml and main.jsp files are created within your project.

  7. Open webapp\decorators\main.jsp to change your main layout and continue your project creating persistent classes, controllers, views etc.



It's really easy to create add-ons. If you are interested in creating your own add-ons have a look at the sources of the SiteMesh add-on or see the Roo core add-ons using Fisheye or SVN.

I think Spring Roo will have a bright future as it is led by SpringSource and primary uses best practices and boosts developer productivity. For companies/developers already using the Spring framework, Roo will be an easy and natural next step. This is a real strong point for Roo. Also integration with the SpringSource Tool Suite integration since day one will have huge benefits.

As a long time Grails adept I'm wondering how SpringSource will position both frameworks in the future. At least we have another choice for productivity again.