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.

UsingURIFragments.asciidoc 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ---
  2. title: Using URI Fragments
  3. order: 26
  4. layout: page
  5. ---
  6. [[using-uri-fragments]]
  7. = Using URI fragments
  8. [[reading-fragment-when-initializing-ui]]
  9. Reading Fragment when Initializing UI
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. URI fragments can either be used for initializing the UI and/or read or
  12. modified in any listener. In the UI.init method you get a "VaadinRequest
  13. request" parameter. The UI's Page contains a information about the HTTP
  14. request used to initialize the application and you can get the URI
  15. fragment using
  16. ....
  17. getPage().getUriFragment()
  18. ....
  19. A simple init that depends on the URI fragment is thus:
  20. [source,java]
  21. ....
  22. public class MyUI extends UI {
  23. @Override
  24. protected void init(VaadinRequest request) {
  25. layout = new VerticalLayout();
  26. layout.setMargin(true);
  27. setContent(layout);
  28. Label label = new Label("Hello, your fragment is "
  29. + getPage().getUriFragment());
  30. layout.addComponent(label);
  31. }
  32. }
  33. ....
  34. [[reading-fragment-changes]]
  35. Reading Fragment Changes
  36. ~~~~~~~~~~~~~~~~~~~~~~~~
  37. The URI fragment can be changed also when the application is running,
  38. either manually in the location bar in browser or from user code. These
  39. changes can be caught with a **FragmentChangedListener**. Notice,
  40. however, that there is no event fired for the initial URL fragment. The
  41. easiest way to handle both cases in the same way is to call the same
  42. method from the FragmentChangedListener and the init method:
  43. [source,java]
  44. ....
  45. public class MyUI extends UI {
  46. // ...
  47. // React to fragment changes
  48. getPage().addUriFragmentChangedListener(new UriFragmentChangedListener() {
  49. @Override
  50. public void uriFragmentChanged(UriFragmentChangedEvent source) {
  51. handleFragment(source.getUriFragment());
  52. }
  53. });
  54. // Handle the fragment received in the initial request
  55. handleFragment(getPage().getUriFragment());
  56. addComponent(new Button("Show and set fragment", new Button.ClickListener() {
  57. @Override
  58. public void buttonClick(ClickEvent event) {
  59. handleFragment(getPage().getUriFragment());
  60. getPage().setUriFragment("customFragment");
  61. }
  62. }));
  63. ....
  64. [[reading-and-writing-the-fragment]]
  65. Reading and Writing the Fragment
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. To later on read the fragment you can use
  68. ....
  69. Page.getCurrent().getUriFragment();
  70. ....
  71. and to set the fragment
  72. ....
  73. Page.getCurrent().setUriFragment(String fragment);
  74. ....