Using Vaadin with Scala

You can use Vaadin with any JVM compatible language, such as Scala or Groovy.

This document will help you to set up a Hello World project in Vaadin with Scala using Maven.

It is assumed you already have Java and Maven pre-installed.

Getting Started

  • Run the Maven archetype below to get a simple app created:

bash

  1. mvn archetype:generate -DarchetypeGroupId=com.vaadin -DarchetypeArtifactId=vaadin-archetype-application -DarchetypeVersion=8.0.5 -DgroupId=com.pany -DartifactId=ui -Dversion=1.0-SNAPSHOT -Dpackaging=war

Note that at this point you could import the project to and IDE like IntelliJ.

  • You now should have the Vaadin Java project under the directory ui. Since we are doing Scala, delete the java directory in ${project_home}/src/main/ and create an empty scala directory in the same place.

  • Add the following Scala dependency and…​

XML

  1. <dependency>
  2. <groupId>org.scala-lang</groupId>
  3. <artifactId>scala-library</artifactId>
  4. <version>2.12.1</version>
  5. </dependency>

…​plugin to your ‘pom.xml’

XML

  1. <plugin>
  2. <groupId>net.alchim31.maven</groupId>
  3. <artifactId>scala-maven-plugin</artifactId>
  4. <version>3.2.2</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>compile</goal>
  9. <goal>testCompile</goal>
  10. </goals>
  11. </execution>
  12. </executions>
  13. <configuration>
  14. <scalaCompatVersion>2.12</scalaCompatVersion>
  15. <scalaVersion>2.12</scalaVersion>
  16. </configuration>
  17. </plugin>
  • Create the following class in the new-created scala directory.

scala

  1. package com.mycompany.myproject
  2. import java.util.Date
  3. import javax.servlet.annotation.WebServlet
  4. import com.vaadin.annotations.{Theme, VaadinServletConfiguration}
  5. import com.vaadin.server.{VaadinRequest, VaadinServlet}
  6. import com.vaadin.ui.Button.{ClickEvent, ClickListener}
  7. import com.vaadin.ui._
  8. @WebServlet(urlPatterns = Array("/*"), name = "MyScalaUIServlet", asyncSupported = true)
  9. @VaadinServletConfiguration(ui = classOf[MyScalaUI], productionMode = false)
  10. class MyScalaUIServlet extends VaadinServlet {
  11. }
  12. @Theme("mytheme")
  13. class MyScalaUI extends UI {
  14. override def init(request: VaadinRequest): Unit = {
  15. val content: VerticalLayout = new VerticalLayout
  16. setContent(content)
  17. val label: Label = new Label("Hello, world!")
  18. content addComponent label
  19. // Handle user interaction
  20. content addComponent new Button("Click Me from Scala!",
  21. new ClickListener {
  22. override def buttonClick(event: ClickEvent): Unit =
  23. Notification.show("The time is " + new Date)
  24. })
  25. }
  26. }
  • Now just execute:

bash

  1. mvn clean package jetty:run -Dvaadin.productionMode=true
  • You should get logs containing something similar to:

bash

  1. [INFO] Started ServerConnector@15e41fff{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
  2. [INFO] Started @9194ms
  3. [INFO] Started Jetty Server
  4. [INFO] Using Non-Native Java sun.nio.fs.PollingWatchService

The important information here is that Jetty is now running on port 8080

  • Go to localhost:8080 from the web browser and the page should be working:

hello world scala page

  • If you can see the page above, congratulations you have created your first Vaadin with Scala app.

Defining Listeners with Lambda Expressions

Scala does not support use of lambda expressions for calling functional interfaces, like Java 8 does. Hence, we can’t just use a lambda expression for the ClickListener in the example above. You can, however, define implicit conversions from lambda expressions to such interface implementations. For example, for click listeners:

scala

  1. implicit def clickListener(f: ClickEvent => Unit) =
  2. new ClickListener {
  3. override def buttonClick(event: ClickEvent) {
  4. f(event)
  5. }
  6. }

You could then use a lambda expression as follows:

scala

  1. content addComponent new Button("Click Me!",
  2. (event: ClickEvent) =>
  3. Notification.show("The time is " + new Date))