简体   繁体   English

GWT:在视图展示位置之间传递数据

[英]GWT : Pass data between view impl to place

I have a view impl say SampleView.java.I have a table in my view.When a row is clicked, a new place is passed to the place controller like, 我有一个视图,表示说SampleView.java。我的视图中有一个表。单击一行时,一个新的地方将传递给位置控制器,例如

placeController.goto(new ScreenPlace()) ;

Now the issue is, i need to pass some data from SampleView to ScreenPlace , so that i can use that data in the ScreenView.Data is basically a MAP of key value pair which i need to pass from SampleView to ScreenView via ScreenPlace.Or in other words, how can i transfer data from one view to another view.How can i do this?.I'm a newbie to GWT.Please help!. 现在的问题是,我需要将一些数据从SampleView传递到ScreenPlace,以便我可以在ScreenView中使用该数据.Data本质上是键值对的MAP,我需要通过ScreenPlace从SampleView传递给ScreenView。换句话说,如何将数据从一个视图传输到另一视图。我该怎么做?我是GWT的新手。请帮忙!

There are basically two ways to solve this problem: 基本上有两种方法可以解决此问题:

  1. Pass the data via an EventBus : You can create an Event (ie LoadSampleDataEvent ) that you pass the data to and fire in on the global EventBus . 通过 EventBus 传递数据 :您可以创建一个事件(即LoadSampleDataEvent ),将数据传递给该事件并在全局EventBusEventBus Because you will have a reference to the global EventBus in your ScreenPlace you will be able to handle the Event there and display it in the corresponding view. 因为你将不得不在全球参考EventBusScreenPlace ,你将能够在那里处理该事件,并在相应的视图显示它。

  2. Encode the data in your URL : You will probably have an URL associated with your ScreenPlace (ie http://myHost/#screen ). 将数据编码到您的URL中 :您可能会有一个与ScreenPlace关联的URL (即http://myHost/#screen )。 You can encode some unique identifier of your data in that url http://myHost/#screen?id=1 or http://myHost/#screen/1 ). 您可以在该网址http://myHost/#screen?id=1http://myHost/#screen/1中对数据的一些唯一标识符进行编码。 Then in your ScreenPlace you can parse the id parameter from the URL and retrieve the data. 然后,在ScreenPlace您可以从URL解析id参数并检索数据。 You will probably need to pass the map that contains the data to your ScreenPlace . 您可能需要将包含数据的地图传递到ScreenPlace This can be done via an Event on the EventBus or a global Singleton instance. 这可以通过EventBus上的Event或全局Singleton实例来完成。

I would recommend the second appraoch, as it makes your ScreenPlace "stateless" and boomarkable. 我会建议第二appraoch,因为它使你的ScreenPlace “无状态”和boomarkable。 A user can just open the URL (ie http://myHost/#screen?id=10 ) and your ScreenPlace will show the detailed view. 用户只需打开URL(即http://myHost/#screen?id=10 ),您的ScreenPlace将显示详细视图。

For that to work you must handle the case when the map hasn't been loaded yet (for example if the user directly navigates to the corresponding URL and not through your table first). 为此,您必须处理尚未加载地图的情况(例如,如果用户直接导航到相应的URL而不是先不通过您的表)。

Handler Interface:


import com.google.gwt.event.shared.EventHandler;

public interface MessageTransferHandler extends EventHandler
 {
    public void onMessageTransfer(MessageTransferEvent event);
}

Event Class:



import com.google.gwt.event.shared.GwtEvent;

public class MessageTransferEvent extends GwtEvent<MessageTransferHandler>{

    String message;
    int flag;

    public MessageTransferEvent(String message,int flag)
    {
        this.message=message;
        this.flag=flag;
    }
    public String getMessage()
    {
        return message;
    }

    public int getFlag()
    {
        return flag;
    }
    public static Type<MessageTransferHandler> TYPE=new Type<MessageTransferHandler>();
    @Override
    public com.google.gwt.event.shared.GwtEvent.Type<MessageTransferHandler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(MessageTransferHandler handler) {
        handler.onMessageTransfer(this);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM