Use Flow with Spring MVC

In the previous section it has been shown the easiest way to use vaadin-spring add-on with Spring Boot. If you are not familiar with Spring Boot magic or it’s just too magic for you and you want to write pure Spring Web application then you can follow this section.

You may create your own Spring Web application from scratch. To be able to use Flow in your Spring Web application you should register Vaadin servlet 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 = new AnnotationConfigWebApplicationContext();
  7. registerConfiguration(context);
  8. servletContext.addListener(new ContextLoaderListener(context));
  9. ServletRegistration.Dynamic registration = servletContext
  10. .addServlet("dispatcher", new SpringServlet(context));
  11. registration.setLoadOnStartup(1);
  12. registration.addMapping("/*");
  13. }
  14. private void registerConfiguration(
  15. AnnotationConfigWebApplicationContext context) {
  16. // register your configuration classes here
  17. }
  18. }

You will need some Flow component annotated with @Route and you should register the VaadinScopesConfig configuration class to be able to use Spring Vaadin scopes (see the Flow application code examples in Use Flow with Spring).

Note
Alternatively, you may add the @EnableVaadin annotation to your configuration class to import VaadinScopesConfig.

The Spring add-on provides an abstract subclass of WebApplicationInitializer class which you may just extend and provide your configuration classes via implementing the getConfigurationClasses() method:

Java

  1. public class SampleWebAppInitializer extends VaadinMVCWebAppInitializer {
  2. @Override
  3. protected Collection<Class<?>> getConfigurationClasses() {
  4. return Collections.singletonList(SampleConfiguration.class);
  5. }
  6. }
  7. @Configuration
  8. @ComponentScan
  9. public class SampleConfiguration {
  10. }
Note
VaadinScopesConfig and VaadinServletConfiguration configurations will be registered automatically for you in this case.

To be able to use Spring Web application you should declare dependencies in your pom.xml file to vaadin-bom and spring-web:

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>