如何在事件接收后重新呈现Wicket组件

时间:2017-06-08 09:16:19

标签: ajax websocket wicket java-websocket

我有一个 Wicket 组件正在侦听某个事件(IEvent)。如果这样的事件到来,我想用改变的模型重新渲染组件。页面上没有活动控件,例如AjaxLink,可以触发重新渲染。

有没有办法刷新这种组件?

我想以某种方式从onEvent方法触发AJAX请求,并向所提到的组件添加AjaxBehavior。但我不知道,如何触发AJAX请求。

public class PersonPanel extends Panel implements Observer {

    private WebMarkupContainer wrapper;

    public PersonPanel(String id) {
        super(id);

        setDefaultModel(new CompoundPropertyModel<PersonInfo>(getModel()));

        wrapper = new WebMarkupContainer("wrapper");
        wrapper.setOutputMarkupId(true);
        add(wrapper);

        wrapper.add(new Label("personID"));
        // some more content
    }

    private IModel<PersonInfo> getModel() {
        return new LoadableDetachableModel<PersonInfo>() {
            @Override
            protected PersonInfo load() {
                // model loading logic
            }
        };
    }

    @Override
    public void onEvent(IEvent<?> event) {
        logger.debug("\n   Person Panel received an Event: " + event.getPayload());

        // Re-rendering of "wrapper" should be triggered from here.
    }

    @Override
    public void update(Observable observable, Object o) {
        send(this, Broadcast.EXACT, "Observable cache has changed.");
    }
}

2 个答案:

答案 0 :(得分:0)

send方面,您可以通过活动的AjaxRequestTarget传递payload

send(getPage(), Broadcast.DEPTH, new MyPayload(target));

然后在接收方:

MyPayload payload = (MyPayload) event.getPayload();
payload.getTarget().add(this);

答案 1 :(得分:0)

这是解决方案,感谢来自martin-g的提示,通过 WebSockets 解决。请参阅方法updateonEvent,并在组件上添加WebSocketBehavior

public class PersonPanel extends Panel implements Observer {

    private WebMarkupContainer wrapper;

    public PersonPanel(String id) {
        super(id);

        setDefaultModel(new CompoundPropertyModel<PersonInfo>(getModel()));

        wrapper = new WebMarkupContainer("wrapper");
        wrapper.setOutputMarkupId(true);
        add(wrapper);

        wrapper.add(new Label("personID"));
        // some more content

        add(new WebSocketBehavior() {
        });

        observableCache.addObserver(this);
    }

    private IModel<PersonInfo> getModel() {
        return new LoadableDetachableModel<PersonInfo>() {
            @Override
            protected PersonInfo load() {
                // model loading logic
            }
        };
    }

    @Override
    public void onEvent(IEvent<?> event) {
        if (event.getPayload() instanceof WebSocketPushPayload) {
            WebSocketPushPayload wsEvent = (WebSocketPushPayload) event.getPayload();
            wsEvent.getHandler().add(wrapper);
        }
    }

    @Override
    public void update(Observable observable, Object o) {
        WebSocketSettings webSocketSettings =
            WebSocketSettings.Holder.get(getApplication());
        WebSocketPushBroadcaster broadcaster =
            new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
        broadcaster.broadcastAll(
                getApplication(),
                new WebSocketMessage("WebSocket message from the PersonPanel."));
    }

}

您可以在Bitbucket上找到一个完整的运行示例项目,在 Wicket 8 Gradle 中实现: