如何在vaadin中动态推送更新页面?

时间:2014-06-20 15:02:30

标签: java spring vaadin vaadin-push

我有VaadinSpring (+Security)的有效应用程序。我现在正尝试使用vaadin-push动态地将更新推送到客户端界面。

我想创建一个"视图"对所有客户来说都是一样的。视图应该通过push动态增加计数器。因此,当客户端显示页面时,计数器会自行更新(或者在我的示例中:将多个标签组件附加到页面)。每个客户都应该看到相同的价值。

但我没有看到推送到网页的更改。为什么呢?

//the static controller the dynamically changes content
@Controller
public class ViewPresenter {
    private static int counter = 0;

    @Autowired
    private void StaticView view;

    @Scheduled(fixedRate = 1000)
    public void refresh() {
        //push the content change periodically to frontend
        view.getUI().access(new Runnable() {
            @Override
            public void run() {
                view.addComponent(new Label("refresh: " + counter++);
                Sysout("update executed: " + counter); //OK, is printed
            }
        });
    }
}

//the static test component that gets a new label added on every content change
@VaadinComponent
public class StaticView extends CssLayout {
        //this is the view that every client should see
}

//the secured admin view
@VaadinView(name = "/secured")
@Scope("ui")
@Secured("user")
public class SecuredView extends VerticalLayout implements View {
    @Autowired
    private StaticView staticView;

    @Override
    public void enter(ViewChangeEvent event) {
        addComponent(staticView);
    }
}

//enable push
@Configuration
@Push
public class AppConfig {
}

@VaadinUI
@PreserveOnRefresh
public class ApplicationUI extends UI {

} 

只有手动刷新网页,我才能在客户端看到更改。但内容本身并没有自动改变。

也许@Push必须放在UI课程上?但在这种情况下,我会收到以下错误:

  

java.lang.IllegalStateException:无法挂起超过的响应   会话超时。在web.xml中增加session-timeout的值     在   org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314)     在   org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:292)     在   com.vaadin.server.communication.PushHandler $ 2.run(PushHandler.java:129)     在   com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:242)     在   com.vaadin.server.communication.PushHandler.access $ 200(PushHandler.java:55)     在   com.vaadin.server.communication.PushHandler $ 1.onRequest(PushHandler.java:73)

1 个答案:

答案 0 :(得分:0)

基于https://github.com/peholmst/vaadin4spring/issues/51#issuecomment-46875255的解决方案:

带有server.sessionTimeout=30

@Push(transport = Transport.LONG_POLLING)