Spring MVC and JRuby controllers

While going through some Spring 2.0 documentation on using dynamic languages in combination with Spring I noticed an example of a Spring MVC controller written in Groovy. I fiddled arround with it a bit, the refreshable bean feature makes it really cool... now I can edit my controllers in a running application server!

Since I like Ruby I decided to combine to documentation on using JRuby and the Groovy controller example.

First, I created this ultra simple controller:

RUBY:
  1. require 'java'
  2.  
  3. include_class 'org.springframework.web.servlet.ModelAndView'
  4. include_class 'org.springframework.web.servlet.mvc.Controller'
  5.  
  6. class MyController <Controller
  7.   def handleRequest(request, response)
  8.     puts "I'm in Ruby"
  9.     mav = ModelAndView.new @@resultView
  10.     mav.addObject("key","value")
  11.    
  12.     return mav
  13.   end
  14.  
  15.   def setResultView(resultView)
  16.     @@resultView = resultView
  17.   end 
  18.  
  19.   def toString
  20.     return "MyController in Ruby"
  21.   end
  22. end
  23.  
  24. # needed by spring
  25. MyController.new

The setResultView method (l. 15) will be called from Spring (IOC) to configure to resulting view for this controller. As you can see the controller class inherits Spring MVC's Controller interface (yes, I know inheriting an interface might sound a bit strange... but Ruby doesn't actually use the interface concept). I had to implement the toString method to get rid of some nasty error messages complaining about it.
For people less familiar with Spring MVC, the ModelAndView object contains the name of the target view AND the model to be rendered in this view. The objects added by the addObject call will be exposed to the view renderer (i.e. jsp, xslt, velocity, freemarker).

The controller can be configured in the Spring MVC dispatcher xml like this:

XML:
  1. <lang:jruby id="myController" refresh-check-delay="3000"
  2.         script-source="/WEB-INF/Ruby/MyController.rb"
  3.         script-interfaces="org.springframework.web.servlet.mvc.Controller">
  4.         <lang:property name="resultView" value="view" />
  5.     </lang:jruby>

Notice the Spring 2 use of namespaces, and the refresh-check-delay attribute which defines the interval in which the script file is checked for modifications. Also, as you can see we use Spring to set the 'resultView' property.

Throw in some url handler mappings and a JSP and there we go! Sweet!

At the moment I don't have a specific case for using Ruby to write spring controllers, but I really like the fact that it is actually possible!

If you want to experiment:


4 Responses to “Spring MVC and JRuby controllers”

  1. 1 erikvanoosten

    Extending an interface is indeed weird. As you can read on
    http://headius.blogspot.com/2006/08/interfaces-should-be-modules.html
    this will change from

    [ruby]class MyController

  2. 2 erikvanoosten

    Ok, lets try this again:

    Extending an interface is indeed weird. As you can read on
    http://headius.blogspot.com/2006/08/interfaces-should-be-modules.html
    this will change from

    class MyController

  3. 3 erikvanoosten

    Extending an interface is indeed weird. As you can read on
    http://headius.blogspot.com/2006/08/interfaces-should-be-modules.html
    this will change from

    class MyController <Controller
    ...
    end

    to

    class MyController
    include Controller
    ...
    end

  4. 4 peter

    Ah, looks much nicer that way... thanks for pointing it out!

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


golden_gate_warning_sign Tim Bray introducing the (J)Ruby panel Charles Nutter & Guillaume Laforge Okke en Rudie Rudie Moscone Center - JavaOne javaone 2008 goodybag javaone2008 keynote Hotel room Stage being build in the nearby park Scribbled Sun Logo Stretched Limo alcatraz Acme Anvile at CommunityOne Keynote Greenland Joshua Bloch at JavaOne2008 Community One Keynote Golden Gate pub smashmouth
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.