You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started-scala.asciidoc 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ---
  2. title: Using Vaadin with Scala
  3. order: 1000
  4. layout: page
  5. ---
  6. [[getting-started.scala]]
  7. = Using Vaadin with Scala
  8. You can use Vaadin with any JVM compatible language, such as Scala or Groovy.
  9. This document will help you to set up a **Hello World** project in Vaadin with Scala using Maven.
  10. *It is assumed you already have Java and Maven pre-installed.*
  11. ## Getting Started
  12. * Run the Maven archetype below to get a simple app created:
  13. ```bash
  14. 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
  15. ```
  16. _Note that at this point you could import the project to and IDE like IntelliJ._
  17. * 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.
  18. * Add the following Scala dependency and...
  19. ```xml
  20. <dependency>
  21. <groupId>org.scala-lang</groupId>
  22. <artifactId>scala-library</artifactId>
  23. <version>2.12.1</version>
  24. </dependency>
  25. ```
  26. ...plugin to your 'pom.xml'
  27. ```xml
  28. <plugin>
  29. <groupId>net.alchim31.maven</groupId>
  30. <artifactId>scala-maven-plugin</artifactId>
  31. <version>3.2.2</version>
  32. <executions>
  33. <execution>
  34. <goals>
  35. <goal>compile</goal>
  36. <goal>testCompile</goal>
  37. </goals>
  38. </execution>
  39. </executions>
  40. <configuration>
  41. <scalaCompatVersion>2.12</scalaCompatVersion>
  42. <scalaVersion>2.12</scalaVersion>
  43. </configuration>
  44. </plugin>
  45. ```
  46. * Create the following class in the new-created `scala` directory.
  47. ```scala
  48. package com.mycompany.myproject
  49. import java.util.Date
  50. import javax.servlet.annotation.WebServlet
  51. import com.vaadin.annotations.{Theme, VaadinServletConfiguration}
  52. import com.vaadin.server.{VaadinRequest, VaadinServlet}
  53. import com.vaadin.ui.Button.{ClickEvent, ClickListener}
  54. import com.vaadin.ui._
  55. @WebServlet(urlPatterns = Array("/*"), name = "MyScalaUIServlet", asyncSupported = true)
  56. @VaadinServletConfiguration(ui = classOf[MyScalaUI], productionMode = false)
  57. class MyScalaUIServlet extends VaadinServlet {
  58. }
  59. @Theme("mytheme")
  60. class MyScalaUI extends UI {
  61. override def init(request: VaadinRequest): Unit = {
  62. val content: VerticalLayout = new VerticalLayout
  63. setContent(content)
  64. val label: Label = new Label("Hello, world!")
  65. content addComponent label
  66. // Handle user interaction
  67. content addComponent new Button("Click Me from Scala!",
  68. new ClickListener {
  69. override def buttonClick(event: ClickEvent): Unit =
  70. Notification.show("The time is " + new Date)
  71. })
  72. }
  73. }
  74. ```
  75. * Now just execute:
  76. ```bash
  77. mvn clean package jetty:run -Dvaadin.productionMode=true
  78. ```
  79. * You should get logs containing something similar to:
  80. ```bash
  81. [INFO] Started ServerConnector@15e41fff{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
  82. [INFO] Started @9194ms
  83. [INFO] Started Jetty Server
  84. [INFO] Using Non-Native Java sun.nio.fs.PollingWatchService
  85. ```
  86. _The important information here is that Jetty is now running on port 8080_
  87. * Go to `localhost:8080` from the web browser and the page should be working:
  88. image::img/hello-world-scala-page.png[]
  89. * If you can see the page above, congratulations you have created your first Vaadin with Scala app.
  90. ifdef::web[]
  91. [[getting-started.scala.lambdas]]
  92. == Defining Listeners with Lambda Expressions
  93. Scala does not support use of lambda expressions for calling functional
  94. interfaces, like Java 8 does. Hence, we can't just use a lambda expression for
  95. the [interfacename]#ClickListener# in the example above. You can, however,
  96. define implicit conversions from lambda expressions to such interface
  97. implementations. For example, for click listeners:
  98. [source, scala]
  99. ----
  100. implicit def clickListener(f: ClickEvent => Unit) =
  101. new ClickListener {
  102. override def buttonClick(event: ClickEvent) {
  103. f(event)
  104. }
  105. }
  106. ----
  107. You could then use a lambda expression as follows:
  108. [source, scala]
  109. ----
  110. content addComponent new Button("Click Me!",
  111. (event: ClickEvent) =>
  112. Notification.show("The time is " + new Date))
  113. ----
  114. endif::web[]