Vaadin:获取UI更改数据的参考 [英] Vaadin: get reference of UI to change data

查看:50
本文介绍了Vaadin:获取UI更改数据的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Vaadin UI中更改数据.更改通过rest调用来调用.在那里,我不知何故需要对UI类的引用来调用其方法,例如changeValue(字符串值).

I want to change data inside a Vaadin UI. The change is invoked by a a rest call. There, i somehow need a reference to the UI class to call its method´, e.g. changeValue(string value).

我正在使用vaadin-spring-boot-starter 1.0.0

I'm using vaadin-spring-boot-starter 1.0.0

这有可能吗?

编辑:现在还有一个问题: 我正在尝试在视图内部执行@Eric提到的服务器推送,以便在广播消息上更新视图.但是,这是行不通的(没有例外,没有要调试的东西,只有视图中没有更新).这就是我在视图中所做的:

EDIT: Another question now: I was trying to do that Server Push, mentioned by @Eric, inside of a View, so that the view will get updated on a Broadcast message. However, this is not working (no exceptions, nothing to debug, just no updates in the view). This is what i do in my View:

@UIScope
@SpringView(name = LoadWebsiteView.VIEW_NAME)
@Push
public class LoadWebsiteView extends VerticalLayout implements View, Broadcaster.BroadcastListener {
...

@Autowired
public LoadWebsiteView(ScraperMainUI scraperMainUi) {
    this.scraperMainUi = scraperMainUi;
    Broadcaster.register(this);
    initControlPane();
}

@Override
public void receiveBroadcast(String message) {
    scraperMainUi.access(new Runnable() {
        @Override
        public void run() {
            urlTxtField.setValue(message);
        }
    });
}

这是我在我的restcontroller中做的简单事情:

and here is the simple stuff i do in my restcontroller:

Broadcaster.broadcast(text);

推荐答案

您正在寻找的是Vaadin的Push功能,以及一种将消息发送到已注册客户端"列表的方法(在这种情况下,是Vaadin UI的用户)需要了解更改).

What you are looking for is Vaadin's Push feature and a way to send a message to a list of registered "clients" (in this case, the Vaadin UIs who need to known about the changes).

您可以在此处了解有关Vaadin推送的信息:启用服务器推送以及文章高级推送

You can read about Vaadin Push here: Enabling Server Push and also in the article Advanced Push

通过Vaadin推送功能,您的服务器可以强制对客户端进行更新,而不必等待浏览器再次请求.

The Vaadin push function allows your server to force updates to the client instead of waiting on the browser to request again.

消息组件只是用来告知已订阅的UI他们需要执行更新的一种方式.

The message component simply acts as a way to tell subscribed UIs that there is an update they need to action.

这就是说,我有一个项目,其作用与多个用户正在执行的项目相同,并且有Spring计划的任务也可以影响用户需要了解的更改.

This said, I have a project that does about the same as multiple users are actioning items and there are Spring scheduled tasks that also can effect changes the user needs to know about.

请注意,以下示例基于启用服务器中的示例推送文章.

Note, the below examples are based on the examples available in Enabling Server Push article.

Broadcaster.java -充当注册实例以接收广播并提供发送广播功能的机制.在下面的示例中,我有一个表示消息的类(BroadcastMessage),但是您可以简化它.

Broadcaster.java - Acts as the mechanism that registers instances to receive broadcasts and provides a facility to send broadcasts. In the below example, I have I have a class that represents a message (BroadcastMessage) but you could simplify it of course.

public class Broadcaster implements Serializable {

    private static final long serialVersionUID = 3540459607283346649L;

    static ExecutorService executorService = Executors.newSingleThreadExecutor();

    private static LinkedList<BroadcastListener> listeners = new LinkedList<BroadcastListener>();

    public interface BroadcastListener {
        void receiveBroadcast(BroadcastMessage message);
    }   

    public static synchronized void register(BroadcastListener listener) {
        listeners.add(listener);
    }

    public static synchronized void unregister(BroadcastListener listener) {
        listeners.remove(listener);
    }

    public static synchronized void broadcast(final BroadcastMessage message) {
        for (final BroadcastListener listener: listeners)
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    listener.receiveBroadcast(message);
                }
            });
    }
}

这是我为我的BroadcastMessage定义的类.这个想法是要有一种方法来表示我拥有的消息类型以及地图形式的一些有效载荷

Here is the class I defined for my BroadcastMessage. The idea is to have a way to denote what kind of message I have and also some payload in the form of a Map

public class BroadcastMessage implements Serializable {

    private static final long serialVersionUID = 5637577096751222106L;

    public BroadcastMessageType messageType;
    public Map<String, String> params;

    public BroadcastMessage() {
    }

    public BroadcastMessage(BroadcastMessageType messageType) {
        this.messageType = messageType;
        this.params = new HashMap<String, String>();
    }

    public BroadcastMessage(BroadcastMessageType messageType, Map<String, String> params) {
        this.messageType = messageType;
        this.params = params;
    }

    public BroadcastMessageType getMessageType() {
        return messageType;
    }
    public void setMessageType(BroadcastMessageType messageType) {
        this.messageType = messageType;
    }
    public Map<String, String> getParams() {
        return params;
    }
    public void setParams(Map<String, String> params) {
        this.params = params;
    }

}

这是示例Vaadin UI,希望收听广播. 请注意@Push注释.如果没有此选项,客户端将仅在浏览器决定时刷新. @Push使其立即生效**

This is an example Vaadin UI that wants to listen for Broadcasts. Note the @Push annotation. Without this, the client will only refresh when the browser decides to. @Push makes it immediate**

@SpringComponent
@UIScope
@Push
@SpringView(name=TaskListComponent.NAME)
public class TaskListComponent extends MyCustomComponent implements Broadcaster.BroadcastListener, View {

    /**  PRUNED DOWN, TO DEMONSTRATE THE KEY CODE  **/

    // Register this window when we enter it
    @Override
    public void enter(ViewChangeEvent event) {
        Broadcaster.register(this);

    }

     // Must also unregister when the UI expires    
    @Override
    public void detach() {
        Broadcaster.unregister(this);
        super.detach();
    }

    // Receive a broadcast
    @Override
    public void receiveBroadcast(BroadcastMessage message) {

        getUI().access(new Runnable() {
            @Override
            public void run() {
                // DO WHATEVER YOU NEED TO DO HERE.
                // I CALLED INITIALIZE BUT IT COULD BE 
                // JUST YOU FIELD UPDATE
                if ( message.getMessageType().equals(BroadcastMessageType.REFRESH_TASK_LIST) )
                    initialize();
            }
        }); 

    }

}

要从休息界面发送消息,请执行以下操作:

To send a message from your rest interface:

Broadcaster.broadcast( 
    new BroadcastMessage( 
        BroadcastMessageType.AUTO_REFRESH_LIST
    ) 
);

希望这会有所帮助! :)

Hope this helps! :)

这篇关于Vaadin:获取UI更改数据的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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