我是Vaadin的新手,只是实现了一个用于测试功能的Prototype。目前我正在使用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?
答案 0 :(得分:3)
看看这个,第11.16.3节:https://vaadin.com/book/vaadin7/-/page/advanced.push.html
答案 1 :(得分: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>
<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