gpt4 book ai didi

GWT 2.1 没有事件的地方示例

转载 作者:行者123 更新时间:2023-12-02 01:49:16 24 4
gpt4 key购买 nike

有没有人有任何关于如何使用地点而不使用历史管理事件的示例。我快速敲击了一些东西,可以看到网址随着浏览器后退和浏览器前进的点击而变化,但显示却没有去任何地方。

我正在使用 DecoratedTabPanel 并有一个触发 getPlaceController().goTo(place) 的 SelectionHandler。

任何想法都会有用。

最佳答案

这是我编写的一段简单代码,用于演示您的期望。它基于 GWT 和 MVP 开发文档 ( GWT and MVP )

在此示例中,您将在两个选项卡之间导航。选择后,将创建一个新的历史记录项(没有任何事件)。只要您使用浏览器按钮后退/前进,页面就会正确更新。

我定义了一个地点、一项事件及其 View 。我已经根据我的需要调整了 AppActivityMapper、AppActivityManager 和 ClientFactory。代码是轻量级的,不需要注释就可以理解。我只是在需要时做了一些解释,但如果不清楚,请随时询问。

ExampleView.java

public interface ExampleView extends IsWidget {
void selectTab(int index);
}

ExampleViewImpl.java

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;
}
}
}

ClientFactory.java

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;
}
}

ExampleActivity.java

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);
}
}

ExamplePlace.java

/**
* 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);
}
}
}

AppActivityMapper.java

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;
}
}

AppPlaceHistoryMapper.java

@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();
}

关于GWT 2.1 没有事件的地方示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com