1

In Contacts project, if a user entered an invalid token for contact editing, how to should handle it, for example, instead of #contactId:12 , entered #contactId:abc. Or in another app, maybe for security reason (such as authorization), the user does not have required permission for editing an object, how to inform user. I tried to navigate to a place such as InvalidUrlPlace but can not. I can not navigate from inside of Activity.start() method , using placeController.goTo()

update1:

@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus)
{
    GWT.log("inside start() method");
    if (assertValidPlace())
    {
        containerWidget.setWidget(view);
        initialize();
    }
}

protected boolean assertValidPlace()
{
    if (!place.isValid())
    {
                GWT.log("going to InvalidUrlPlace");
                placeController.goTo(new InvalidUrlPlace());
                return false;
    }

    return true;
}

update2:

<code>
public class InvalidUrlPlace extends Place
{
    private Messages messages = GWT.create(Messages.class);
    private InvalidToken invalidToken;

    public InvalidUrlPlace()
    {
    }

    public InvalidUrlPlace(InvalidToken invalidToken)
    {
        this.invalidToken = invalidToken;
    }

    public InvalidToken getInvalidToken()
    {
        return invalidToken;
    }

    public String getMessage()
    {
        if (invalidToken != null)
            return invalidToken.getMessage();
        return messages.pageNotFound();
    }

    @Prefix("invalidUrl")
    public static class Tokenizer implements PlaceTokenizer<InvalidUrlPlace>
    {
        @Override
        public InvalidUrlPlace getPlace(String token)
        {
            return new InvalidUrlPlace();
        }

        @Override
        public String getToken(InvalidUrlPlace place)
        {
            return "";
        }
    }
}

Askar
  • 544
  • 1
  • 6
  • 17
  • You can navigate anywhere you like from Activity.start() using placeController.goTo(). Show your code. – Andrei Volgin Jan 02 '16 at 12:26
  • Thanks for your reply, I updated the message – Askar Jan 02 '16 at 13:09
  • **Solved** The problem answered [here](http://stackoverflow.com/questions/8003229/gwt-how-do-i-go-to-a-new-place-from-within-the-activity-start-method#answers-header) – Askar Jan 03 '16 at 07:13

1 Answers1

0

Try this:

@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus)
{
    if (place.isValid())
    {
        containerWidget.setWidget(view);
        initialize();
    }
    else
    {
        placeController.goTo(new InvalidUrlPlace());
    }
}

You are trying to execute code (return false) after you already told the activity to leave to a new place - i.e. finish this activity and start a new one.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • thanks, this code worked, but the url (token) not updated to InvalidUrlPlace token, and still has the old token. – Askar Jan 03 '16 at 04:44
  • Do you get any errors? It should be InvalidUrlPlace(""), by the way, unless you added a new constructor to `Place`. – Andrei Volgin Jan 03 '16 at 04:52
  • I use InvalidUrlPlace as a regular place, and it worked totally, but if navigate from start() method, this problem raise, also from the current activity, cannot navigate to the same activity with an other token – Askar Jan 03 '16 at 04:58
  • InvalidUrlPlace has two constructor, if i use default constructor, the result not changed, i update message and add the InvalidUrlPlace code, also if i use OtherPlace that work fine, instead of InvalidUrlPlace, i can not navigate that from inside of start() method correctly and the url does not update, thanks again – Askar Jan 03 '16 at 05:09
  • Place does not have a no-args constructor. You probably should do: `public InvalidUrlPlace() {super("");} Same with the other constructor that you use. – Andrei Volgin Jan 03 '16 at 05:13
  • com.google.gwt.place.shared.Place does not have any constructor – Askar Jan 03 '16 at 05:16
  • My bad - it's an abstract class. Do you see any errors (e.g. exceptions) in your browser tools? – Andrei Volgin Jan 03 '16 at 06:01
  • No, I use IntelliJ IDEA and chrome developer tools, I dont know what exactly is wrong here? – Askar Jan 03 '16 at 06:10