JSF with Ruby

I was wondering if writing the backing bean code for JSF with ruby or groovy is a good idea or not after seeing Kito’s presentation in Austria JSFDays08 about this topic. So decided to give it a try. Spring’s dynamic language support is where I’ve started. Currently spring supports groovy, jruby and beanshell, in this example I’m using ruby.

Following is a simple xhtml jsf view;

<html xmlns="http://www.w3.org/1999/xhtml"

<h:inputText value="#{myBean.text}"></h:inputText>
<h:commandButton action="#{myBean.click}" value="Submit"></h:commandButton>
You typed: #{myBean.text}

The backing bean name is “myBean” with a text property, you can easily provide the code for this bean with java, but that’s just boring right:)
In order to implement this backing bean with ruby we need an interface for the spring-jruby integration;

public interface IMyBeanController {
public String getText();
public void setText(String text);
public String click();

Here’s the ruby backing bean that implements this interface;

require 'java'

include_class 'com.prime.tutorial.moviestore.view.scripting.IMyBeanController'

class MyBeanController
include IMyBeanController

@text = nil

def setText(text)
@text = text

def getText

def save
puts @text


Final step is to define this bean as a spring bean;

<lang:jruby id="myBean"
script-source="classpath:com/prime/tutorial/moviestore/view/scripting/MyBeanController.rb" scope="request">

Note that the scripted backing bean can also have a scope like request. Of course we need standard jsf-spring integration configuration to make this stuff work, you can check my older post about how to do it. By the way you can also use groovy or beanshell instead of ruby.

Although the idea of using a scripted language with JSF looks interesting there’re some downsides;

– I couldn’t make attr_reader :message work, so I had to write the accessors for the text property, what’s the point of using ruby if I’m going to use it as java right?
– Your scripted backing bean needs to implement a java interface for the integration, this just sucks.

Other than these downsides there’s a really cool part here, since the backing bean is scripted it can be reloaded without redeploying or reloading the application, just add refresh-check-delay=”5000″ to the bean definition in spring and the changes in the development environment will be reflected easily.

To sum up, sure the idea is cool but the cons make this a bit ugly.

5 Responses to JSF with Ruby

  1. Pingback: JSF with Ruby « Cagatay Civici’s Weblog

  2. I read a similar post somewhere before on the sun blogs, where some of their rails dudes were trying this. Interesting. BTW. both major JSF implementions have support for dynamic languages (mainly groovy) added recently. If done right… it shouldn’t be a big deal to actually support (J)Ruby there as well.

  3. Kito D. Mann says:

    Hey Cagatay. I’m glad to see you gave it a shot🙂. Matthias is right — the RI (and soon MyFaces – don’t know if it’s released yet) have Groovy support integrated. The RI can also re-load Faces config files too, which is very cool.

    For Spring support, you can actually get around implementing an interface (at least with Groovy) if you use a somewhat annoying syntax — you have to get to the Groovy class through the Spring proxy. I was talking to Mark Fisher from Spring Source about this — it’s something like “#{myGroovyBean.target.property}”. This could easily be wrapped in an ELResolver, though.

  4. Fred says:

    Their interface IMyBeanController have the setText, getText and click methods. But, the ruby class implements it with the setText, getText and SAVE methods.


%d bloggers like this: