GWT 2.1给出了没有活动的示例

GWT 2.1给出了没有活动的示例,gwt,Gwt,有没有人举过一些例子,说明如何在不使用活动进行历史管理的情况下使用场所。我很快找到了一些东西,可以看到url随着浏览器后退和浏览器前进的点击而改变,但显示不到任何地方 我使用的是一个装饰的选项卡面板,它有一个SelectionHandler,可以触发getPlaceController().goTo(place) 任何想法都是有用的 下面是我制作的一段简单代码,用于演示您的期望。它基于GWT和MVP开发文档() 在本例中,您可以在两个选项卡之间导航。在选择时,将创建一个新的历史记录项(无任何活动

有没有人举过一些例子,说明如何在不使用活动进行历史管理的情况下使用场所。我很快找到了一些东西,可以看到url随着浏览器后退和浏览器前进的点击而改变,但显示不到任何地方

我使用的是一个装饰的选项卡面板,它有一个SelectionHandler,可以触发getPlaceController().goTo(place)


任何想法都是有用的

下面是我制作的一段简单代码,用于演示您的期望。它基于GWT和MVP开发文档()

在本例中,您可以在两个选项卡之间导航。在选择时,将创建一个新的历史记录项(无任何活动)。只要使用浏览器按钮返回/前进,页面就会正确更新

我定义了一个地方,一个活动和它的观点。我已根据需要调整了AppActivityMapper、AppActivityManager和ClientFactory。代码是轻量级的,不需要注释就可以理解。我只是在需要的时候做了一些解释,但如果不清楚,请不要犹豫

ExampleView.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}
public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}
public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}
public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}
/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}
@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}
示例ViewImpl.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}
public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}
public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}
public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}
/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}
@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}
ExampleActivity.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}
public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}
public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}
public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}
/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}
@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}
ExamplePlace.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}
public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}
public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}
public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}
/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}
@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}
AppPlaceHistoryMapper.java

public interface ExampleView extends IsWidget {
    void selectTab(int index);
}
public class ExampleViewImpl extends Composite implements ExampleView, SelectionHandler<Integer> {

private DecoratedTabPanel panel;

public ExampleViewImpl() {
    panel = new DecoratedTabPanel();

    initComposite();

    initWidget(panel);
}

private void initComposite() {              
    panel.add(new HTML("Content 1"), "Tab 1");
    panel.add(new HTML("Content 2"), "Tab 2");

    panel.selectTab(0);

    panel.addSelectionHandler(this);
}

@Override
public void selectTab(int index) {
    if (index >=0 && index < panel.getWidgetCount()) {
        if (index != panel.getTabBar().getSelectedTab()) {
            panel.selectTab(index);
        }
    }
}

@Override
public void onSelection(SelectionEvent<Integer> event) {
    // Fire an history event corresponding to the tab selected
    switch (event.getSelectedItem()) {
    case 0:
        History.newItem("thetabplace:1");
        break;
    case 1:
        History.newItem("thetabplace:2");
        break;
    }
}
}
public class ClientFactory {
private final EventBus eventBus = new SimpleEventBus();
private final PlaceController placeController = new PlaceController(eventBus);
private final ExampleViewImpl example = new ExampleViewImpl();

public EventBus getEventBus() {
    return this.eventBus;
}

public PlaceController getPlaceController() {
    return this.placeController;
}

public ExampleViewImpl getExampleView() {
    return example;
}
}
public class ExampleActivity extends AbstractActivity {

private ExampleView view;
private ClientFactory factory;

public ExampleActivity(ExamplePlace place, ClientFactory factory) {
    // Get the factory reference
    this.factory = factory;

    // Get the reference to the view
    view = this.factory.getExampleView();

    // Select the tab corresponding to the token value
    if (place.getToken() != null) {
        // By default the first tab is selected
        if (place.getToken().equals("") || place.getToken().equals("1")) {
            view.selectTab(0);
        } else if (place.getToken().equals("2")) {
            view.selectTab(1);
        }
    }
}

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // Attach this view to the application container
    panel.setWidget(view);
}
}
/**
 * Just an very basic place
 */
public class ExamplePlace extends Place {
// The token corresponding to an action
private String token;

// This place should use a token to identify a view behavior
public ExamplePlace(String token) {
    this.token = token;
}

// Return the current token
public String getToken() {
    return token;
}

// Custom prefix to break the default name : ExamplePlace
// So that the history token will be thetabplace:token
// and not any more : ExamplePlace:token
@Prefix(value="thetabplace")
public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {
    @Override
    public String getToken(ExamplePlace place) {
        return place.getToken();
    }

    @Override
    public ExamplePlace getPlace(String token) {
        return new ExamplePlace(token);
    }
}
}
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;

public AppActivityMapper(ClientFactory clientFactory) {
    super();
    this.clientFactory = clientFactory;
}

@Override
public Activity getActivity(Place place) {
    if (place instanceof ExamplePlace) {
        return new ExampleActivity((ExamplePlace) place, clientFactory);
    }
    return null;
}
}
@WithTokenizers({ExamplePlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}
一起

private Place defaultPlace = new ExamplePlace("1");
private SimplePanel appWidget = new SimplePanel();

public void onModuleLoad() {
    ClientFactory clientFactory = new ClientFactory();
    EventBus eventBus = clientFactory.getEventBus();
    PlaceController placeController = clientFactory.getPlaceController();

    // Start ActivityManager for the main widget with our ActivityMapper
    ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
    ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
    activityManager.setDisplay(appWidget);

    // Start PlaceHistoryHandler with our PlaceHistoryMapper
    AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
    PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
    historyHandler.register(placeController, eventBus, defaultPlace);

    RootPanel.get().add(appWidget);
    // Goes to the place represented on URL else default place
    historyHandler.handleCurrentHistory();
}

很好的例子,正是我想要的。我在网上读了几篇帖子,让我相信你不需要通过活动来使用场所。ExampleViewImpl和ExampleActivity很好地填补了空白。您的示例很好地建立在google文章的基础上。感谢您花时间回复,非常感谢。