Ruby On Spring
The goals of the project are to permit rapid innovation and iteration, full reuse of our existing Spring and Hibernate business logic and objects, and a smooth transition from prototype to production code.
We were inspired by many of the benefits and philosophy of Ruby on Rails, including avoiding configuration files and compilation. Make a change, and you’ll see it immediately. At the same time, if you’re already commited to Spring, the package doesn’t require you to learn a new framework or abandon your existing investment in Spring. The total size of the package is less than a 1000 lines, so it’s small and easily integrated into existing projects.
The key part of the system is a Spring controller that instantiates a JRuby script engine and executes a JRuby script to define the controller and the model. The view is provided by a standard JSP file. If the JRuby controller is replaced by a Java one, the view can be used as-is with no changes.
Hello World
The simplest possible example consists of a JRuby script “hello.rb” which returns a model object and a JSP view “hello.jsp” which uses that model. To try the example, I just need to drop these two files into the “rad” directory on my Tomcat server. No compilation, configuration files, or server restarts are required.hello.rb
{ ‘greeting’ => ‘Hello world’}
hello.jsp
<html>
${command[’greeting’]}
</html>
hello.rb
class DemoControllerdef getModel(request)
{ ‘greeting’ => ‘Hello world’}
end
end
DemoController.new()
Reusing Spring Beans
Here’s a more interesting example that reuses business logic from a Spring bean implemented in Java. ($context represents the Spring application context.) Thanks to the magic of JRuby, we can interact and create Java objects just as easily as Ruby ones.location.rb
locationCalculator = $context.getBean(’locationCalculator’) {’locations’ => locationCalculator.getPointMatches(’seattle’)}
Hibernate Queries
The package also supports Hibernate. The follow example uses Hibernate to fetch a list of company objects starting with a given letter. Note that the query returns full fledged Java objects with behavior, not just data.hibernate.rb
s = $request.getParameter(’s’) companies = $helper.queryHibernate(”from jobster.model.Company where name>=?”, [s])
{’companies’ => companies, ‘label’ => “Companies starting with #{s}”}
hibernate.jsp
<div class=”left pad”><div class=”bold”>Companies: ${command.label}</div>
<ol>
<c:forEach var=”company” items=”${command.companies}”>
<li> ${company.name} <br />
</li></c:forEach>
</ol>
</div>
onSubmit Handlers
Our final example is a simple calculator controller. It implements an onSubmit handler to run business logic (computing the calculation) when the user submits the form and choose a new result view.calculator.rb
class CalculatorControllerdef getModel(request)
{’x’ => 2, ‘y’ => 2}
end
def onSubmit(request, response, form, errors)
form[’total’] = form[’x'].to_i + form[’y'].to_i
form[’viewName’] = ‘/rad/calculator_results’
return form
end
end
CalculatorController.new()
9 Comments so far
Leave a comment
I am interested in your example. I work with a guy that helps with JRuby, and I am planning a presentation on Spring 2. I would be glad to play with you framework and provide feedback. I am interested in scripting out the service layer of apps with Ruby or another scripting language.
By Adam Waldal on 01.03.06 11:47 pm
My email is adam.waldal@objectpartners.com
By Adam Waldal on 01.03.06 11:49 pm
Trackback!
I just stumbled across your blog, Adam, and posted a link to this entry on my own. I’m one of the two active developers on JRuby now and will be the lead presenter at JavaOne. It’s really cool to see JRuby put to such good use, and I hope you keep it up. Thanks!
By Charles Oliver Nutter on 01.27.06 6:43 am
[…] Interessante esta entrada do Phil Bogle. […]
By Fragmental » Ruby + Spring on 02.01.06 3:06 pm
Hi Phil,
This looks really interesting. I played around with a similar concept a while back but using Rhino, but never had a chance to complete it. I would would love to take a look at this.
Thanks,
John
By john cavacas on 03.06.06 5:28 pm
I couldnt get these examples to work. I sent you an email on it. For example, missing classes? And I think the directories werent right; WEB-INF/jsps?
etc. But still a good approach.
By Berlin Brown on 10.12.06 9:13 pm
I redid some of the example here. I got the calculator working and described it a little here:
www dot botspiritcompany dot com/src/springruby/example_ruby_spring.html
By Berlin Brown on 11.04.06 6:34 pm
Wow, very cool and useful,thanks a lot,
By ivan on 02.28.07 10:41 pm
Hii,
I am new to Springs. Rite now I have downloaded the Springs Framework. I use Eclips, Anyone could tell me step by step developing the application in springs only “Hello World” program and also forward mail also same steps.
By koustubh K. on 03.10.07 2:52 am
Leave a comment