Docs

Documentation versions (currently viewingVaadin 24)

Using Vaadin with Spring MVC

How to use Vaadin with Spring MVC.

This section covers how to use Vaadin with Spring MVC. Spring MVC is the original Spring web framework built on the Servlet API.

Note
For more on using Vaadin with Spring Boot, see Using 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.

This examples shows how to register the SpringServlet as a dispatcher servlet:

public abstract class ExampleWebAppInitializer
        implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        AnnotationConfigWebApplicationContext context =
            new AnnotationConfigWebApplicationContext();
        registerConfiguration(context);
        servletContext.addListener(
                new ContextLoaderListener(context));

        ServletRegistration.Dynamic registration =
            servletContext.addServlet("dispatcher",
                new SpringServlet(context, true));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

    private void registerConfiguration(
        AnnotationConfigWebApplicationContext context) {
        // register your configuration classes here
    }
}

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, which is an abstract subclass of the WebApplicationInitializer class. You can extend this class and provide your configuration classes by implementing the getConfigurationClasses() method.

This example is extending VaadinMVCWebAppInitializer and implementing the getConfigurationClasses() method:

public class SampleWebAppInitializer
        extends VaadinMVCWebAppInitializer {

    @Override
    protected Collection<Class<?>>
            getConfigurationClasses() {
        return Collections.singletonList(
                SampleConfiguration.class);
    }
}
@Configuration
@ComponentScan
public class SampleConfiguration {
}

This registers VaadinScopesConfig.

Handling URLs

To handle URLs, you need at least one Vaadin component annotated with @Route. See Add Vaadin view to Spring Boot application for an @Route annotation example.

Declaring Dependencies

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring</artifactId>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>

B13B6261-A0AB-47F3-8555-F999E2C05917