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]
require ‘java’

include_class ‘org.springframework.web.servlet.ModelAndView’
include_class ‘org.springframework.web.servlet.mvc.Controller’

class MyController < Controller
def handleRequest(request, response)
puts "I'm in Ruby"
mav = ModelAndView.new @@resultView
mav.addObject("key","value")

return mav
end

def setResultView(resultView)
@@resultView = resultView
end

def toString
return "MyController in Ruby"
end
end

# needed by spring
MyController.new
[/ruby]

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]
script-source="/WEB-INF/Ruby/MyController.rb"
script-interfaces="org.springframework.web.servlet.mvc.Controller">


[/xml]

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:

This entry was posted in java, ruby. Bookmark the permalink.

4 Responses to Spring MVC and JRuby controllers

  1. erikvanoosten says:

    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. erikvanoosten says:

    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. erikvanoosten says:

    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. peter says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>