0

当数据库发生某些更改时,如何更新 UI 上的数据?我希望我的用户界面在我更改数据库上的记录(使用通用数据库查看器)时自动更新数据,就像 Facebook 在某人朋友在剪贴板上发布某些内容时所做的那样(不刷新)

4

1 回答 1

0

您可以使用轮询或推送。

对于推动,您可以扩展 SelectorComposer 并应用于您的 zul

public class ComposerPush extends SelectorComposer<Component>  {

    private static final long serialVersionUID = 1L;
// wire components
@Wire
ZHighCharts chartComp;

// Basic Line
private SimpleExtXYModel dataChartModel = new SimpleExtXYModel();


 public void startWorker() {
            final Desktop desktop = Executions.getCurrent().getDesktop();
            if (desktop.isServerPushEnabled()) {
            } else {
                desktop.enableServerPush(true);
                if(timer != null)timer.cancel();
                timer = new Timer();
                timer.schedule(databaseCheck(), 0, 5000);
            }
        }

    private TimerTask databaseCheck() { 
            return new TimerTask() {
                @Override
                public void run() {
                    updateInfo();
                }
            };
        }

        private void updateInfo() {
            try {
                 Desktop desktop = chartComp.getDesktop();
                if(desktop == null) {
                    timer.cancel();
                    return;
                }

                Executions.activate(desktop);
                try {

                          **<your code in here>**

                } finally {
                    Executions.deactivate(desktop);
                }
            } catch (DesktopUnavailableException ex) {
                System.out.println("Desktop currently unavailable");
                timer.cancel();
            } catch (InterruptedException e) {
                System.out.println("The server push thread interrupted");
                timer.cancel();
            }


        }
于 2013-10-21T14:58:43.600 回答