--- title: Using Vaadin with Scala order: 1000 layout: page --- [[getting-started.scala]] = 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 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 org.scala-lang scala-library 2.12.1 ``` ...plugin to your 'pom.xml' ```xml net.alchim31.maven scala-maven-plugin 3.2.2 compile testCompile 2.12 2.12 ``` * Create the following class in the new-created `scala` directory. ```scala package com.mycompany.myproject 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 { } @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: ```bash mvn clean package jetty:run -Dvaadin.productionMode=true ``` * You should get logs containing something similar to: ```bash [INFO] Started ServerConnector@15e41fff{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} [INFO] Started @9194ms [INFO] Started Jetty Server [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: image::img/hello-world-scala-page.png[] * If you can see the page above, congratulations you have created your first Vaadin with Scala app. ifdef::web[] [[getting-started.scala.lambdas]] == 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 [interfacename]#ClickListener# in the example above. You can, however, define implicit conversions from lambda expressions to such interface implementations. For example, for click listeners: [source, scala] ---- implicit def clickListener(f: ClickEvent => Unit) = new ClickListener { override def buttonClick(event: ClickEvent) { f(event) } } ---- You could then use a lambda expression as follows: [source, scala] ---- content addComponent new Button("Click Me!", (event: ClickEvent) => Notification.show("The time is " + new Date)) ---- endif::web[] ture/dnd Vaadin 6, 7, 8 is a Java framework for modern Java web applications: https://github.com/vaadin/frameworkwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/documentation/getting-started/getting-started-netbeans.asciidoc
blob: 64948471fec63b53004b6ee9c0325398ced44fa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106