Using parameters with Views

When the Navigator API is in use, one can pass “parameters” to Views in the URI fragment.

The remainder of the fragment that is left after the (longest) view name matched is removed, is considered to be “fragment parameters”. These are passed to the View in question, which can then handle the parameter(s). Basically: #!viewname/parameters.

Continuing from the basic navigation example, let’s make a View that displays a message passed as a fragment parameter:

Java

  1. import com.vaadin.navigator.View;
  2. import com.vaadin.ui.Label;
  3. import com.vaadin.ui.Panel;
  4. public class MessageView extends Panel implements View {
  5. public static final String NAME = "message";
  6. public MessageView() {
  7. super(new VerticalLayout());
  8. setCaption("Messages");
  9. }
  10. @Override
  11. public void enter(ViewChangeEvent event) {
  12. if(event.getParameters() != null){
  13. // split at "/", add each part as a label
  14. String[] msgs = event.getParameters().split("/");
  15. for (String msg : msgs) {
  16. ((Layout)getContent()).addComponent(new Label(msg));
  17. }
  18. }
  19. }
  20. }

Let’s register MessageView along with the other Views:

Java

  1. import com.vaadin.navigator.Navigator;
  2. import com.vaadin.navigator.Navigator.SimpleViewDisplay;
  3. import com.vaadin.server.Page;
  4. import com.vaadin.server.WrappedRequest;
  5. import com.vaadin.ui.UI;
  6. public class NavigationtestUI extends UI {
  7. @Override
  8. public void init(VaadinRequest request) {
  9. // Create Navigator, make it control the ViewDisplay
  10. Navigator navigator = new Navigator(this, this);
  11. // Add some Views
  12. navigator.addView(MainView.NAME, new MainView()); // no fragment
  13. // #!count will be a new instance each time we navigate to it, counts:
  14. navigator.addView(CountView.NAME, CountView.class);
  15. // #!message adds a label with whatever it receives as a parameter
  16. navigator.addView(MessageView.NAME, new MessageView());
  17. }
  18. }

Finally, we’ll add two labels to the MainView so we don’t have to type in the browsers address-bar to try it out:

Java

  1. import com.vaadin.navigator.View;
  2. import com.vaadin.server.ExternalResource;
  3. import com.vaadin.ui.Link;
  4. import com.vaadin.ui.Panel;
  5. public class MainView extends Panel implements View {
  6. public static final String NAME = "";
  7. public MainView() {
  8. VerticalLayout layout = new VerticalLayout();
  9. Link lnk = new Link("Count", new ExternalResource("#!" + CountView.NAME));
  10. layout.addComponent(lnk);
  11. lnk = new Link("Message: Hello", new ExternalResource("#!"
  12. + MessageView.NAME + "/Hello"));
  13. layout.addComponent(lnk);
  14. lnk = new Link("Message: Bye", new ExternalResource("#!"
  15. + MessageView.NAME + "/Bye/Goodbye"));
  16. layout.addComponent(lnk);
  17. setContent(layout);
  18. }
  19. @Override
  20. public void enter(ViewChangeEvent event) {
  21. }
  22. }

Simple! Let’s just conclude by noting that it’s usually a good idea to make sure the parameters are URI encoded, or the browser might disapprove.