2

我是 Vaadin 的新手,只是实现了一个原型来测试功能。目前我在玩 Vaadin Server-Push 功能。我喜欢做一个可以通过 Button 增加的简单计数器。之后我喜欢从不同的浏览器打开这一面,当我计数时,我喜欢更新所有浏览器。

现在我知道当前的浏览器是由一个单独的线程更新的。

public class PushTestView extends VerticalLayout implements View {

   private static final long serialVersionUID = -8056849522760207972L;
   private Label counter;

   public void enter(ViewChangeEvent event) {
      removeAllComponents();
      PushTestController controller = PushTestController.getInstance();
      counter = new Label("" + controller.getCounter());
      Button button = new Button("Count up");

      button.addClickListener(new ClickListener() {
         private static final long serialVersionUID = -1437136914245802675L;

         @Override
         public void buttonClick(ClickEvent event) {
            InitializerThread thread = new InitializerThread();
            thread.run();
         }
      });

      addComponent(counter);
      addComponent(button);

   }

   class InitializerThread extends Thread {
      @Override
      public void run() {
         // Init done, update the UI after doing locking
         UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
               // Here the UI is locked and can be updated
               PushTestController.getInstance().countUp();
               counter.setValue("" + PushTestController.getInstance().getCounter());
               UI.getCurrent().notifyAll();
            }
         });
      }
   }
}

这个视图嵌入到我的 UI 中,由@Push. 这是我的控制器:

public class PushTestController {

   private static PushTestController instance = new PushTestController();
   private int counter = 0;

   private PushTestController() {
      //Private Constructor for Singleton 
   }

   public static PushTestController getInstance() {
      return instance;
   }

   public int getCounter() {
      return counter;
   }

   public String getCounterString() {
      return "" + counter;
   }

   public void countUp() {
      counter++;
   }
}

我如何扩展此示例以更新不同浏览器中的 UI?

4

2 回答 2

3

看看这个,第 11.16.3 节:https ://vaadin.com/book/vaadin7/-/page/advanced.push.html

于 2013-11-20T16:57:48.463 回答
2

您必须在您的项目中使用 servlet 3.0 并编辑您的 ivy.xml(或者如果您使用 Maven,则使用 pom.xml)。之后,您必须添加 @Push 注释以启用 ui 推送。像这样的东西:

<dependency org="com.vaadin" name="vaadin-push" rev="&vaadin.version;" conf="default->default" />

在 Maven 中:

<dependency>
  <groupId>com.vaadin</groupId>
  <artifactId>vaadin-push</artifactId>
  <version>${vaadin.version}</version>
</dependency>

小服务程序配置:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
        <param-name>ui</param-name>
        <param-value>org.vaadin.example.MyUI</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>

最后是@Push

@Push 
public class PushTestUI extends UI 

参考这里

于 2013-07-16T14:15:15.307 回答