Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

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.

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.

public abstract class ExampleWebAppInitializer
        implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext)
            throws ServletException {
        AnnotationConfigWebApplicationContext context =
            new AnnotationConfigWebApplicationContext();
                new ContextLoaderListener(context));

        ServletRegistration.Dynamic registration =
                new SpringServlet(context, true));

    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 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.

public class SampleWebAppInitializer
        extends VaadinMVCWebAppInitializer {

    protected Collection<Class<?>>
            getConfigurationClasses() {
        return Collections.singletonList(
public class SampleConfiguration {
  • 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:



    <!-- Spring -->