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.

ScalaAndVaadinHOWTO.asciidoc 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ---
  2. title: Scala And Vaadin HOWTO
  3. order: 16
  4. layout: page
  5. ---
  6. [[scala-and-vaadin-how-to]]
  7. = Scala and Vaadin how-to
  8. [[introduction]]
  9. Introduction
  10. ~~~~~~~~~~~~
  11. Since Vaadin is a server-side library it works very well with all JVM
  12. languages, including Scala. This article provides instructions on how to
  13. get started with Vaadin using Scala. First, we'll go through setting up
  14. a new project. After that we'll introduce the Scaladin add-on and see
  15. how it enhances Vaadin components by adding features that leverage the
  16. power of Scala.
  17. [[creating-a-new-eclipse-vaadin-project-with-scala]]
  18. Creating a new Eclipse Vaadin project with Scala
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. [[installing-the-required-software-components]]
  21. Installing the required software components
  22. +++++++++++++++++++++++++++++++++++++++++++
  23. * Download and install http://eclipse.org/[Eclipse] Helios or Indigo by
  24. unpacking it to a location of your choice. Please note that only Eclipse
  25. Helios is officially supported by Scala IDE but also Indigo can be used.
  26. * Start Eclipse, and install the http://vaadin.com/eclipse[Vaadin
  27. Eclipse Plug-in] and http://www.scala-ide.org[Scala IDE Eclipse Plug-in]
  28. using the plug-in installation feature of Eclipse (available under
  29. `Help -> Install New Software...`).
  30. You also need a servlet container to run your application. In this
  31. example we use Tomcat, but any standard container (Jetty, JBoss,
  32. Glassfish, Oracle WebLogic, IBM WebSphere etc.) should be fine.
  33. * Download and install http://tomcat.apache.org/[Tomcat] by unpacking it
  34. to a location of your choice.
  35. * Add the server to Eclipse
  36. * Open the Servers view
  37. * Right click in the Servers view and choose `New -> Server`
  38. * Choose the type of your server, in this case `Apache -> Tomcat`
  39. * Choose the server runtime environment in the dialog by selecting the
  40. folder you unpacked Tomcat to.
  41. [[creating-a-new-project]]
  42. Creating a new project
  43. ++++++++++++++++++++++
  44. * Create a new Vaadin project in Eclipse:
  45. * Choose `File -> New...`
  46. * Choose `Other...`
  47. * Choose `Vaadin -> Vaadin Project` from the list. You can use the
  48. filter to narrow down the list.
  49. * Choose a name for your project, eg. "ScalaTest"
  50. The New Vaadin Project Wizard allows you to configure different aspects
  51. your project, but the defaults are fine.
  52. At this point you have a ready-to-go Vaadin Java project. To start doing
  53. Scala we need to do a few more things:
  54. * Add the Scala nature to your project: right click your project root,
  55. and choose `Configure -> Add Scala Nature` from the menu.
  56. * Navigate to the `src` folder, and delete the generated Java file under
  57. the default package (eg. `com.example.scalatest`)
  58. Next up, some Scala!
  59. * Add a new Scala class in your project: right click the default
  60. package, and choose `New -> Scala Class`
  61. * Choose a name for the class, eg. "ScalaApp"
  62. * Our new class should extend the `com.vaadin.Application`, so in the
  63. wizard, click the `Browse...` button next to the "Superclass" field, and
  64. choose that from the list.
  65. * Click "Finish" to let Eclipse generate the class.
  66. Now we need to write some code in the method of our new Vaadin
  67. application.
  68. * Open the `ScalaApp.scala`
  69. * Add the following lines in the `init()`
  70. method: `setMainWindow(new Window("Scala Rocks!"))` `getMainWindow.addComponent(new Label("Hello World!"))`
  71. You can let Eclipse add the imports as you go, or just import the Vaadin
  72. components `(import com.vaadin.ui._)` yourself. The resulting file
  73. should look like this:
  74. [source,javascript]
  75. ....
  76. import com.vaadin.Application
  77. import com.vaadin.ui._
  78. class ScalaApp extends Application {
  79. def init(): Unit = {
  80. setMainWindow(new Window("Scala Rocks!"))
  81. getMainWindow.addComponent(new Label("Hello World!"))
  82. }
  83. }
  84. ....
  85. Next we make sure the servlet container knows which class it should
  86. load.
  87. * Open `WebContent/WEB-INF/web.xml`
  88. * Under the `<web-app><servlet>` branch change the `param-value` of the
  89. `application` init-param to contain to your application class, including
  90. the package name. Eg. "com.example.scalatest.ScalaApp"
  91. [[additional-configuration]]
  92. Additional configuration
  93. ++++++++++++++++++++++++
  94. We're almost done. The last thing we need to do is make sure that the
  95. `scala-library.jar` is available at runtime. We do this by adding the
  96. JAR into the classpath of our servlet container.
  97. First, we need the JAR file itself. You already have this in the Scala
  98. IDE installation folder under Eclipse, or you can download the Scala
  99. distribution from http://www.scala-lang.org/downloads.
  100. We have a few options how to make sure the JAR is available at runtime.
  101. * Put the file in the `WEB-INF/lib` folder under your project.
  102. * Put the file directly in the lib folder of your servlet container.
  103. * Add the Scala library to the deployment assembly:
  104. `project properties -> Deployment assembly -> Add... -> Java build path entries`
  105. After you have done this we can fire up our application!
  106. [[running-the-application]]
  107. Running the application
  108. +++++++++++++++++++++++
  109. Running the application is simple
  110. * Right click your project, and choose `Run As -> Run On Server`
  111. * Choose the previously created Tomcat instance as the target. You might
  112. also want to check the "Always use this server when running this
  113. project" checkbox.
  114. Eclipse should then start the server and open the UI in a internal
  115. browser window.
  116. [[creating-a-new-project-using-a-giter8-template]]
  117. Creating a new project using a Giter8 template
  118. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  119. https://github.com/n8han/giter8[Giter8] is a command-line tool that
  120. generates project skeletons from templates that are published on GitHub.
  121. The Vaadin-Scala template creates the basic structure for a
  122. http://www.scala-sbt.org/[SBT]-project that has Vaadin, Scala
  123. and Scaladin included.
  124. First, install Giter8 following the instructions
  125. https://github.com/n8han/giter8#readme[on their readme]. Then just
  126. ....
  127. g8 ripla/vaadin-scala
  128. ....
  129. And answer the questions, or press enter for defaults. After that launch
  130. the server (jetty):
  131. ....
  132. cd <project dir>
  133. sbt
  134. container:start
  135. ....
  136. You can then browse to
  137. __[[http://localhost:8080__|http://localhost:8080_]] for the app. The
  138. created project is a standard SBT-project that uses the normal maven
  139. style layout, so you'll find the application source from_
  140. src/main/scala__.__
  141. To create Eclipse project files, type _eclipse_ in the sbt prompt. After
  142. this, the project can be imported as an Eclipse project.
  143. [[scaladin]]
  144. Scaladin
  145. ~~~~~~~~
  146. Scaladin is a library that extends Vaadin and adds Scala-like features
  147. to Vaadin classes. It's just a single add-on (one JAR) and is highly
  148. recommended for any Scala Vaadin development. See the
  149. http://github.com/henrikerola/scaladin/wiki[GitHub wiki] and the
  150. https://vaadin.com/directory/component/scaladin[Directory page] for more information.