Build Your First Data List View
- Create the View Class
- Create a Data Transfer Object
- Add a Grid Component
- Create a Repository Interface
- Add Data to the Grid
- Complete Code for This Step
- Next Steps
Your first task is to build a product catalog view that displays a list of products from the database. You’ll create a new Vaadin view class and set up a grid component to show the product data.
You’ll take only the minimal steps needed to get the view up and running. Later in the tutorial, you’ll enhance the view with additional features and functionality, and change the structure of the code when those features call for it.
|
Tip
|
Restart or Hotswap?
If you run the application with hotswap enabled, changes to the UI should show up automatically in the browser. However, if you change the data model or repository interfaces, you’ll need to restart the application to see the changes.
|
Create the View Class
You’ll be packaging your code by feature in this tutorial. Start by making a new package com.example.product. Inside that package, create a new Java class named ProductCatalogView that extends VerticalLayout. Annotate the class with @Route("") to make it the main view of the application:
Source code
ProductCatalogView.java
package com.example.product;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("")
@PageTitle("Product Catalog") 1
class ProductCatalogView extends VerticalLayout { 2
ProductCatalogView() {
// The view implementation will go here
}
}-
The view will have the page title "Product Catalog", which appears in the browser tab.
-
The class has package visibility as you won’t need to access it from other packages.
Now run the application, and navigate to http://localhost:8080. You should see an empty page with the title "Product Catalog".
Create a Data Transfer Object
You’ll need a data transfer object (DTO) to represent the product data in the view. Create a new record named ProductCatalogItem in the same package as the view.
Since you’ll be using Spring Data to fetch the data, the record fields must match the column names in the PRODUCTS table of the database. You also need to annotate the record with @Table and the ID field with @Id so that Spring Data can map the database rows to instances of the record:
Source code
ProductCatalogItem.java
package com.example.product;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
import java.math.BigDecimal;
@Table("PRODUCTS")
record ProductCatalogItem( 1
@Id long productId,
String name,
String description,
String category,
String brand,
BigDecimal price
) {
}-
The record has package visibility as you won’t need to access it from other packages.
Add a Grid Component
To display the product data, you’ll use a Grid component. A grid takes several calls to set up: one for each column, and more for the headers and the alignment of the columns. Instead of making these calls in the view, create a class that extends Grid, and set up the grid in its constructor. The name of the class then says what the grid shows, and the view stays short.
Only the view uses the grid, so the class could be nested in the view. However, the setup is long, and it grows when you make the columns sortable in the next step. Therefore, create a new class named ProductCatalogGrid in a file of its own, in the same package as the view:
Source code
ProductCatalogGrid.java
package com.example.product;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid;
class ProductCatalogGrid extends Grid<ProductCatalogItem> { 1
ProductCatalogGrid() {
addColumn(ProductCatalogItem::name) 2
.setHeader("Name");
addColumn(ProductCatalogItem::price)
.setHeader("Price")
.setTextAlign(ColumnTextAlign.END);
addColumn(ProductCatalogItem::description)
.setHeader("Description");
addColumn(ProductCatalogItem::category)
.setHeader("Category");
addColumn(ProductCatalogItem::brand)
.setHeader("Brand");
}
}-
The grid shows
ProductCatalogItemrecords. -
Use method references to define how the columns get their values from the
ProductCatalogItemrecord. Since the class is a grid, it calls the methods ofGriddirectly.
The class is still a grid, so the code that uses it can call every method of Grid on it. See Extending an Existing Component for more about this pattern.
Next, add the grid to the view. The constructor of the view creates the components first, and then lays them out:
Source code
ProductCatalogView.java
package com.example.product;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("")
@PageTitle("Product Catalog")
class ProductCatalogView extends VerticalLayout {
ProductCatalogView() {
// Create components
var grid = new ProductCatalogGrid();
// Layout view
setSizeFull(); 1
grid.setSizeFull(); 2
add(grid); 3
}
}-
Make the view take the full size of the browser window.
-
Make the grid take the full size of the view.
-
Add the grid to the view layout.
VerticalLayout arranges its components in a column. Under the hood, it’s a CSS flexbox container, and sizing components inside it follows flexbox rules, which can be surprising at first. The Arrange with Layouts guide explains them.
The view should now look like this:
Create a Repository Interface
To fetch the product data from the database, you’ll use a Spring Data repository. Create a new interface named ProductCatalogItemRepository in the same package as the view and DTO:
Source code
ProductCatalogItemRepository.java
package com.example.product;
import org.springframework.data.repository.PagingAndSortingRepository;
interface ProductCatalogItemRepository 1
extends PagingAndSortingRepository<ProductCatalogItem, Long> { 2
}-
The interface has package visibility as you won’t need to access it from other packages.
-
The
PagingAndSortingRepositoryinterface provides methods for pagination and sorting, which you’ll use later in the tutorial.
Spring Data provides the implementation of the repository interface at runtime, so you don’t need to write any code for it.
Add Data to the Grid
Finally, you’ll fetch the product data from the database and set it as the items of the grid. To do this, inject the ProductCatalogItemRepository into the constructor of the ProductCatalogView class and use it to retrieve all products. The view does this, not the grid. The grid only shows the items that it gets, and the view decides where they come from.
In production applications, you should never perform unbounded queries that can return a large number of items. Since Grid has built-in support for pagination, you’ll use that to fetch only the items needed for the current page:
Source code
ProductCatalogView.java
package com.example.product;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("")
@PageTitle("Product Catalog")
class ProductCatalogView extends VerticalLayout {
ProductCatalogView(ProductCatalogItemRepository repository) { 1
// Create components
var grid = new ProductCatalogGrid();
grid.setItemsPageable(pageable -> repository
.findAll(pageable)
.getContent()
); 2
// Layout view
setSizeFull();
grid.setSizeFull();
add(grid);
}
}-
Use constructor injection to get the repository instance.
-
Use the
setItemsPageablemethod of the grid to fetch only the items needed for the current page.
Now, when you run the application and navigate to http://localhost:8080, you should see the product catalog populated with data from the database:
If you scroll the grid, it looks like all items are loaded at once. Under the hood, only the visible items are fetched from the database. For more information about paginated grids, see Add a Paginated Grid guide.
|
Note
|
Why No Service?
The view calls the repository directly, without going through an application service. This is a deliberate choice: a view that only reads data, and passes its parameters straight to a repository method, doesn’t need a service. You’ll add one later in the tutorial, when the view starts changing data. See Calling Repositories from Views for details.
|
Complete Code for This Step
If your application doesn’t behave as described, compare your files with these listings. They show every class in the application as it should look at the end of this step.
The step/list-data branch of the vaadin/tutorial repository on GitHub has the whole project as it is at the end of this step.
ProductCatalogView.java
Source code
Java
package com.example.product;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@Route("")
@PageTitle("Product Catalog")
class ProductCatalogView extends VerticalLayout {
ProductCatalogView(ProductCatalogItemRepository repository) {
// Create components
var grid = new ProductCatalogGrid();
grid.setItemsPageable(pageable -> repository
.findAll(pageable)
.getContent()
);
// Layout view
setSizeFull();
grid.setSizeFull();
add(grid);
}
}ProductCatalogGrid.java
Source code
Java
package com.example.product;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid;
class ProductCatalogGrid extends Grid<ProductCatalogItem> {
ProductCatalogGrid() {
addColumn(ProductCatalogItem::name)
.setHeader("Name");
addColumn(ProductCatalogItem::price)
.setHeader("Price")
.setTextAlign(ColumnTextAlign.END);
addColumn(ProductCatalogItem::description)
.setHeader("Description");
addColumn(ProductCatalogItem::category)
.setHeader("Category");
addColumn(ProductCatalogItem::brand)
.setHeader("Brand");
}
}ProductCatalogItem.java
Source code
Java
package com.example.product;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
import java.math.BigDecimal;
@Table("PRODUCTS")
record ProductCatalogItem(
@Id long productId,
String name,
String description,
String category,
String brand,
BigDecimal price
) {
}ProductCatalogItemRepository.java
Source code
Java
package com.example.product;
import org.springframework.data.repository.PagingAndSortingRepository;
interface ProductCatalogItemRepository
extends PagingAndSortingRepository<ProductCatalogItem, Long> {
}Next Steps
Congratulations! You’ve built your first Vaadin view that displays data from a database. In the next part of the tutorial, you’ll enhance the product catalog view with sorting and filtering capabilities. Proceed to the Sort and Filter the Product Catalog step to continue the tutorial.