1

いくつかのパネルがマップに通知し、マップが他のすべてのパネルに通知する場合のように、いくつかのウィケット パネル ストアを別のセッションの静的ハッシュマップに格納しています。例えば:

public class PanelMap{
    private static Map<Long, List<MyPanel>> map = new HashMap<Long, List<MyPanel>>();

public static void subscribe(Long id, MyPanel panel){
        if (!map.containsKey(id)){
            map.put(id, new ArrayList<MyPanel>());
        }
        map.get(id).add(panel);
    }
}

public static void notify(Long id, String notification){
        if (map.containsKey(id)){
            List<MyPanel> panels = map.get(id);
            for(MyPanel panel : panels){
                panel.newNotification(notification);
            }
        }
    }
}

パネルでは、newNotification(文字列通知) サーバーにリクエストを送信し、ブラウザでパネルを更新したいと考えています。

public void String newNotification(String notification){
   // do some business logic depends on notification
   onMyRequest();
}

私はウィケットの振る舞いのソースファイルの中からいくつかの検索AbstractDefaultAjaxBehaviorを行い、次のようにウィケットパネル内で独自の onRequest メソッドを作成しようとしました

private void onMyRequest(){
    AjaxRequestTarget target = ((WebApplication)getApplication()).newAjaxRequestTarget(getPage());
        target.add( _some_wicket_components_ );

        RequestCycle.get().scheduleRequestHandlerAfterCurrent(target);
    }

しかし、私がしたのは、Wicket Ajax Debug の Ajax エラーだけです。

 Wicket.Ajax.Call.processComponent: Component with id _containerdiv_ was not found while trying to perform markup update.
ERROR: Cannot find element with id: _someComponentIdOnPanel_

(これらのコンポーネントは存在します)

自分のリクエストをサーバーに送信するにはどうすればよいですか (または有効な AjaxRequestTarget を取得してコンポーネントを更新するにはどうすればよいですか?)

更新:セッション間通信が必要です。

4

2 に答える 2

2

異なるユーザーのセッションでパネルを更新するために、明らかに現在の AjaxRequestTarget を使用することはできません。これはある意味で、別のユーザーのブラウザーが知る方法がない、サーバーと要求しているユーザーの間の単一の通信を表すためです。(非常に基本的に話されています)

AjaxSelfUpdatingTimerBehaviorを使用して更新をポーリングすることもできます。これにより、変更されたパネルをアタッチするために使用できる定期的な間隔で、すべてのユーザーに対して新しい AjaxRequestTarget が生成されます。これは非常に基本的で単純な実装であり、システムのパフォーマンスに影響を与え、かなりの量のトラフィックを生成する可能性があります。

もう 1 つの方法は、Wicket-Atmosphere (クイックスタートはこちら) でサポートされているAtmosphereのようなものを使用することで、 wicket-library.comにいくつかの例があります。

于 2013-07-01T11:10:35.463 に答える
1

Wicket イベント バス システムを使用します。無料の Wicket ガイドの「Wicket イベント インフラストラクチャ」の章をご覧ください。

AjaxRequestTarget最初に、通知をカプセル化し、イベント インフラストラクチャを使用してそれらを渡すクラスを 1 つ作成する必要があります。

private class Notification {
    private String message;
    private AjaxRequestTarget target;

    ... constructor, getters, setters...
}

次に、イベントを受け取りたいパネルはonEvent、次のようにメソッドをオーバーライドする必要があります。

public void onEvent(IEvent<?> event) {
    if (event.getPayload() instanceof Notification) {
        Notification notification = (Notification) event.getPayload();

        ... do whatever you want before updating the panel ...

        // Update the panel 
        notification.getTarget().add(this);

    }
}

すべてのコンポーネントは、Wicket イベント インフラストラクチャを使用して送信されるすべてのイベントを受け取ります。したがって、このような1つの方法を使用して、他のパネルからイベントを送信できます

protected void sendMessage(String message, AjaxRequestTarget target) {
    send(getSession(), Broadcast.BREADTH, new Notification(message, target));
}

AJAX を使用してコンポーネントを更新する場合は、 を設定する必要があることに注意してくださいsetOutputMarkupId(true)。また、非表示にできるコンポーネントを AJAX を使用して表示したい場合は、 を設定する必要がありますsetOutputMarkupPlaceholderTag(true)

于 2013-06-30T02:28:30.847 に答える