Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vaadin 7 Cookbook

You're reading from   Vaadin 7 Cookbook Take the shortcut to developing rich internet applications in pure Java. Vaadin makes it easy and this cookbook makes it easier still with its practical recipes and straightforward approach.

Arrow left icon
Product type Paperback
Published in Apr 2013
Publisher
ISBN-13 9781849518802
Length 404 pages
Edition Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (19) Chapters Close

Vaadin 7 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Creating a Project in Vaadin 2. Layouts FREE CHAPTER 3. UI Components 4. Custom Widgets 5. Events 6. Messages 7. Working with Forms 8. Spring and Grails Integration 9. Data Management 10. Architecture and Performance 11. Facilitating Development 12. Fun Index

Using Vaadin with Scala


Scala is a multi-paradigm language integrating object-oriented and functional programming. Read more about Scala on http://www.scala-lang.org.

Getting ready

Scala installation is quite easy. Just go to http://www.scala-lang.org/downloads, download Scala, and set up system variables as follow:

  • Linux/Mac

    SCALA_HOME=/Users/John/Installations/scala
    export SCALA_HOME
    export PATH=$PATH:$SCALA_HOME/bin
    
  • Windows

    %SCALA_HOME% = c:\Scala
    %PATH% = %PATH%;%SCALA_HOME%\bin
    

We have to maintain our project anyhow and we are going to utilize Gradle.

Tip

There is also another way to manage Scala projects. It is called Typesafe and more info is on http://typesafe.com.

How to do it...

Carry out the following steps in order to create a new Scala project:

  1. Make the project structure so we have folders where we can put our project files.

    mkdir -p vaadin-in-scala/src/main/{scala/app,webapp/WEB-INF}
    
  2. Make build.gradle in the project root. It is going to be just a few lines that are necessary for running the project.

    apply plugin: 'war'
    apply plugin: 'jetty'
    apply plugin: 'scala'
        
    repositories {
      mavenCentral()
    }
        
    dependencies {
      scalaTools 'org.scala-lang:scala-compiler:2.10.0'
      scalaTools 'org.scala-lang:scala-library:2.10.0'
      compile 'org.scala-lang:scala-library:2.10.0'
      compile group:'com.vaadin', name:'vaadin-server', version:'7.0.4'
      compile group:'com.vaadin', name:'vaadin-client', version:'7.0.4'
      compile group:'com.vaadin', name:'vaadin-client-compiled', version:'7.0.4'
      compile group:'com.vaadin', name:'vaadin-themes', version:'7.0.4'
      compile group:'com.vaadin', name:'vaadin-client-compiler', version:'7.0.4'
    }
    
  3. Create a new Scala class MyVaadinUI that we place into the MyVaadinUI.scala file in the folder src/main/scala/app.

    package app
    
    import com.vaadin.ui._
    import com.vaadin.server.VaadinRequest
    
    class MyVaadinUI extends UI {
      def init(request: VaadinRequest) = {
        val layout = new VerticalLayout()
        layout.setMargin(true)
        setContent(layout)
        layout.addComponent(new Label("Hello Vaadin user."))
        }
    }
  4. Add the web.xml file to the src/main/webapp/WEB-INF folder.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Vaadin Web Application</display-name>
      <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
      </context-param>
      <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
          <description>Vaadin UI to display</description>
          <param-name>UI</param-name>
          <param-value>app.MyVaadinUI</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
    </web-app>
  5. We are done and we can run the application.

    gradle jettyRun
    

    The following output should be displayed:

    :compileJava UP-TO-DATE
    :compileScala UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    > Building > :jettyRun > Running at http://localhost:8080/vaadin-in-scala
    

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works...

We have made a Gradle project in which we have applied the scala plugin. The Scala plugin inside the Gradle build script ensures that all the .scala files inside the src/main/scala source folder will be compiled. Scala files are compiled to .class files that are then deployed on the Jetty webserver.

The deployment descriptor (web.xml file) defines one servlet. When users access the URL with the /* pattern, which is mapped to the Vaadin servlet, MyVaadinUI is shown in the browser.

The important thing we need to check is the init-param element of the Vaadin servlet. It needs to point exactly to the UI class, which represents our application. The path to the UI class must be the full name of the class together with the package, for example, app.MyVaadinUI.

See also

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime