You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsingParametersWithViews.asciidoc 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ---
  2. title: Using Parameters With Views
  3. order: 14
  4. layout: page
  5. ---
  6. [[using-parameters-with-views]]
  7. = Using parameters with Views
  8. When the Navigator API is in use, one can pass "parameters" to Views in
  9. the URI fragment.
  10. The remainder of the fragment that is left after the (longest) view name
  11. matched is removed, is considered to be "fragment parameters". These are
  12. passed to the View in question, which can then handle the parameter(s).
  13. Basically: `#viewname/parameters`.
  14. Continuing from the basic navigation example, let's make a View that
  15. displays a message passed as a fragment parameter:
  16. [source,java]
  17. ....
  18. import com.vaadin.navigator.View;
  19. import com.vaadin.ui.Label;
  20. import com.vaadin.ui.Panel;
  21. public class MessageView extends Panel implements View {
  22. public static final String NAME = "message";
  23. public MessageView() {
  24. super(new VerticalLayout());
  25. setCaption("Messages");
  26. }
  27. @Override
  28. public void enter(ViewChangeEvent event) {
  29. if(event.getParameters() != null){
  30. // split at "/", add each part as a label
  31. String[] msgs = event.getParameters().split("/");
  32. for (String msg : msgs) {
  33. ((Layout)getContent()).addComponent(new Label(msg));
  34. }
  35. }
  36. }
  37. }
  38. ....
  39. Let's register `MessageView` along with the other Views:
  40. [source,java]
  41. ....
  42. import com.vaadin.navigator.Navigator;
  43. import com.vaadin.navigator.Navigator.SimpleViewDisplay;
  44. import com.vaadin.server.Page;
  45. import com.vaadin.server.WrappedRequest;
  46. import com.vaadin.ui.UI;
  47. public class NavigationtestUI extends UI {
  48. @Override
  49. public void init(VaadinRequest request) {
  50. // Create Navigator, make it control the ViewDisplay
  51. Navigator navigator = new Navigator(this, this);
  52. // Add some Views
  53. navigator.addView(MainView.NAME, new MainView()); // no fragment
  54. // #count will be a new instance each time we navigate to it, counts:
  55. navigator.addView(CountView.NAME, CountView.class);
  56. // #message adds a label with whatever it receives as a parameter
  57. navigator.addView(MessageView.NAME, new MessageView());
  58. }
  59. }
  60. ....
  61. Finally, we'll add two labels to the MainView so we don't have to type
  62. in the browsers address-bar to try it out:
  63. [source,java]
  64. ....
  65. import com.vaadin.navigator.View;
  66. import com.vaadin.server.ExternalResource;
  67. import com.vaadin.ui.Link;
  68. import com.vaadin.ui.Panel;
  69. public class MainView extends Panel implements View {
  70. public static final String NAME = "";
  71. public MainView() {
  72. VerticalLayout layout = new VerticalLayout();
  73. Link lnk = new Link("Count", new ExternalResource("#!" + CountView.NAME));
  74. layout.addComponent(lnk);
  75. lnk = new Link("Message: Hello", new ExternalResource("#!"
  76. + MessageView.NAME + "/Hello"));
  77. layout.addComponent(lnk);
  78. lnk = new Link("Message: Bye", new ExternalResource("#!"
  79. + MessageView.NAME + "/Bye/Goodbye"));
  80. layout.addComponent(lnk);
  81. setContent(layout);
  82. }
  83. @Override
  84. public void enter(ViewChangeEvent event) {
  85. }
  86. }
  87. ....
  88. Simple! Let's just conclude by noting that it's usually a good idea to
  89. make sure the parameters are URI encoded, or the browser might
  90. disapprove.