I b - Getting started with Vaadin Spring without Spring Boot

Note: this tutorial applies to Vaadin Spring 1.0.0 and later

During this tutorial we will create a new Vaadin project, add Spring and Vaadin Spring as dependencies, create a simple Spring UI and deploy the application to a server.

Note that this tutorial is for using Vaadin Spring without Spring Boot. Using Spring Boot is the recommended approach for getting started quickly when creating a new project. For more information about setting up a project both with and without Spring Boot - see https://vaadin.github.io/spring-tutorial/\[the Vaadin Spring Boot tutorial].

Creating a Vaadin project

If you’ve created Vaadin projects before, there’s nothing new here. File→New→Project…​ then select Maven Project. This will take you to the project creation wizard. You can also create the project using Vaadin Plug-in for Eclipse if you prefer.

Project creation

then select the Vaadin archetype (com.vaadin:vaadin-archetype-application:<newest Vaadin version>). Set your group and artefact ids and your package to your liking. (I used com.vaadin, spring.tutorial and com.vaadin.spring.tutorial respectively)

Adding Vaadin Spring as a dependency

Open pom.xml and add

XML

  1. <dependency>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-spring</artifactId>
  4. <version>1.0.0</version>
  5. </dependency>

in the dependencies. Also replace the dependency “vaadin-client” with “vaadin-client-compiled” and remove the scope line for it as we are using a pre-compiled widgetset. If your project does not use the Vaadin add-on Maven repository yet, add it to the POM:

XML

  1. <repositories>
  2. <repository>
  3. <id>vaadin-addons</id>
  4. <url>https://maven.vaadin.com/vaadin-addons</url>
  5. </repository>
  6. </repositories>

Then save and update project.

Creating the UI

The project wizard created a UI for us that we’ll use as a starting point for building our application. There are some unnecessary things we’ll take out and some things we’ll need to add. Start of by deleting the AppWidgetSet.gwt.xml, as we won’t need a custom widgetset in these tutorials. We’ll also have to make a few changes to the UI to make it work with Vaadin Spring.

Here’s the UI’s original source:

Java

  1. @Theme("mytheme")
  2. @SuppressWarnings("serial")
  3. public class MyVaadinUI extends UI {
  4. @WebServlet(value = "/*", asyncSupported = true)
  5. @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "com.vaadin.spring.tutorial.AppWidgetSet")
  6. public static class Servlet extends VaadinServlet {
  7. }
  8. @Override
  9. protected void init(VaadinRequest request) {
  10. final VerticalLayout layout = new VerticalLayout();
  11. layout.setMargin(true);
  12. setContent(layout);
  13. Button button = new Button("Click Me");
  14. button.addClickListener(new Button.ClickListener() {
  15. public void buttonClick(ClickEvent event) {
  16. layout.addComponent(new Label("Thank you for clicking"));
  17. }
  18. });
  19. layout.addComponent(button);
  20. }
  21. }

To allow Vaadin Spring to use the UI you’ll need to add the following annotation to the UI:

Java

  1. @SpringUI

The servlet configuration needs to be updated to initialize a Spring application context, and the servlet should inherit from SpringVaadinServlet. In this tutorial, a ContextLoaderListener is used to initialize Spring.

Finally, as we do not need a custom theme in the application, the theme annotation is updated to use “valo” and the custom theme in the project can be deleted.

The resulting UI should be something like this:

Java

  1. package com.vaadin.spring.tutorial;
  2. import javax.servlet.annotation.WebListener;
  3. import javax.servlet.annotation.WebServlet;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.context.ContextLoaderListener;
  6. import com.vaadin.annotations.Theme;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.spring.annotation.EnableVaadin;
  9. import com.vaadin.spring.annotation.SpringUI;
  10. import com.vaadin.spring.server.SpringVaadinServlet;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.Button.ClickEvent;
  13. import com.vaadin.ui.Label;
  14. import com.vaadin.ui.UI;
  15. import com.vaadin.ui.VerticalLayout;
  16. @Theme("valo")
  17. @SpringUI
  18. @SuppressWarnings("serial")
  19. public class MyVaadinUI extends UI {
  20. @WebServlet(value = "/*", asyncSupported = true)
  21. public static class Servlet extends SpringVaadinServlet {
  22. }
  23. @WebListener
  24. public static class MyContextLoaderListener extends ContextLoaderListener {
  25. }
  26. @Configuration
  27. @EnableVaadin
  28. public static class MyConfiguration {
  29. }
  30. @Override
  31. protected void init(VaadinRequest request) {
  32. final VerticalLayout layout = new VerticalLayout();
  33. layout.setMargin(true);
  34. setContent(layout);
  35. Button button = new Button("Click Me");
  36. button.addClickListener(new Button.ClickListener() {
  37. public void buttonClick(ClickEvent event) {
  38. layout.addComponent(new Label("Thank you for clicking"));
  39. }
  40. });
  41. layout.addComponent(button);
  42. }
  43. }

With the @SpringUI annotation the Vaadin Spring plugin will know to inject the UI rather than directly instantiating it. With injected beans we can use all of the usual Spring features such as autowiring. More on that in later tutorials.

In addition to these changes, when not using Spring Boot, create the following Spring context file at src/main/webapp/WEB-INF/applicationContext.xml :

XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  8. <bean class="com.vaadin.spring.tutorial.MyVaadinUI.MyConfiguration" />
  9. <context:component-scan base-package="com.vaadin.spring.tutorial" />
  10. </beans>

A full description of alternative approaches to configuring Spring is outside the context of this tutorial and you should consult Spring documentation for them, but a brief introduction to them is given in this tutorial.

Deployment

Once the UI is done we’ll deploy it to our server by Run→Run as→Run on Server. Select your server runtime (Tomcat in our case) and click Finish.

Eclipse should automatically open an embedded browser directed at your development server.

Congratulations! You’ve deployed your first Spring application.