8

RequestFactory可能であれば、すべてのクライアント/サーバー通信にGWT を使用したいと考えています。私の理解では、にマップ/gwtServletRequestFactoryServlet、アノテーションをweb.xml使用して、クライアント側のリクエストを適切なサービスにマップする方法を伝える必要があるということです。@ServiceRequestFactoryServlet

クライアント側とサーバー側の両方で、誰かがこのプロセスの完全なコード例を提供できますか? Widgetクライアント側からサーバー側のサービスにオブジェクトを送信したいWidgetProcessor:

public class Widget {
    // This is a domain object (POJO).
}

public class WidgetProcessor {
    public void processWidget(Widget w) {
        // Inspect the Widget. If certain properties contain certain
        // values, place it on a queue. Else "process" the Widget
        // and persist it to a DB.

        if(w.containsSpecialValues())
            QueueManager.sendToQueue(w);
        else {
            // Process widget...

            WidgetDAO.save(w);
        }
    }
}

GWT 以外のコンテキストでは、 を定義してWidgetProcessorServletにマップし/processWidget、次のようにします。

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
    WidgetProcessor widgetProcessor = new WidgetProcessor();
    widgetProcessor.processWidget(getWidgetFromRequest(request));
}

RequestFactoryこれは-landでどのように機能しますか? 前もって感謝します。

4

5 に答える 5

4

RequestFactories でも​​可能ですが、GWT-RPC でも可能です。RequestFactory の方が好きです。

次のクラスとインターフェースがあります。

サーバー上:

  1. ウィジェット (サーブレット)
  2. WidgetProcessor

クライアント上:

  1. ウィジェットプロキシ
  2. ウィジェットリクエスト
  3. WidgetRequestFactory
  4. WidgetPresenter

ウィジェットサーブレット

package com.foo.bar.server;
public class Widget {
   //Your methods
    public void yourInstanceMethod(){
       //foo
    }
    public static void yourStaticMethod(){
       //bar
    }
    public static void processWidget(Widget w){
       WidgetProcessor widgetProcessor = new WidgetProcessor();
       widgetProcessor.processWidget(getWidgetFromRequest(request));
    }
}

WidgetProcessor

package com.foo.bar.server;
public class WidgetProcessor {
    public void processWidget(Widget w) {
        // Inspect the Widget. If certain properties contain certain
        // values, place it on a queue. Else "process" the Widget
        // and persist it to a DB.

        if(w.containsSpecialValues())
            QueueManager.sendToQueue(w);
        else {
            // Process widget...

            WidgetDAO.save(w);
        }
    }
}

ウィジェットプロキシ

package com.foo.bar.server;
import com.foo.bar.server.Widget;
@ProxyFor(value = Widget.class)
public interface WidgetProxy extends EntityProxy{
    //Your getter and Setter methods
}

ウィジェットリクエスト

package com.foo.bar.client;
import com.foo.bar.server.Widget;
@Service(value = Widget.class)
public interface WidgetRequest extends RequestContext{
    InstanceRequest<WidgetProxy, Void> yourInstanceMethod();
    Request<Void> yourStaticMethod();
    Request<Void> widgetProcessor(WidgetProxy widget);
}

WidgetRequestFactory

package com.foo.bar.client;
public interface WidgetRequestFactory extends RequestFactory{
    WidgetRequest widgetRequest();      
}

WidgetPresenter

package com.foo.bar.client;
public class WidgetPresenter {
    private final WidgetRequestFactory rf = GWT.create(WidgetRequestFactory.class);
    public WidgetPresenter() {
        rf.initialize(new EventBus());
        rf.widgetRequest().widgetProcessor().fire(new Receiver<Void>() {
            @Override
            public void onSuccess() {

                //Do what you want to confirm to user...
            }
        });
    }
}

さらに、処理されたウィジェットをユーザーに再度応答する場合は、次のようにします。

@Service(value = Widget.class)
public interface WidgetRequest extends RequestContext{
    ...
    Request<WidgetProxy> widgetProcessor(WidgetProxy widget);
}

public class Widget {
    ...
    public static void processWidget(Widget w){
       WidgetProcessor widgetProcessor = new WidgetProcessor();
       return widgetProcessor.processWidget(getWidgetFromRequest(request));
    }
}

public class WidgetProcessor {
    public Widget processWidget(Widget w) {
        // Inspect the Widget. If certain properties contain certain
        // values, place it on a queue. Else "process" the Widget
        // and persist it to a DB.

        if(w.containsSpecialValues())
            QueueManager.sendToQueue(w);
        else {
            // Process widget...

            WidgetDAO.save(w);
        }
        return w;
    }
}

public class WidgetPresenter {
    private final WidgetRequestFactory rf = GWT.create(WidgetRequestFactory.class);
    public WidgetPresenter() {
        rf.initialize(new EventBus());
        rf.widgetRequest().widgetProcessor().fire(new Receiver<WidgetProxy>() {
            @Override
            public void onSuccess(WidgetProxy response) {

                WidgetView v = new WidgedView(response);
                            RootPanel.get().add(view);
            }
        });
    }
}

package com.foo.bar.client;
public class WidgetView {


    public WidgetView(WidgetProxy widget) {

        //paint widget with widget
            // widget.getSomeProperty(); etc.

    }
}
于 2013-02-20T16:50:06.277 に答える
4

RequestFactory のシナリオについて少し混乱しています。

ここから始める必要があります - https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory

サンプル RequestFactory デモ @ DynatableRF コードを参照する - http://code.google.com/p/google-web-toolkit/source/browse/#svn/trunk/samples/dynatablerf

サンプル RequestFactory デモ @ DynatableRF コードをダウンロード - http://google-web-toolkit.googlecode.com/files/gwt-2.5.0.zip

編集 - RequestFactory の例は、MVP、アクティビティ、およびエディター フレームワークと複雑になります。シナリオに必要な RF、エディター、MVP、およびアクティビティの組み合わせを決定するには、多大な労力が必要です。

于 2013-02-18T15:34:27.297 に答える
3

Request Factoryこの目的のためにブレインストーミングを行う必要はないと思います。

私の意見Gwt RPCによると、それは非常に簡単です。

簡単に言えば、次のような単純な RPC 構造です。

GWT Code <===> InterfaceAsync <===> Interface (Synchronous)<===> Server Code 

私はあなたとそれ自身の要素を説明しようとしています.

同期インターフェース (RPC 全体の中心):

import com.google.gwt.user.client.rpc.RemoteService;
public interface WidgetRPCInterface extends RemoteService
{
    public Widget widgetProcessRPC(Widget myWidget);
}

ASynchronous インターフェイス (クライアントの主要部分):

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface WidgetRPCInterfaceAsync
{
    public void widgetProcessRPCWidget myWidget, AsyncCallback callback);
}

ここでは、Service「WidgetRPCInterface」を実装する (Equals to servlet) を使用します。

public class WidgetRPCImpl extends RemoteServiceServlet implements RPCInterface
{
    private static final long serialVersionUID = 1L;

    public Widget widgetProcessRPCWidget myWidget)
    {
       //Process your widget  here (CRUD operations)
       //You can change return type and return what ever you want to client . 
    }

**you can override doget,doPost,doDelete....etc along with your methods 
}

上記のクラスを web.xml にマップします。

     <servlet>
    <servlet-name>widgetrpc</servlet-name>
    <servlet-class>com.server.WidgetRPCImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>widgetrpc</servlet-name>
    <url-pattern>/widgetrpc</url-pattern>
  </servlet-mapping>

最後に、GWT コードで以下のようにサービスを使用します。

コードでの使用:

//サービスを登録します。

   private final WidgetRPCInterfaceAsync widgetService = 
        GWT.create(WidgetRPCInterface.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint('widgetrpc');

コールバックでサーバーをリクエストする

widgetService.widgetProcessRPC(widgetFromClient, callback);
 AsyncCallback callback = new AsyncCallback()
    {
        public void onFailure(Throwable caught)
        {
           //Do on fail
        }

        public void onSuccess(Object result)
        {
           //Process successfully done with result (result is which you
           returned in impl class) .
        }
    };

PS .パッケージ構造に注意してください:

WidgetRPCInterfaceAsync 、WidgetRPCInterfaceは client* パッケージに含まれている必要があります

Widgetクラスは shared* パッケージに含まれている必要があります

WidgetRPCImpl は server* パッケージに含まれている必要があります

そしてRPCの例を見てください

于 2013-02-24T10:09:14.263 に答える
1

あなたの目的のために、単純なスケルトンプロジェクトを開発しました。Github で私の回答widget-processor を確認してください。

このプロジェクトは、Maven ビルダーと Java EE プラットフォーム (GlassFish) に基づいています。

プロジェクト モジュール:

  1. ear- 共通耳モジュールは、展開が容易な 1 つの EAR パッケージに他のモジュールを組み込みます。
  2. persistence- このモジュールにはファイルが 1 つしかなくpersistence.xml、すべての外部エンティティが 1 つのデータ ソースにリンクされます。
  3. ejb- このモジュールにはエンティティWidgetとステートレス EJBがWidgetProcessorあります。
  4. web- RequestFactory を使用した一般的な GWT モジュール。

すべてを適切にビルドするように調整された Maven ビルド プロセス。プロジェクトは問題なく IntelliJ Idea にインポートできました。また、標準の Idea ツールを使用して Glassfish でデバッグすることもできます。

また、GWT RF および Persistence ボイラープレート コードをGithubの外部bilionix-coreアーティファクトに移動しました。

于 2013-02-25T10:42:49.777 に答える
1

Your Widget class will be on the server-side. On the client-side you will use an interface to represent a Widget; this interface will be a proxy for the actual Widget object. You associate the proxy interface with the class using the @ProxyFor annotation. For example, if you have the Widget class on the server, you could have the WidgetProxy interface on the client:

@ProxyFor(value = Widget.class)
public interface WidgetProxy extends EntityProxy {
}

プロキシにはエンティティのプロパティのゲッターとセッターが含まれており、これらのゲッターとセッターはサーバー上のクラスにミラーリングされます。リクエスト ファクトリには、プロキシが関連付けられているクラスを特定する方法も必要です。これには「ロケーター」クラスを使用します (他の方法もあります)。ロケーターは @Proxy アノテーションでも指定されます。

@ProxyFor(value = Widget.class, locator = WidgetLocator.class)

The WidgetLocator class is also on the server side. For each Widget that you want to insert or update you will need to create a RequestContext. The RequestContext identifies the methods that will operate against the entity. The actual implementation of those methods is again on the server side ... you can either introduce a separate class containing the methods, or add them to the entity class (Widget in your case). I prefer to separate them into a "DAO" class, limiting the entity class (i.e., Widget) to a simple bean. (Your DAO class is named WidgetProcessor). The RequestContext on the client side is an interface that extends the RequestContext interface. Your RequestContext interface tells request factory where to find the DAO on the server, and that's where you use the @Service annotation.

Take a look at Thomas Broyer's blog for a good introduction: GWT 2.1.1 RequestFactory. I also found the book GWT in Action (Second Edition) a useful resource. I created an example based upon my learning experience, which may also be useful to you: A GWT Request Factory Journey; again, this may or may not be useful!

于 2013-02-20T13:49:10.997 に答える