GWT Anchor to Place?
In a GWT 2.1+ app, how can I g开发者_StackOverflow中文版enerate a link to a place for external consumption?
For instance, say I want to create a link to Place1. For internal consumption I could do presenter.goTo(new Place1("token"))
. How can I make this into an Anchor
or some sort of link that users can paste into their browser?
Here's how I would do:
final Place1 place = new Place1("token");
Anchor anchor = new Anchor("go to place 1", "#" + placeHistoryMapper.getToken(place));
anchor.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
placeController.goTo(place);
event.preventDefault();
}
});
As far as I know as i am new to GWT myself, if you use Hyperlink instead of Anchor you will not have to write the event handler. It will redirect you to the place and handle the history stuff automatically.
You can convert a place to a token string using the PlaceHistoryMapper. See https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces for details on how to implement the MVP design in GWT.
final YourImplementationOfPlaceHistoryMapper placeHistoryMapper = GWT.create(YourImplementationOfPlaceHistoryMapper.class);
final Hyperlink link = new Hyperlink("A Link To A Place", placeHistoryMapper.getToken(new YourNewPlace()));
If you have already mapped the token to a place, simply create an anchor with the href property equals to the token.
Anchor anchor = new Anchor("go to place1 ", "token");
精彩评论