

Designing Quarkus Front-Ends with Vaadin made easy
source link: https://www.mastertheboss.com/soa-cloud/quarkus/designing-quarkus-front-ends-with-vaadin-made-easy/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Vaadin Flow provides a comprehensive set of UI components and tools for creating rich and interactive user interfaces, while Quarkus offers a lightweight and efficient Java framework for developing cloud-native applications. In this article, we will explore how to combine the strengths of Vaadin and Quarkus to build web applications with ease.
What is Vaadin?
If you are new to Vaadin, you can get a quick introduction in this article which shows how to set up a basic Vaadin application: Vaadin Tutorial: Building Modern Web Applications with Ease
Vaadin Flow follows a component-based architecture, where UI components are created and composed together to build the application’s user interface. These components are written in Java and rendered on the client-side using JavaScript and HTML. This approach allows developers to write the application logic in Java while leveraging the power of the web platform for rendering and interactivity.
In order to use Vaadin Flows with Quarkus you can add the Vaadin Flow extension to your Quarkus project:

We will now build a sample Quarkus application which combines Vaadin Flow extension with a simple Service that loads data from the Database and displays it in a Grid Component.
Step 1: Build the Server Side
Firstly, we will create a simple Model that we will display in a Vaadin Grid. This model uses the following Person Entity Class:
@Entity @NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p WHERE country = :country") public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; private String surname; private int age; private String country; // Getters/Setters omitted for brevity }
Then, in order to fetch the list of Person, filtered by Country, we will add the following Service Class:
@Dependent public class PersonService { @Inject EntityManager entityManager; public List<Person> getPersons(String country) { return (List<Person>) entityManager.createNamedQuery("Person.findAll") .setParameter("country", country) .getResultList(); } }
Step 2: Building the Vaadin View
In Vaadin, a View represents a distinct portion of a web application’s user interface. It defines the visual components and layout that are displayed to the user when accessing a specific URL or navigating to a particular section of the application.
A Vaadin View is typically a Java class that extends a Vaadin layout component, such as VerticalLayout or HorizontalLayout, and contains the @Route
annotation. This annotation specifies the URL path that you will use to access the view.
Finally, within the View, we will add a set of Components that you will be interacting with during the navigation.
Here is our Main View, which you will be able to reach on the Web application Home page via the @Route(“”) annotation:
@Route("") public class MainView extends VerticalLayout { @Inject PersonService greetService; public MainView() { H1 pageTitle = new H1("Welcome to Quarkus Vaadin!"); add(pageTitle); ComboBox<String> comboBox = new ComboBox<>("Country"); comboBox.setAllowCustomValue(true); List<String> countries = new ArrayList(); countries.add("US"); countries.add("UK"); comboBox.setItems(countries); Grid<Person> grid = new Grid<>(Person.class); grid.removeColumnByKey("id"); grid.setColumns("name", "surname", "age", "country"); grid.setSizeFull(); grid.setWidth("600px"); Button button = new Button("Search", e -> { List<Person> persons = greetService.getPersons(comboBox.getValue()); System.out.println(persons); grid.setItems(persons); grid.getDataProvider().refreshAll(); }); button.addThemeVariants(ButtonVariant.LUMO_PRIMARY); button.addClickShortcut(Key.ENTER); add(comboBox, button,grid); setWidth("100%"); setSizeFull(); } }
Here is a detail about the Vaadin components we use in this View:
- H1: This is a Vaadin component representing a level-one heading (tag). It displays the title “Welcome to Quarkus Vaadin!”. The add() method adds this component to the view.
- ComboBox: This component is a drop-down list with a list of “Country”.
- Grid: This component represents a table or grid for displaying tabular data. In this case, it contains the Person class as the item type.
- Button: This component represents a clickable button. The button triggers a click event listener (e -> { … }) that fetches persons based on the selected value in the ComboBox and updates the data in the grid accordingly.
Step 3: Running the application
For the sake of simplicity, our application uses the H2 Database as Datasource. Also, we will add an import.sql file to pre-load some data:
INSERT INTO Person (id, name, surname, age, country) VALUES (1, 'John', 'Doe', 25,'US'); INSERT INTO Person (id, name, surname, age, country) VALUES (2, 'Jane', 'Smith', 30,'UK'); INSERT INTO Person (id, name, surname, age, country) VALUES (3, 'Michael', 'Johnson', 35,'US'); INSERT INTO Person (id, name, surname, age, country) VALUES (4, 'Emily', 'Davis', 28,'UK'); INSERT INTO Person (id, name, surname, age, country) VALUES (5, 'David', 'Brown', 32,'US');
Finally, you can run the application as follows:
mvn install quarkus:dev
Upon selecting the Country Combobox, you will see that the Grid Component reloads Data via the Listener Event bound to the Button Object:

Conclusion
In conclusion, Vaadin Flow offers a powerful and efficient way to build front-ends for Quarkus applications. By combining the ease and productivity of Java development with the rich and interactive UI capabilities of Vaadin, developers can create modern and feature-rich web applications.
Source code: https://github.com/fmarchioni/mastertheboss/tree/master/quarkus/vaadin-demo
Recommend
-
106
Alternative navigator in Vaadin In Vaadin, to change the components displayed on the screen, there are a few options. The most straightforward way is to use the setContent() method o...
-
5
For true rock stars, the road never ends. We’re bringing the band back on the road to continue bringing our unique, hands-on experience with access to Quarkus experts designed to help you get started with Java in a Kubernetes...
-
9
Vaadin 10+: SASS Integration and CSS Refresh during Development Posted on Apr 15, 2019. Updated on Apr 17, 2019 Since Vaadin 10, SASS is no longer supported out of the box....
-
15
In this blog, I will show you how I got started with Vaadin Flow. With Vaadin Flow, you can create web apps without HTML or JavaScript, but entirely with Java. Let’s get started! 1. Introduction For some time, I have...
-
11
Vaadin 23.0.0 Released Vaadin 23.0.0 Released - Flow and Hilla Written by Nikos Vaggalis Monday, 21 March 2022 Vaadin, the versatile full stack Java-based f...
-
13
Blink a LED on a Raspberry Pi With Vaadin Build a respons...
-
12
Difference between two datesFields in Vaadin advertisements I'm having some problem with calculate days between two dateFie...
-
7
aire-wizard Aire-wizard is a full-featured wizard component for Vaadin flow Contributing Clone the repo: git clone [email protected]:aire-ux/aire-wizard Prepare package for development: gradl...
-
8
Vaadin CEO: Developers are the architects of the future Vaadin’s Steven Grandchamp explains why every company needs to listen to its software developers....
-
7
vaadin/hilla:前后端集成框架 解道Jdon ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK