Using Vaadin with Spring MVC

In this section we cover how to use Vaadin with Spring MVC. Spring MVC is the original Spring web framework built on the Servlet API.

Note
See Using Vaadin with Spring Boot to use Vaadin with Spring Boot.

Registering the Vaadin Servlet

To use Vaadin in your Spring web application you need to register the Vaadin SpringServlet as a dispatcher servlet.

Example: Registering the SpringServlet as a dispatcher servlet.

Java

  1. public abstract class ExampleWebAppInitializer
  2. implements WebApplicationInitializer {
  3. @Override
  4. public void onStartup(ServletContext servletContext)
  5. throws ServletException {
  6. AnnotationConfigWebApplicationContext context =
  7. new AnnotationConfigWebApplicationContext();
  8. registerConfiguration(context);
  9. servletContext.addListener(
  10. new ContextLoaderListener(context));
  11. ServletRegistration.Dynamic registration =
  12. servletContext.addServlet("dispatcher",
  13. new SpringServlet(context, true));
  14. registration.setLoadOnStartup(1);
  15. registration.addMapping("/*");
  16. }
  17. private void registerConfiguration(
  18. AnnotationConfigWebApplicationContext context) {
  19. // register your configuration classes here
  20. }
  21. }

Registering Vaadin Scopes

To use Vaadin Spring scopes you need to register the VaadinScopesConfig configuration class. As an alternative, you can add the @EnableVaadin annotation to your configuration class to import VaadinScopesConfig.

The Vaadin Spring add-on provides the VaadinMVCWebAppInitializer class that is an abstract subclass of the WebApplicationInitializer class. You can extend this class and provide your configuration classes by implementing the getConfigurationClasses() method.

Example: Extending VaadinMVCWebAppInitializer and implementing the getConfigurationClasses() method.

Java

  1. public class SampleWebAppInitializer
  2. extends VaadinMVCWebAppInitializer {
  3. @Override
  4. protected Collection<Class<?>>
  5. getConfigurationClasses() {
  6. return Collections.singletonList(
  7. SampleConfiguration.class);
  8. }
  9. }

Java

  1. @Configuration
  2. @ComponentScan
  3. public class SampleConfiguration {
  4. }
  • This registers VaadinScopesConfig and VaadinServletConfiguration automatically.

Handling URLs

To handle URLs, you need at least one Vaadin component, annotated with @Route. See Handling URLs for an @Route annotation example.

Declaring Dependencies

To use your Spring web application, you need to declare dependencies in your pom.xml file to vaadin-bom and spring-web as follows:

XML

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.vaadin</groupId>
  5. <artifactId>vaadin-bom</artifactId>
  6. <version>${vaadin.version}</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.vaadin</groupId>
  15. <artifactId>vaadin-spring</artifactId>
  16. </dependency>
  17. <!-- Spring -->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-web</artifactId>
  21. <version>5.0.2.RELEASE</version>
  22. </dependency>