Escolar Documentos
Profissional Documentos
Cultura Documentos
Previously Vaadin developers used a lot of proprietary interfaces called Containers, Items and Properties
to do the databinding. The old Container interface has been removed from the new Vaadin 8 API
altogether. See the Data API & other comparisons here. What is of utmost importance & interest is the
API to lazy load data from backend. See the official reference here. Lets understand the new API in the
following Vaadin DataProvider Example.
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 1/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
You will notice that this DataProvider has three key overridden methods:
1. fetchFromBackend()
2. sizeInBackend()
3. getId()
fetchFromBackend(): Whenever a Vaadin 8 list data component (Grid, ComboBox etc.) needs data, it
requests the needed data from the attached DataProvider. The component passes a Query object to
the DataProvider. The Query object contains important information like filter string (for fetching a filtered
resultset), limit & offset (to fetch the desired subset or page of the resultset).
“The results of the first and second callback must be symmetric so that fetching all available items using
the first callback returns the number of items indicated by the second callback. Thus if you impose any
restrictions on e.g. a database query in the first callback, you must also add the same restrictions for the
second callback.”
getId(): This method returns the unique ID of the data objects. generally, this is usually the id property of
the the entity class. This getId() method helps the list data components (Grid, ComboBox etc.) to swap
out the stale objects from & swap in the fresh objects into their internal objects-cache.
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 2/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 3/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
76. }
This EmployeeService makes use of H2 as the backend database. See the earlier post for Vaadin
Database Connection example. Our example-db.mv.db is located at “D:\data-provider-example”. See
attached screen-shot:
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 4/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
Here is the source code for SortStringGenerator.java. This class helps in converting the sorting
information, passed on by the list data components, into a format that can be used in our SQL queries.
1. @Theme("mytheme")
2. public class MyUI extends UI {
3.
4. /**
5. *
6. */
7. private static final long serialVersionUID = 1L;
8.
9. @Override
10. protected void init(VaadinRequest vaadinRequest) {
11. // create and initialize connection pool
12. final HorizontalLayout root = new HorizontalLayout();
13. root.setSizeFull();
14. root.setSpacing(true);
15. root.setMargin(true);
16. setContent(root);
17.
18. EmployeeService employeeService = new EmployeeService();
19. EmployeeDataProvider employeeDataProvider = new
EmployeeDataProvider(employeeService);
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 5/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
20.
21. //combo-box
22. ComboBox<Employee> employeeCombo = new ComboBox<>("Employee Combo");
23. employeeCombo.setPlaceholder("Select employee...");
24. employeeCombo.setWidth("400px");
25. employeeCombo.setItemCaptionGenerator(emp -> {
26. return "#" + emp.getId() + " - " + emp.getName();
27. });
28. employeeCombo.setDataProvider(employeeDataProvider);
29.
30. //grid
31. Grid<Employee> employeeGrid = new Grid<>("Employee Grid");
32. employeeGrid.setWidth("400px");
33. employeeGrid.addColumn(emp->{return
"#"+emp.getId();}).setCaption("Id").setWidth(80).setSortProperty(EmployeeService.SORT_ON
_ID);
34. employeeGrid.addColumn(emp->{return
emp.getName();}).setCaption("Name").setWidth(320).setSortProperty(EmployeeService.SORT_O
N_NAME);
35. employeeGrid.setDataProvider(employeeDataProvider);
36.
37. //add components
38. root.addComponents(employeeCombo, employeeGrid);
39. }
40.
41. @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
42. @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
43. public static class MyUIServlet extends VaadinServlet {
44.
45. private static final long serialVersionUID = 1L;
46. }
47. }
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 6/7
22/08/2017 Vaadin DataProvider Example - Vaadin Help
http://vaadinhelp.co.in/vaadin-dataprovider-example/ 7/7