其他分享
首页 > 其他分享> > spring – Vaadin:获取UI的参考来改变数据

spring – Vaadin:获取UI的参考来改变数据

作者:互联网

我想在Vaadin UI中更改数据.休息调用会调用此更改.在那里,我不知何故需要引用UI类来调用它的方法,例如changeValue(字符串值).

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

这有可能吗?

编辑:现在的另一个问题:
我试图在视图中执行@Eric提到的服务器推送,以便视图将在广播消息上得到更新.但是,这不起作用(没有例外,没有调试,只是在视图中没有更新).这就是我在视图中所做的:

@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中做的简单的事情:

Broadcaster.broadcast(text);

解决方法:

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

你可以在这里阅读Vaadin Push:Enabling Server Push以及文章Advanced Push

Vaadin推送功能允许您的服务器强制更新客户端,而不是等待浏览器再次请求.

消息组件只是作为一种方式告诉订阅的UI他们需要采取行动的更新.

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

请注意,以下示例基于Enabling Server Push文章中提供的示例.

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

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定义的类.我的想法是有一种方法来表示我有什么样的消息,还有一些地图形式的有效载荷

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让它立竿见影**

@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();
            }
        }); 

    }

}

要从休息界面发送消息:

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

希望这可以帮助!

标签:vaadin7,spring,spring-boot,vaadin
来源: https://codeday.me/bug/20191003/1848426.html