Introduction
This blog will discuss how to use Vaadin with Spring MVC. But before talking about this, let's know what the Spring MVC is.
A Spring MVC is a Java framework for building web applications that follow the Model-View-Controller design pattern. Like Inversion of Control and Dependency Injection, it implements all the basic features of a core spring framework. It provides an effective solution to use MVC in the spring framework with the help of DispatcherServlet, where DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controllers, models, and views.
Using Vaadin with Spring MVC

To use Vaadin with Spring MVC, we need to do the following things. So, let's discuss this one by one.
Registering the Vaadin Servlet.
It is the first thing we must do to use Vaadin in our Spring web application. Therefore we need to register the Vaadin SpringServlet as a dispatcher servlet.

Let’s see an example where we register the SpringServlet as a dispatcher servlet.
Example:
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

As we know from the above, to use Vaadin scopes, we have to register the VaadinScopesConfig configuration class. Alternatively, we can add the @EnableVaadin annotation to our configuration class to import VaadinScopesConfig.
Also, the Vaadin Spring add-on provides the VaadinMVCWebAppInitializer class, which is an abstract subclass of the WebApplicationInitializer class. We can extend this class and provide our configuration classes by implementing the getConfigurationClasses() method.
Let's see an example to know more about this.
Example:
In this example we simple extends the VaadinMVCWebAppInitializer and implements the getConfigurationClasses() method.
public class SampleWebAppInitializer
extends VaadinMVCWebAppInitializer{
@Override
protected Collection<Class<?>>
getConfigurationClasses(){
return Collections.singletonList(
SampleConfiguration.class);
}
}
@Configuration
@ComponentScan
public class SampleConfiguration {
}Handling URLs.
We need at least one Vaadin component annotated with @Route to handle the URLs.
Let's see an example for a better understanding.

Example:
Here the views are defined as Java classes with Vaadin using the @Route annotation.
@Route
public class MainView extends VerticalLayout {
public MainView() {
add(new Text("Welcome to MainView."));
}
}






