Facing new challenges everyday

Dynamic languages and JSF

March 6th, 2008

Yesterday I started to think about how can I use languages like Groovy, JRuby, Jython or Scala with JSF, after some research I found a elegant way to do it, in order to do that, you must add the following jars to your project:

  • antlr-2.7.6.jar
  • aopalliance-1.0.jar
  • asm-2.2.3.jar
  • commons-beanutils-1.7.0.jar
  • commons-codec-1.3.jar
  • commons-collections-3.1.jar
  • commons-digester-1.6.jar
  • commons-el-1.0.jar
  • commons-lang-2.1.jar
  • commons-logging-1.0.4.jar
  • commons-validator-1.2.0.jar
  • groovy-1.5.4.jar
  • myfaces-api-1.1.5-SNAPSHOT.jar
  • myfaces-impl-1.1.5-SNAPSHOT.jar
  • oro-2.0.8.jar
  • spring-aop-2.5.1.jar
  • spring-beans-2.5.1.jar
  • spring-context-2.5.1.jar
  • spring-core-2.5.1.jar
  • spring-web-2.5.1.jar

Now you must create the necessary configuration files:

web.xml
[code lang="xml"]



test-groovy

contextConfigLocation /WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener


Faces Servlet

javax.faces.webapp.FacesServlet

1


Faces Servlet
*.jsf


index.jsp


[/code]

faces-config.xml
[code lang="xml"]

"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">




org.springframework.web.jsf.DelegatingVariableResolver



[/code]

applicationContext.xml
[code lang="xml"]

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">

script-source="classpath:com/company/groovy/beans/SayHelloPage.groovy"/>


[/code]

Here’s how your groovy code looks like:

sayHelloPage.groovy
[code lang="groovy"]
package br.eti.faces.groovy.beans

class SayHelloPage {

private String name = "someone"

public String sayHello() {
name = "Hello " + name + "!"
return null
}

public String getName() {
return name
}

public void setName(String name) {
this.name = name
}
}
[/code]

You can use this groovy bean in your jsf pages in the same way as any other java bean:

[code lang="html4strict"]
<%@ page session="false"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>













[/code]

Note: The script-source attribute of <lang:groovy/> or <lang:yourlanguage> tag must point to a absolute path of your classpath where your script file resides.

Please refer to dynamic languages chapter of spring documentation for more details.

Post to Twitter

1 Comment »

  1. Rafael Ponte says

    Great post Rogerio!!!

    The dynamic languages are very much famous currently, and unites JSF and Groovy is a great idea, congratulations!

    However, in the groovy code i think that you don\’t need to declare the getters and setters methods for its attributes, the groovy generates them dynamically in runtime :) well, i think so.

    Great post!

    March 10th, 2008 | #

Leave a comment

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad:
Imagem CAPTCHA

RSS feed for these comments. | TrackBack URI

Visitors Around the World

Polls

How Is My Site?

View Results

Loading ... Loading ...

Categories

Meta

Links

hosted by easy2use.net