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.3KB

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