JSF + Scala Integration

Scala is a popular programming language for the Java Virtual Machine (JVM). Like Java, it is strongly typed and object-oriented, but it also supports functional programming. Many developers are attracted to Scala because it requires less boilerplate for common constructs, such as properties. Scala does support calling a code in the Java Library from it, so it’s possible to integrate the JavaServer Faces (JSF Framework) with Scala. This tutorial you’re going to install and configure the created jsf maven project to support using of Scala.

Tools Required For JSF And Scala Integration

  1. Eclipse IDE 4.3 (Kepler)
  2. Tomcat Servlet Runner (7.0.35)
  3. Scala 2.10.4
  4. JSF 2.0

1. Install Scala IDE for Eclipse

We are going to install the Scala in the installed Eclipse IDE. By using the scala download link you can install the Scala IDE for Eclipse for enabling the Scala tools, compiler and perspective. The steps required for installing the Scala are as following:

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

Install Scala IDE for Eclipse

2. Add Scala Nature

If you’ve tried to create any type of Scala files (Scala class, objects, etc ..) you’re surely about facing a complexity for doing that for being the created JSF maven project that imported into eclipse doesn’t support Scala nature.

For adding the Scala nature 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 Add Scala Nature.
  • From Project menu select clean.
  • For more familiarity using of scala, you have a new scala perspective. From Window menu, select Open perspective and choose Scala. If you cannot show such that perspective choose Other and select Scala again.

By doing all of the above steps, you have the ability to create scala assets easily.

JSF 2 Scale Adding Scale Nature

JSF 2 Scala Perspective

3. Scala Library

Make sure that once you’ve added a scala nature to your project the scala library will be added for it. Just look at your project structure and beside libraries such Maven Dependencies and JRE System Library you should see Scala Library [Version].

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

  • Right click on your project.
  • From Scala Menu, select Add Scala Library To Build Path. (Someone may ask, if you are using a maven why not add those libraries into maven dependencies).
  • For those interesting in the maven and only maven add the scala dependency to your pom.xml.

Scala 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 Scala Adding Library

JSF 2 Scala Libraries

4. Create Scala Class (Managed Bean)

To create a scala 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 Scala Class.
  • Name your newly created scala class as IndexBean.
  • Staring to write a scala class.

Your class should look like the below one:

package net.javabeat.jsf

import javax.faces.bean.ManagedBean
import scala.beans.BeanProperty

class IndexBean {
	var name = "JavaBeat !";
	var message = "Hello";

	def navigate(): String ={
	  return "anonymousView";
  • Note using of javax.faces.bean.ManagedBean annotation to annotate a scala class
  • Scala provides you another type of annotations called @BeanProperty that annotate the scala class properties.
  • IndexBean scala class defines two properties and one method named navigate that doesn’t consider any parameters and it will return a string.

JSF 2 Adding Scala 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;amp; Scala</h2>
			<h:outputText value="#{indexBean.message}"></h:outputText>
			<h:outputText value="#{indexBean.name}"></h:outputText>
			<h:commandButton value="Navigate Using Scala" 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;amp; Scala</h2>
			<h3>Anonymous View</h3>

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 + Scala Integration Demo

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

JSF 2 Scala Example 1

JSF 2 Scala Example 2

  • You’ve achieved the rendering of Scala class (Managed Bean) properties.
  • You’ve navigated into another view using the Scala class (Managed Bean) action method.

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!