JSF + Groovy Integration

Groovy is another popular programming language for the Java Virtual Machine. Groovy is dynamically typed, and groovy code can be more concise than the equivalent Java because you can omit types of variables and parameters.

Almost any Java code is legal groovy, that’s make it simple and easier for getting started using it. Ultimately, using the Groovy within JSF applications isn’t more complex for one reason the using of groovy compiler to compile your groovy code does generate a .class file. In JSF, it doesn’t know, or care that you’re using groovy to generate them.

The JSF reference implementation  supports hot deployment of your Groovy code. When you change the groovy source files, the JSF implementation automatically recompile it and deploys the class files to the web application.

This tutorial you are going to integrate the Groovy with the JavaServer Faces using support of Eclipse IDE. This tutorial you will use the already created jsf maven project.

Tools Required For JSF + Groovy Integration

  • Eclipse IDE 4.3 (Kepler)
  • Tomcat Servlet Runner (7.0.35)
  • Groovy 2.0.x
  • JSF 2.0

1. Install Groovy Plugin for Eclipse

By using the Groovy download link you can choose the proper plugin for your Eclipse, but in our case you a have consider the link that provided for Kepler version. The steps required for installing the Groovy is as following:

  • Open Eclipse IDE
  • From Help menu choose Install New Software.
  • Inside Work with type the Groovy download link mentioned above.
  • From the fetched list, select Groovy-Eclipse.

Groovy Eclipse IDE - Download Groovy Plugin

2. Add Groovy Nature

If you’ve tried to create any type of Groovy files (groovy class, project, etc ..) you’re surely about getting complexity, cause your imported maven project doesn’t support Groovy naturally.

For adding Groovy you have to follow the below steps:

  • Right click on the created JSF project that imported into your eclipse IDE.
  • From Configure choose the option Convert To Groovy Project.
  • From Project menu select clean.
  • There is no specific perspective for Groovy, cause it’s considered as Java Project.

JSF 2 Groovy Add Nature

3. Groovy Library

Make sure that once you’ve added a groovy nature to your project,  the groovy library in it’s turn being added for it. Just look at your project structure and beside libraries such Maven Dependencies and JRE System Library you should see Groovy Libraries Groovy DSL Support.

If you’ve not seen it, add it by the following steps:

  • Right click on your project.
  • From Grrovy Menu, select Add Groovy Library To Build Path.

But for those developers that focusing on using of maven you can consider the following dependency as mandatory.

Groovy Maven Dependency


Either you are using the library added by the eclipse or use the library added by the maven, you are getting the right way and your application should running smoothly.

JSF 2 Groovy Add Library

JSF 2 Groovy Added Libraries

4. Groovy Class (Managed Bean)

To create a Groovy class that would be considering later as a managed bean, you have to follow the below steps:

  • Right click on the already created package and select New.
  • From the sub menu of new select Groovy Class.
  • Name your newly created Groovy class as IndexBean.
  • Staring to write a groovy class.

Your class should look like the below one:


package net.javabeat.jsf

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

class IndexBean implements GroovyObject{
	private message = "Hello Mr. "; // Within groovy, you can omit the types
	private userInput; // Within groovy, you can omit the types

	public void setMessage(String message){
		this.message = message;

	public String getMessage(){
		return this.message;

	public void setUserInput(String userInput){
		this.userInput = userInput;

	public String getUserInput(){
		return this.userInput;

	public String navigate(){
		return "anonymousView";
  • Note using of properties without typos.
  • Note using of jsf2 @ManagedBean annotation
  • Note using of jsf2 @SessionScoped annotation
  • IndexBean groovy class defines two properties and one method named navigate that doesn’t consider any parameters and it will return a string.

JSF Groovy Adding Groovy Class

5. The View


<html xmlns="http://www.w3.org/1999/xhtml"
		<h:form prependId="false">
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 &amp; Groovy</h2>
			<h:outputText value="#{indexBean.message}"></h:outputText>
			<h:inputText value="#{indexBean.userInput}"></h:inputText>
			<h:commandButton value="Navigate Using Groovy Action" action="#{indexBean.navigate}"/>


<html xmlns="http://www.w3.org/1999/xhtml"
		<h:form prependId="false">
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 &amp; Groovy</h2>
			<h3>Anonymous View</h3>
			<h:outputText value="#{indexBean.message}"></h:outputText>
			#{' '}
			<h:outputText value="#{indexBean.userInput}"></h:outputText>

6. Faces Configuration File


<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"

7. The Deployment Descriptor


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5" metadata-complete="true">
		<description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>

8. JSF & Groovy Integration Demo

The below snapshots show you a running demonstration for a jsf application does use a Groovy programming language.

JSF 2 Groovy Example 1

JSF 2 Groovy Example 2

9. java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

If you’ve followed the previous steps carefully, and your application doesn’t functional properly. Make sure that your Tomcat 7 has loaded the Groovy library in it’s classpath even your application has been compiled successfully. Also, you might be familiar with an error message that shown on the Eclipse console says

SEVERE: Unable to load annotated class: net.javabeat.jsf.IndexBean, reason: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

Even that your application has been running, but your groovy managed bean doesn’t initialized and try to navigate for seeing a big exception telling you that your managed bean is resolved to null.

Don’t worry, all things that you have to do is to assemble the groovy library into your server by following the below steps:

  • Right click on the project.
  • Choose properties, and point on Deployment Assembly.
  • Click Add button within the Web Deployment Assembly dialog.
  • Choose the Java Build Path Entries and click next.
  • Choose those libraries that mentioned for Groovy. and click finish.
  • Enjoy running your application.

JSF 2 Groovy Example 4

JSF 2 Groovy Example 5

JSF 2 Groovy Example 6

JSF 2 Groovy Example 7

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!