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

<dependency>
	<groupId>org.scala-lang</groupId>
	<artifactId>scala-library</artifactId>
	<version>2.10.4</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.

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:
IndexBean.scala

package net.javabeat.jsf

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

@ManagedBean
class IndexBean {
	@BeanProperty
	var name = "JavaBeat !";
	@BeanProperty
	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

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

Comments

comments

About Amr Mohammed

Speak Your Mind

*