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"
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.

Posted in Java. 5 Comments »

Annotation Driven JSF-Spring-JPA-Spring Security-Orchestra

On my way to combine my favorite frameworks together, I posted an example application called moviestore demonstrating the annotation driven integration of JSF(Facelets)-Spring-JPA. .
Lately I’ve added Spring-Security and Apache MyFaces Orchestra to this formula to fix the missing parts for security and conversation-workflow management.

Here’s the final contents of moviestore;
- JSF with Facelets, Apache MyFaces 1.2 impl
- Spring 2.5.x
- JPA with Toplink
- Spring Security
- Apache MyFaces Orchestra

with jetty and hsqldb

The example contains two modes of new movie entity creation, one is done on single page and the other is the wizard mode and consists of 3 pages.Thanks to MyFaces Orchestra, creating wizards, flowScopes, viewScopes are no-brainer. Best part is that you dont get Lazy Exception since Orchestra manages the entity manager per conversation rescuing us from the openblablainviewfilter pattern. If you’re still not using Orchestra, give it a try, it’ll make your life much easier, simply request or session scope both are not enough for web applications and Orchestra is a life-saver. Again Persistence support
is a fantastic bonus.

Other addition to the moviestore example is the spring-security formerly known as acegi. I really liked the new namespace support, considering the number of xml lines with Acegi, Spring-Security is fun to use.

You can download the moviestore here. It uses in memory db and jetty so just running
mvn jetty:run is enough to test the moviestore in action. By the way,

username: tony
password: 55555

Posted in Java. 14 Comments »