From b07c97b0d07567269b69c2486ddbc1923f81b679 Mon Sep 17 00:00:00 2001 From: Fabio Serragnoli Date: Mon, 7 Aug 2017 14:06:41 +0100 Subject: Update getting-started-scala.asciidoc (#9418) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per suggestion of Fredrik Rönnlund following https://dzone.com/articles/working-vaadin-with-scala --- .../getting-started/getting-started-scala.asciidoc | 128 +++++++++++++-------- .../getting-started/img/hello-world-scala-page.png | Bin 0 -> 88314 bytes 2 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 documentation/getting-started/img/hello-world-scala-page.png (limited to 'documentation') diff --git a/documentation/getting-started/getting-started-scala.asciidoc b/documentation/getting-started/getting-started-scala.asciidoc index 85d4bec191..b3705b6a38 100644 --- a/documentation/getting-started/getting-started-scala.asciidoc +++ b/documentation/getting-started/getting-started-scala.asciidoc @@ -8,44 +8,70 @@ layout: page = Using Vaadin with Scala You can use Vaadin with any JVM compatible language, such as Scala or Groovy. -There are, however, some caveats related to libraries and project set-up. In the -following, we give instructions for creating a Scala UI in Eclipse, with the -Scala IDE for Eclipse and the Vaadin Plugin for Eclipse. - -. Install the link:http://scala-ide.org/[Scala IDE for Eclipse], either from an -Eclipse update site or as a bundled Eclipse distribution. - -. Open an existing Vaadin Java project or create a new one as outlined in -<>. You can delete the UI class created by -the wizard. - -. Switch to the Scala perspective by clicking the perspective in the upper-right -corner of the Eclipse window. - -. Right-click on the project folder in [guilabel]#Project Explorer# and select -"Configure > Add Scala Nature". - -. The web application needs [filename]#scala-library.jar# in its class path. If -using Scala IDE, you can copy it from somewhere under your Eclipse installation -to the class path of the web application, that is, either to the -[filename]#WebContent/WEB-INF/lib# folder in the project or to the library path -of the application server. If copying outside Eclipse to a project, refresh the -project by selecting it and pressing kbd:[F5]. - -+ -You could also get it with a Maven dependency, just make sure that the -version is same as what the Scala IDE uses. - - -You should now be able to create a Scala UI class, such as the following: +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 { +} -[source, scala] ----- @Theme("mytheme") class MyScalaUI extends UI { - override def init(request: VaadinRequest) = { + + override def init(request: VaadinRequest): Unit = { val content: VerticalLayout = new VerticalLayout setContent(content) @@ -53,28 +79,34 @@ class MyScalaUI extends UI { content addComponent label // Handle user interaction - content addComponent new Button("Click Me!", + content addComponent new Button("Click Me from Scala!", new ClickListener { - override def buttonClick(event: ClickEvent) = + override def buttonClick(event: ClickEvent): Unit = Notification.show("The time is " + new Date) }) } } ----- +``` -Eclipse and Scala IDE should be able to import the Vaadin classes automatically -when you press kbd:[Ctrl+Shift+O]. +* Now just execute: +```bash +mvn clean package jetty:run -Dvaadin.productionMode=true +``` -You need to define the Scala UI class either in a servlet class (in Servlet 3.0 -project) or in a [filename]#web.xml# deployment descriptor, just like described -in -<> for Java UIs. +* 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_ -ifdef::web[] -The link:https://github.com/henrikerola/scaladin[Scaladin add-on] offers a more -Scala-like API for Vaadin. A Vaadin 7 compatible version is under development. -endif::web[] +* 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]] diff --git a/documentation/getting-started/img/hello-world-scala-page.png b/documentation/getting-started/img/hello-world-scala-page.png new file mode 100644 index 0000000000..10b03b710e Binary files /dev/null and b/documentation/getting-started/img/hello-world-scala-page.png differ -- cgit v1.2.3