如何使用GWT的内置MVP框架响应URL?

时间:2010-12-03 20:45:43

标签: gwt mvp

我正在构建一个非常简单的日历应用程序,以熟悉与GWT 2.1版本一起引入的MVP框架。
我想要实现的是能够在预定约会列表和可用时间列表之间切换 我创建了CalendarPlace,CalendarActivity,CalendarView和CalendarViewImpl。 我知道要导航到另一个地方我会调用PlaceController.goTo(Place),所以在我的日历应用中我会打电话:

clientFactory.getPlaceController.goTo(new CalendarPlace("freeTime");

URL为index.html#CalendarPlace:freeTime表示空闲时间列表或

clientFactory.getPlaceController.goTo(new CalendarPlace("appointments");

列出预约的约会。该URL将是index.html#CalendarPlace:约会

但问题是我在哪里回应不同的令牌?我猜CalendarPlace会是正确的地方,但我该怎么做呢? 这是我的源代码(我从教程here中获取了大部分样板文件:
CalendarPlace:

public class CalendarPlace extends Place {
    private String calendarName;
    public CalendarPlace(String token) {
        this.calendarName = token;
    }
    public String getCalendarName() {
        return calendarName;
    }
    public static class Tokenizer implements PlaceTokenizer<CalendarPlace> {
        @Override
        public CalendarPlace getPlace(String token) {
            return new CalendarPlace(token);
        }
        @Override
        public String getToken(CalendarPlace place) {
            return place.getCalendarName();
        }

    }
}

CalendarActivity:

public class CalendarActivity extends AbstractActivity
    implements
        CalendarView.Presenter {
    private ClientFactory clientFactory;
    private String name;
    public CalendarActivity(CalendarPlace place, ClientFactory clientFactory) {
        this.name = place.getCalendarName();
        this.clientFactory = clientFactory;
    }
    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }
    @Override
    public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
        CalendarView calendarView = clientFactory.getCalendarView();
        calendarView.setName(name);
        calendarView.setPresenter(this);
        containerWidget.setWidget(calendarView.asWidget());
    }
}

CalendarViewImpl:

public class CalendarViewImpl extends Composite implements CalendarView {
    private VerticalPanel content;
    private String name;
    private Presenter presenter;
    private OptionBox optionBox;
    public CalendarViewImpl() {
        //optionBox is used for navigation
        //optionBox is where I call PlaceController.goTo() from
        optionBox=new OptionBox();
        RootPanel.get("bluebar").add(optionBox);
        content=new VerticalPanel();
        this.initWidget(content);
    }
    @Override
    public void setPresenter(Presenter listener) {
        this.presenter=listener;
    }
    @Override
    public void setName(String calendarName) {
        this.name = calendarName;
    }

    public void displayFreeTime() {
        //called from somewhere to display the free time
    }
    public void getAppointments() {
        //called from somewhere to display the appointments
    }
}

1 个答案:

答案 0 :(得分:1)

在CalendarActivity构造函数中,您可以访问该位置,因此也可以访问该令牌。把它放在一边,然后在你的start()方法中你可以使用它。活动是为每个新导航创建的轻量级对象。