【问题标题】:how to get the last history token in GWT如何获取 GWT 中的最后一个历史令牌
【发布时间】:2014-10-09 16:54:33
【问题描述】:

我想获取我导航的前一个标记。

例如,在这个页面中:

http://www.example.com/com.example.gwt.HistoryExample/HistoryExample.html#page1

我被重定向到这个新页面:

http://www.example.com/com.example.gwt.HistoryExample/HistoryExample.html#page2

现在,如果我想要当前的令牌,我可以通过这种方式轻松获得:

String currentPlace = placeManager.getCurrentPlaceRequest().getNameToken();

currentPlace 将收到“page2”,但我不知道如何获取上一个令牌“page1”

【问题讨论】:

    标签: java gwt token history


    【解决方案1】:

    您可以将此代码添加到您的 onModuleLoad() 方法中,或者添加到在应用程序开始时创建的类中,当然之后不会被销毁

    private Place previousPlace;
    private Place currentPlace;
    
    eventBus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() {
    
          public void onPlaceChange(PlaceChangeEvent event) {
    
              previousPlace = currentPlace;
              currentPlace = event.getNewPlace();
          }
      });
    

    【讨论】:

      【解决方案2】:

      我不确定为什么您的应用程序问题 page1 会重定向到 page2,但在这里。我扩展了位置控制器,以便我可以执行 goBackOr(Place place)。这将允许您获得以前的位置。您可以调用 getPreviousPlace()。当您有一个编辑活动并且您想返回到用户来自的上一个位置时,这可能是一种处理位置导航的有用方法。至少出于测试目的,您应该能够用这个地方控制器替换而不需要更改。

      public class PlaceController extends com.google.gwt.place.shared.PlaceController {
      
          private static final Logger log = Logger.getLogger(PlaceController.class.getName());
      
          private final EventBus eventBus;
      
          private final Delegate delegate;
      
          private Place where = Place.NOWHERE;
      
          /**
           * Previous place from history.
           */
          private Place previousPlace = null;
      
          /**
           * Legacy method tied to the old location for {@link EventBus}.
           * 
           * @deprecated use {@link #PlaceController(EventBus)}
           */
          @Deprecated
          public PlaceController(com.google.gwt.event.shared.EventBus eventBus) {
              this((EventBus) eventBus);
          }
      
          /**
           * Legacy method tied to the old location for {@link EventBus}.
           * 
           * @deprecated use {@link #PlaceController(EventBus, Delegate)}
           */
          @Deprecated
          public PlaceController(com.google.gwt.event.shared.EventBus eventBus, Delegate delegate) {
              this((EventBus) eventBus, delegate);
          }
      
          /**
           * Create a new PlaceController with a {@link DefaultDelegate}. The
           * DefaultDelegate is created via a call to GWT.create(), so an alternative
           * default implementation can be provided through <replace-with> rules
           * in a {@code .gwt.xml} file.
           * 
           * @param eventBus
           *            the {@link EventBus}
           */
          public PlaceController(EventBus eventBus) {
              this(eventBus, (Delegate) GWT.create(DefaultDelegate.class));
          }
      
          /**
           * Create a new PlaceController.
           * 
           * @param eventBus
           *            the {@link EventBus}
           * @param delegate
           *            the {@link Delegate} in charge of Window-related events
           */
          public PlaceController(EventBus eventBus, Delegate delegate) {
              super(eventBus, delegate);
              this.eventBus = eventBus;
              this.delegate = delegate;
              delegate.addWindowClosingHandler(new ClosingHandler() {
                  public void onWindowClosing(ClosingEvent event) {
                      String warning = maybeGoTo(Place.NOWHERE);
                      if (warning != null) {
                          event.setMessage(warning);
                      }
                  }
              });
          }
      
          /**
           * Get the previous place, if null then this is the first place.
           * 
           * @return a {@link Place} instance
           */
          public Place getPreviousPlace() {
              return previousPlace;
          }
      
          /**
           * Returns the current place.
           * 
           * @return a {@link Place} instance
           */
          public Place getWhere() {
              return where;
          }
      
          /**
           * Request a change to a new place. It is not a given that we'll actually
           * get there. First a {@link PlaceChangeRequestEvent} will be posted to the
           * event bus. If any receivers post a warning message to that event, it will
           * be presented to the user via {@link Delegate#confirm(String)} (which is
           * typically a call to {@link Window#confirm(String)}). If she cancels, the
           * current location will not change. Otherwise, the location changes and a
           * {@link PlaceChangeEvent} is posted announcing the new place.
           * 
           * @param newPlace
           *            a {@link Place} instance
           *            
           */
          public void goTo(Place newPlace) {
              log().fine("goTo: " + newPlace);
      
              // if (getWhere().equals(newPlace)) {
              // log().fine("Asked to return to the same place: " + newPlace);
              // return;
              // }
      
              String warning = maybeGoTo(newPlace);
              if (warning == null || delegate.confirm(warning)) {
                  previousPlace = where;
                  where = newPlace;
                  eventBus.fireEvent(new PlaceChangeEvent(newPlace));
              }
          }
      
          /**
           * This is useful for going back unless there is,
           * not a previous place, commonly when a user refreshes
           * the screen and then tries to click back.
           * @param place Place to go if there is not a previous place set.
           */
          public void goBackOr(Place place) {
              Place previousPlace = getPreviousPlace();
      
              if (previousPlace != null && previousPlace != Place.NOWHERE) {
                  goTo(previousPlace);
              } else if (place != null) {
                  goTo(place);
              } else {
                  throw new RuntimeException("Previous place was null and the optional place was not specified");
              }
      
          }
      
          /**
           * Visible for testing.
           */
          private Logger log() {
              return log;
          }
      
          private String maybeGoTo(Place newPlace) {
              PlaceChangeRequestEvent willChange = new PlaceChangeRequestEvent(newPlace);
              eventBus.fireEvent(willChange);
              String warning = willChange.getWarning();
              return warning;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-16
        • 2021-06-15
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多