Using Vaadin with Scala
Scala is a multi-paradigm language integrating object-oriented and functional programming. Read more about Scala on http://www.scala-lang.org.
Getting ready
Scala installation is quite easy. Just go to http://www.scala-lang.org/downloads, download Scala, and set up system variables as follow:
- Linux/Mac
SCALA_HOME=/Users/John/Installations/scala export SCALA_HOME export PATH=$PATH:$SCALA_HOME/bin
- Windows
%SCALA_HOME% = c:\Scala %PATH% = %PATH%;%SCALA_HOME%\bin
We have to maintain our project anyhow and we are going to utilize Gradle.
Tip
There is also another way to manage Scala projects. It is called Typesafe and more info is on http://typesafe.com.
How to do it...
Carry out the following steps in order to create a new Scala project:
- Make the project structure so we have folders where we can put our project files.
mkdir -p vaadin-in-scala/src/main/{scala/app,webapp/WEB-INF}
- Make
build.gradle
in the project root. It is going to be just a few lines that are necessary for running the project.apply plugin: 'war' apply plugin: 'jetty' apply plugin: 'scala' repositories { mavenCentral() } dependencies { scalaTools 'org.scala-lang:scala-compiler:2.10.0' scalaTools 'org.scala-lang:scala-library:2.10.0' compile 'org.scala-lang:scala-library:2.10.0' compile group:'com.vaadin', name:'vaadin-server', version:'7.0.4' compile group:'com.vaadin', name:'vaadin-client', version:'7.0.4' compile group:'com.vaadin', name:'vaadin-client-compiled', version:'7.0.4' compile group:'com.vaadin', name:'vaadin-themes', version:'7.0.4' compile group:'com.vaadin', name:'vaadin-client-compiler', version:'7.0.4' }
- Create a new Scala class
MyVaadinUI
that we place into theMyVaadinUI.scala
file in the foldersrc/main/scala/app
.package app import com.vaadin.ui._ import com.vaadin.server.VaadinRequest class MyVaadinUI extends UI { def init(request: VaadinRequest) = { val layout = new VerticalLayout() layout.setMargin(true) setContent(layout) layout.addComponent(new Label("Hello Vaadin user.")) } }
- Add the
web.xml
file to thesrc/main/webapp/WEB-INF
folder.<?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"> <display-name>Vaadin Web Application</display-name> <context-param> <description>Vaadin production mode</description> <param-name>productionMode</param-name> <param-value>false</param-value> </context-param> <servlet> <servlet-name>Vaadin Application Servlet</servlet-name> <servlet-class>com.vaadin.server.VaadinServlet</servlet-class> <init-param> <description>Vaadin UI to display</description> <param-name>UI</param-name> <param-value>app.MyVaadinUI</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Vaadin Application Servlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
- We are done and we can run the application.
gradle jettyRun
The following output should be displayed:
:compileJava UP-TO-DATE :compileScala UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE > Building > :jettyRun > Running at http://localhost:8080/vaadin-in-scala
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
How it works...
We have made a Gradle project in which we have applied the scala
plugin. The Scala plugin inside the Gradle build script ensures that all the .scala
files inside the src/main/scala
source folder will be compiled. Scala files are compiled to .class
files that are then deployed on the Jetty webserver.
The deployment descriptor (web.xml
file) defines one servlet. When users access the URL with the /*
pattern, which is mapped to the Vaadin servlet, MyVaadinUI
is shown in the browser.
The important thing we need to check is the init-param
element of the Vaadin servlet. It needs to point exactly to the UI class, which represents our application. The path to the UI class must be the full name of the class together with the package, for example, app.MyVaadinUI
.
See also
- Scaladin is a Vaadin add-on that makes using Vaadin with the Scala language easier. More information can be found on the Scaladin add-on page at https://vaadin.com/directory#addon/scaladin.