Table of contents
1.
Introduction
2.
Using Vaadin with Spring MVC
2.1.
Registering the Vaadin Servlet.
2.1.1.
Example:
2.2.
Registering Vaadin Scopes
2.2.1.
Example:
2.3.
Handling URLs.
2.3.1.
Example:
3.
Declaring Dependencies.
4.
Frequently Asked Questions
4.1.
Why do we use Spring MVC?
4.2.
What is the difference between the Spring Boot and Spring MVC?
4.3.
What is @controller in spring?
4.4.
Is Vaadin free for commercial use?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Using Vaadin with Spring MVC.

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

Using Vaadin with Spring MVC Image

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.

Registering the Vaadin Servlet Image

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.
    }
}
You can also try this code with Online Java Compiler
Run Code

Registering Vaadin Scopes

Registering Vaadin Scopes Image

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 {
}
You can also try this code with Online Java Compiler
Run Code

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.

Handling URL Image

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."));
   }
}
You can also try this code with Online Java Compiler
Run Code

Declaring Dependencies.

Declaring Dependencies Image

Let's see how we declare dependencies on vaadin-bom and spring-web in our pom.xml file to use our Spring web applications.

<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>
</dependencies>

 

Frequently Asked Questions

Why do we use Spring MVC?

The Spring MVC can be used to develop flexible and loosely coupled web applications.

What is the difference between the Spring Boot and Spring MVC?

Spring Boot is considered a module of the Spring framework for packaging the Spring-based application with sensible defaults. In contrast, Spring MVC is regarded as the model view controller-based web framework under the Spring framework.

What is @controller in spring?

The @Controller annotation indicates a particular class that serves the role of a controller.

Is Vaadin free for commercial use?

We may freely download and use Vaadin Framework for commercial, personal and non-commercial purposes.

Conclusion

In this article, we have discussed how we have used Vaadin with Spring MVC. We have discussed it with an example to understand it better.

After reading about the use of Vaadin with Spring MVC, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See JavaJSP-XMLGWT vs VaadinGWT BasicsIntroduction to Spring BootSpring Boot Starter Web, and Spring Boot to learn.

Recommended Readings:

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass