Vaadin中当前UI的访问方法 [英] Access method from current UI in Vaadin
问题描述
我目前正在Vaadin上开发Java Web应用程序.
i'm currently working on a Java Web Application in Vaadin.
我想从另一个类访问位于我的LoginUI中的getter方法.
I want to access a getter method, which is located in my LoginUI, from another class.
UI.getCurrent()成功返回当前线程(LoginUI).
UI.getCurrent() successfully returns the current Thread (LoginUI).
我需要调用哪些方法来实现这一目标?
Which methods do i need to call to achieve this?
谢谢.
推荐答案
UI.getCurrent()不是线程安全的.相反,我建议您采用以下模式.
UI.getCurrent() is not thread safe. Instead I recommend you the following pattern.
public class MyView extends VerticalLayout implements View {
private UI ui;
@Override
public onAttach() {
ui = getUI();
...
}
...
public updateMe(..) {
...
try {
ui.access( ... do updates ... );
} catch (UIDetachedException e) {
// Do nothing, this exception is thrown if Browser is closed
}
}
}
解释.附加视图后,存储用户界面参考.并编写一种方法,根据需要更新视图,在该处执行ui.access().调用此方法进行更新,而不是在线程中执行UI.getCurrent().
Explained. Store UI reference when your View is attached. And write a method that updates the view as you need do the ui.access() there. Call this method to do the updates instead do doing UI.getCurrent() in the thread.
这是一个经常被问到的话题,这里也有关于同一件事的更具体的案例问题,
This is frequently asked topic, there is more specific case question about the same thing also here vaadin 10 - Push - Label won't update
这篇关于Vaadin中当前UI的访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!