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"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:form>
<h:inputText value="#{myBean.text}"></h:inputText>
<h:commandButton action="#{myBean.click}" value="Submit"></h:commandButton>
You typed: #{myBean.text}
</h:form>
</html>
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 end def getText @text end def save puts @text end end
Final step is to define this bean as a spring bean;
<lang:jruby id="myBean" script-interfaces="com.prime.tutorial.moviestore.view.scripting.MyBeanController" script-source="classpath:com/prime/tutorial/moviestore/view/scripting/MyBeanController.rb" scope="request"> </lang:jruby>
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.