Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to ensure load from db when rendering ?
Hi
I am new to Vaadin for 2 days. I am blocked by a simple question : How to ensure load from db when rendering ?
In my UI class , I create a HorizontalSplitPanel . When user clicks the left tree , right panel renders correspondent view.
The problem is , the right panel caches the data loaded from DB. It cannot reflects the latest state in DB.
The right panel contains two component . countLabel , and mTable .
countLabel shows how many users in DB . and mTable loads/displays them.
@SpringComponent
public class UsersView extends VerticalLayout {
@Inject
private UserDao userDao;
@PostConstruct
void init() {
Label countLabel = new Label(new AbstractProperty<String>() {
@Override
public String getValue() {
return String.valueOf(userDao.count());
}
@Override
public void setValue(String newValue) throws ReadOnlyException {
}
@Override
public Class<? extends String> getType() {
return String.class;
}
});
addComponent(countLabel);
final int pageSize = 10;
MTable<User> mTable = new MTable<>(User.class,
i -> {
return userDao.list(1, pageSize);
},
() -> (int) userDao.count() , pageSize
)
.withProperties("id", "name")
;
mTable.refreshRows();
mTable.setSizeFull();
addComponent(mTable);
}
}
I want Vaadin to display correct countLabel (user count) and userList (mTable) upon each rendering.
But it seems only countLabel works correctly (But I feel it is tedious , anyway to improve/shorten it ?) .
The mTable is cached forever.
If the user navigates to other component and back (by clicking the left tree node) , if DB state changes (user added or removed) , countLabel shows correctly , but mTable doesn't reflect the true state.
Even the user refresh the browser , the mTable still displays stale data.
How to fix such issue ?
(The UsersView is injected to my UI class )
Environment :
spring-boot 1.3.3
vaadin-spring-boot-starter : 1.0.0
viritin : 1.47
I solve the problem by introducing another customized interface : Reloadable
public interface Reloadable {
void reload();
}
And my views implement this interface :
@SpringComponent
public class TeachersView extends VerticalLayout implements Reloadable {
@Inject
private TeacherDao teacherDao;
private final static int PAGESIZE = 10;
private MTable<Teacher> mTable = new MTable<>(Teacher.class);
@PostConstruct
public void init() {
mTable.setSizeFull();
// other mTable settings ...
reload();
addComponent(mTable);
}
@Override
public void reload() {
mTable.setBeans(new SortableLazyList<>(
sortablePagingProvider ,
() -> (int) teacherDao.count() ,
PAGESIZE
));
}
private SortableLazyList.SortablePagingProvider<Teacher> sortablePagingProvider =
(firstRow, asc, sortProperty) -> {
logger.info("teacherDao.findAll ...");
return teacherDao.findAll(
new PageRequest(
firstRow / PAGESIZE, PAGESIZE,
asc ? Sort.Direction.ASC : Sort.Direction.DESC,
sortProperty == null ? "id" : sortProperty
)
).getContent();
};
}
And in the UI class :
@SpringUI(path = "/ui")
@Theme("valo")
public class VaadinUI extends UI {
@Inject
private TeachersView teachersView;
// other injected views skipped
@Override
protected void init(VaadinRequest vaadinRequest) {
Panel panel = new Panel("Admin Panel");
HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
splitPanel.setSplitPosition(15, Unit.PERCENTAGE);
panel.setContent(splitPanel);
Tree tree = new Tree("Menu");
splitPanel.setFirstComponent(tree);
Label home = new Label("Home");
Map<String, Component> map = new HashMap<>();
map.put("Teachers", teachersView);
// other views
map.forEach((k, v) -> tree.addItem(k));
tree.addItemClickListener(event -> {
Component view = map.get(event.getItemId());
if (view instanceof Reloadable) {
Reloadable reloadable = (Reloadable) view;
reloadable.reload();
}
splitPanel.setSecondComponent(view);
});
splitPanel.setSecondComponent(home);
setContent(panel);
} // init()
}
It works !
But I don't know if it the proper or standard way achieving this ?
Is it the vaadin way ? Or anything built-in I can follow ? Thanks.