DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Incident Response Guide
  • Demystifying SPF Record Limitations

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Incident Response Guide
  • Demystifying SPF Record Limitations
  1. DZone
  2. Coding
  3. Java
  4. Vaadin With Scala

Vaadin With Scala

Vaadin's Scala documentation doesn't include a full example, so here's how to get your app up and running from start to finish.

Fabio Serragnoli user avatar by
Fabio Serragnoli
·
Updated May. 26, 17 · Tutorial
Like (7)
Save
Tweet
Share
11.37K Views

Join the DZone community and get the full member experience.

Join For Free

This post shows a fully working 'Hello World' application in Vaadin 8 with Scala 2.12 running on Jetty.

Following my frustrated attempt to Dockerize an Ionic application as the Web UI of my petty Akka project, I started evaluating the latest version of Vaadin.

I followed the "Using Vaadin with Scala" documentation to create a 'Hello World', but the documentation doesn't contain a fully working example. So I've put the steps below to help others who might be in the same situation as I was.

Getting Started

Run the Maven archetype below to get a simple app created.

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


This archetype will generate a Java project. 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 Scala dependency and plugin to the pom.xml

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.12.1</version>
</dependency>
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.2.2</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <scalaCompatVersion>2.12</scalaCompatVersion>
        <scalaVersion>2.12</scalaVersion>
    </configuration>
</plugin>


Create the following class inside the new scala directory. 

import java.util.Date
import javax.servlet.annotation.WebServlet

import com.vaadin.annotations. {
    Theme,
    VaadinServletConfiguration
}
import com.vaadin.server. {
    VaadinRequest,
    VaadinServlet
}
import com.vaadin.ui.Button. {
    ClickEvent,
    ClickListener
}
import com.vaadin.ui._

@WebServlet(urlPatterns = Array("/*"), name = "MyScalaUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = classOf[MyScalaUI], productionMode = false)
class MyScalaUIServlet extends VaadinServlet {
    /* 
     * This servlet is missing in the 
     * "Using Vaadin with Scala" documentation
     */
}

@Theme("mytheme")
class MyScalaUI extends UI {

    override def init(request: VaadinRequest): Unit = {
        val content: VerticalLayout = new VerticalLayout
        setContent(content)

        val label: Label = new Label("Hello, world!")
        content addComponent label

        // Handle user interaction
        content addComponent new Button("Click Me from Scala!",
            new ClickListener {
                override def buttonClick(event: ClickEvent): Unit =
                    Notification.show("The time is " + new Date)
            })
    }
}


Now just execute:

mvn clean package jetty:run -Dvaadin.productionMode=true

There should be something similar to the below in the logs. The important information here is that Jetty is running on port 8080.

Image title

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

Image title


Scala (programming language) Vaadin

Opinions expressed by DZone contributors are their own.

Trending

  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Incident Response Guide
  • Demystifying SPF Record Limitations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: