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.
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.
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
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.2.2</version> </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.
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:
IndexBean.groovy
package net.javabeat.jsf import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @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.
5. The View
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <f:view> <h:form prependId="false"> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 &amp; Groovy</h2> <br/> <h:outputText value="#{indexBean.message}"></h:outputText> <h:inputText value="#{indexBean.userInput}"></h:inputText> <br/> <h:commandButton value="Navigate Using Groovy Action" action="#{indexBean.navigate}"/> </h:form> </f:view> </html>
anonymousView.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <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> </h:form> </html>
6. Faces Configuration File
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <resource-bundle> <base-name>net.javabeat.jsf.application</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
7. The Deployment Descriptor
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
8. JSF & Groovy Integration Demo
The below snapshots show you a running demonstration for a jsf application does use a Groovy programming language.
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.
[wpdm_file id=55]