Connect a View to the Backend
On the previous part of this tutorial, you created a view using Vaadin components and layouts. On this part, you’ll connect the view to the backend to display and update data.
You can find the back-end code in the src/main/java directory.
This part of this tutorial covers three main aspects of this:
Spring Boot introduction;
Spring Service Interface to the backend; and
Accessing a service from a view.
Introduction to Spring Boot
Vaadin uses Spring Boot on the server. Spring Boot is an opinionated, convention-over-configuration approach to creating Spring applications. It automates much of the required configuration and manages an embedded Tomcat server. So, you don’t need to deploy the application to a separate server.
This tutorial uses the following features that are configured by Spring Boot:
Spring Data for accessing the database through JPA and Hibernate;
An embedded H2 Database for development (easy to replace with e.g. PostgreSQL for production);
Spring Boot DevTools for automatic code reload;
Embedded Tomcat server for deployment; and
Spring Security for authenticating users.
Backend Overview
The starter you downloaded contains the entities and repositories you need. It also contains sample data loaded using src/main/resources/data.sql file.
Domain Model: Entities
The Vaadin CRM application has three JPA entities that make up its domain model: Contact, Company, and Status. A contact belongs to a company and has a status.
You can find the entities in the com.example.application.data.entity package.

Database Access: Repositories
The application uses Spring Data JPA repositories for database access. Spring Data provides implementations of basic create, read, update, and delete (i.e., CRUD) database operations when you extend from the JpaRepository interface.
You can find the repositories in the com.example.application.data.repository package.
Sample Data
The src/main/resources/data.sql file contains sample data that Spring Boot populates to the database on startup.
Create a Service for Database Access
Instead of accessing the database directly from the view, you would create a Spring Service. The service class handles the application’s business logic and, in larger applications, it often transforms database entities into Data-Transfer Objects (DTO) for views. This tutorial shows how to create a single service that provides all of the methods you need.
First, create a new class, CrmService.java, in the data.service package with the following content:
CrmService.java,Copyto clipboard
The
@Serviceannotation makes this a Spring-managed service that you can inject into your view.Use Spring constructor injection to autowire the database repositories.
Check if there’s an active filter: return either all contacts, or use the repository to filter based on the string.
Service classes often include validation and other business rules before persisting data. You check here that you aren’t trying to save a
nullobject.
Implement Filtering in the Repository
Add the search() method to the contacts repository so as to provide the service class with the required method for filtering contacts.
ContactRepository.java,Copyto clipboard
This example uses the @Query annotation to define a custom query (see annotation 1). In this case, it checks if the string matches the first or the last name, and ignores the case. The query uses Java Persistence Query Language (JPQL) which is an SQL-like language for querying JPA-managed databases.
You don’t need to implement the method. Spring Data provides the implementation based on the query.
Using Back-End Service
You can now inject the CrmService into the list view to access the backend.
ListView.java,Copyto clipboard
Autowire
CrmServicethrough the constructor. Save it in a field, so you can access it from other methods.Call
updateList()once you have constructed the view.Use the service to fetch companies and statuses.
Call
updateList()any time the filter changes.updateList()sets the grid items by calling the service with the value from the filter text field.
Now build the project, refresh the browser, and verify that you can now see contacts in the grid. It should look like the screenshot here. Try filtering the contents by typing in the filter text field.

Last updated
Was this helpful?