Spring MVC and JRuby controllers
Published by peter August 9th, 2006 in java, ruby.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:
-
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
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:
-
<lang:jruby id="myController" refresh-check-delay="3000"
-
script-source="/WEB-INF/Ruby/MyController.rb"
-
script-interfaces="org.springframework.web.servlet.mvc.Controller">
-
<lang:property name="resultView" value="view" />
-
</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:




















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
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
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
Ah, looks much nicer that way... thanks for pointing it out!