vaadin 10 - 推送 - 标签不会更新 [英] vaadin 10 - Push - Label won't update

查看:13
本文介绍了vaadin 10 - 推送 - 标签不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainView 包含信息组件:

MainView include InformationCOmponent:

@Push
@Route
public class MainView extends VerticalLayout {       
    InformationComponent infoComponent;

    public MainView(@Autowired StudentRepository studentRepo, @Autowired Job jobImportCsv, @Autowired JobLauncher jobLauncher, @Value("${file.local-tmp-file}") String inputFile) {     
    [...] // some stuffs

    infoComponent = new InformationComponent(studentRepo);
    add(infoComponent);
    }

    //update when job process is over
    private void uploadFileSuccceed() {
       infoComponent.update(myUploadComponent.getFile());
    }

信息组件:

public class InformationComponent extends HorizontalLayout {

    StudentRepository studentRepo;

    Label nbLineInFile = new Label();

    VerticalLayout componentLeft = new VerticalLayout();;
    VerticalLayout componentRight = new VerticalLayout();;

    public InformationComponent(StudentRepository studentRepo) {
    [...] // some init and style stuff

    addLine("Nombre de lignes dans le fichier", nbLineInFile);
    }

    private void addLine(String label, Label value) {
    componentLeft.add(new Label(label));
    componentRight.add(value);
    }

    public void update(File file) {
    try {


        long nbLines = Files.lines(file.toPath(), Charset.defaultCharset()).count();

        System.out.println("UPDATED! " +nbLines); // value is display in console ok!
        UI.getCurrent().access(() -> nbLineInFile.setText(nbLines)); // UI is not updated!!
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
  }
}

当我从 MainView 调用 InformationComponent 标签未在浏览器中更新.

When I call InformationComponent from MainView the Label is not update in the browser.

UI.getCurrent().access(() -> nbLineInFile.setText(nbLines))

也可以尝试 wwith @Push(PushMode.MANUAL) 和 ui.push();但也不起作用...

also try wwith @Push(PushMode.MANUAL) and ui.push(); but doesn't work either...

完整的源代码在这里:https://github.com/Tyvain/ProcessUploadedFile-Vaadin_SpringBatch/tree/push-not-working

推荐答案

我怀疑这里的问题是 uploadFileSuccceed() 从后台线程运行,在这种情况下 UI.getCurrent() 将返回 null.这将导致 NullPointerException 终止后台线程,或者异常被调用者捕获并静默忽略.另一种选择是 uploadFileSuccceed() 通过不同的浏览器窗口发生,因此也是不同的 UI 实例,这意味着更改将在错误的上下文中推送<代码>用户界面.

I suspect the problem here is that uploadFileSuccceed() is run from a background thread, in which case UI.getCurrent() will return null. This would cause a NullPointerException that either kills the background thread or alternatively the exception is caught and silently ignored by the caller. Another alternative is that uploadFileSuccceed() happens through a different browser window and thus also a different UI instance, which means that the changes would be pushed in the context of the wrong UI.

正是出于这些原因,UI.getCurrent().access(...) 通常是一种反模式,尽管不幸的是它在旧示例中​​被广泛使用.

For exactly these reasons, UI.getCurrent().access(...) is generally an anti pattern, even though it's unfortunately quite widely used in old examples.

您可以通过在 update 方法的开头记录 UI.getCurrent() 的值,并将其与UI.getCurrent() 的值,例如在 InformationComponent 的构造函数中.

You can check whether this is the cause of your problem by logging the value of UI.getCurrent() in the beginning of the update method, and comparing that to the value of UI.getCurrent() e.g. in the constructor of InformationComponent.

要正确解决问题,您应该将正确的 UI 实例传递给整个事件链,该事件链源自触发后台处理启动的任何事件.您还应该注意,使用任何 Component 子类中可用的 getUI() 方法可能很诱人,但该方法不是线程安全的,因此应避免使用在后台线程中.

To properly fix the problem, you should pass the correct UI instance through the entire chain of events originating from whatever triggers the background processing to start. You should also note that it might be tempting to use the getUI() method that is available in any Component subclass, but that method is not thread safe and should thus be avoided in background threads.

最后一点,在这种情况下,我建议使用 SpanText 组件而不是 Label 组件.在 Vaadin 10 中,Label 组件已更改为使用 HTML 元素,这意味着它主要用作输入组件的标签.

As a final notice, I would recommend using the Span or Text component instead of Label in cases like this. In Vaadin 10, the Label component has been changed to use the <label> HTML element, which means that it's mainly intended to be used as the label of an input component.

这篇关于vaadin 10 - 推送 - 标签不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆