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.

advanced-urifu.asciidoc 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ---
  2. title: Managing URI Fragments
  3. order: 11
  4. layout: page
  5. ---
  6. [[advanced.urifu]]
  7. = Managing URI Fragments
  8. NOTE: This chapter contains instructions how to manage URI fragments. As browser support for HTML5 History API has improved, developers should in most cases developers instead use real URIs with _pushState_ method. Read more from
  9. <<advanced-navigator#advanced.pushstate,"Manipulating Browser History">>.
  10. A major issue in AJAX applications is that as they run in a single web page,
  11. bookmarking the application URL (or more generally the __URI__) can only
  12. bookmark the application, not an application state. This is a problem for many
  13. applications, such as product catalogs and discussion forums, in which it would
  14. be good to provide links to specific products or messages. Consequently, as
  15. browsers remember the browsing history by URI, the history and the
  16. [guibutton]#Back# button do not normally work. The solution before HTML5 API was available was to use the
  17. __fragment identifier__ part of the URI, which is separated from the primary
  18. part (address + path + optional query parameters) of the URI with the hash (#)
  19. character. For example:
  20. ----
  21. http://example.com/path#myfragment
  22. ----
  23. The exact syntax of the fragment identifier part is defined in RFC 3986
  24. (Internet standard STD 66) that defines the URI syntax. A fragment may only
  25. contain the regular URI __path characters__ (see the standard) and additionally
  26. the slash and the question mark.
  27. Vaadin offers two ways to enable the use of URI fragments: the high-level
  28. [classname]#Navigator# utility described in
  29. <<advanced-navigator#advanced.navigator,"Navigating
  30. in an Application">> (if the legacy [classname]#UriFragmentManager# is configured for the Navigator) and the low-level API described here.
  31. [[advanced.urifu.setting]]
  32. == Setting the URI Fragment
  33. You can set the current fragment identifier with the
  34. [methodname]#setUriFragment()# method in the [classname]#Page# object.
  35. [source, java]
  36. ----
  37. Page.getCurrent().setUriFragment("mars");
  38. ----
  39. Setting the URI fragment causes an [interfacename]#UriFragmentChangeEvent#,
  40. which is processed in the same server request. As with UI rendering, the URI
  41. fragment is changed in the browser after the currently processed server request
  42. returns the response.
  43. Prefixing the fragment identifier with an exclamation mark enables the web
  44. crawler support described in <<advanced.urifu.crawling>>.
  45. [[advanced.urifu.reading]]
  46. == Reading the URI Fragment
  47. The current URI fragment can be acquired with the [methodname]#getUriFragment()#
  48. method from the current [classname]#Page# object. The fragment is known when the
  49. [methodname]#init()# method of the UI is called.
  50. [source, java]
  51. ----
  52. // Read initial URI fragment to create UI content
  53. String fragment = getPage().getUriFragment();
  54. enter(fragment);
  55. ----
  56. To enable reusing the same code when the URI fragment is changed, as described
  57. next, it is usually best to build the relevant part of the UI in a separate
  58. method. In the above example, we called an [methodname]#enter()# method, in a
  59. way that is similar to handling view changes with [classname]#Navigator#.
  60. [[advanced.urifu.listening]]
  61. == Listening for URI Fragment Changes
  62. After the UI has been initialized, changes in the URI fragment can be handled
  63. with a [interfacename]#UriFragmentChangeListener#. The listeners are called when
  64. the URI fragment changes, but not when the UI is initialized, where the current
  65. fragment is available from the page object as described earlier.
  66. For example, we could define the listener as follows in the [methodname]#init()#
  67. method of a UI class:
  68. [source, java]
  69. ----
  70. public class MyUI extends UI {
  71. @Override
  72. protected void init(VaadinRequest request) {
  73. getPage().addUriFragmentChangedListener(
  74. new UriFragmentChangedListener() {
  75. public void uriFragmentChanged(
  76. UriFragmentChangedEvent source) {
  77. enter(source.getUriFragment());
  78. }
  79. });
  80. // Read the initial URI fragment
  81. enter(getPage().getUriFragment());
  82. }
  83. void enter(String fragment) {
  84. ... initialize the UI ...
  85. }
  86. }
  87. ----
  88. <<figure.advanced.urifu>> shows an application that allows specifying the menu
  89. selection with a URI fragment and correspondingly sets the fragment when the
  90. user selects a menu item.
  91. [[figure.advanced.urifu]]
  92. .Application State Management with URI Fragment Utility
  93. image::img/urifu-1.png[]
  94. [[advanced.urifu.crawling]]
  95. == Supporting Web Crawling
  96. Stateful AJAX applications can not normally be crawled by a search engine, as
  97. they run in a single page and a crawler can not navigate the states even if URI
  98. fragments are enabled. The Google search engine and crawler
  99. link:http://googlewebmastercentral.blogspot.fi/2009/10/proposal-for-making-ajax-crawlable.html[support
  100. a convention] where the fragment identifiers are prefixed with exclamation mark,
  101. such as [literal]#++#!myfragment++#. The servlet needs to have a separate
  102. searchable content page accessible with the same URL, but with a
  103. [literal]#++_escaped_fragment_++# parameter. For example, for
  104. [literal]#++/myapp/myui#!myfragment++# it would be
  105. [literal]#++/myapp/myui?_escaped_fragment_=myfragment++#.
  106. You can provide the crawl content by overriding the [methodname]#service()#
  107. method in a custom servlet class. For regular requests, you should call the
  108. super implementation in the [classname]#VaadinServlet# class.
  109. [source, java]
  110. ----
  111. public class MyCustomServlet extends VaadinServlet
  112. @Override
  113. protected void service(HttpServletRequest request,
  114. HttpServletResponse response)
  115. throws ServletException, IOException {
  116. String fragment = request
  117. .getParameter("_escaped_fragment_");
  118. if (fragment != null) {
  119. response.setContentType("text/html");
  120. Writer writer = response.getWriter();
  121. writer.append("<html><body>"+
  122. "<p>Here is some crawlable "+
  123. "content about " + fragment + "</p>");
  124. // A list of all crawlable pages
  125. String items[] = {"mercury", "venus",
  126. "earth", "mars"};
  127. writer.append("<p>Index of all content:</p><ul>");
  128. for (String item: items) {
  129. String url = request.getContextPath() +
  130. request.getServletPath() +
  131. request.getPathInfo() + "#!" + item;
  132. writer.append("<li><a href='" + url + "'>" +
  133. item + "</a></li>");
  134. }
  135. writer.append("</ul></body>");
  136. } else
  137. super.service(request, response);
  138. }
  139. }
  140. ----
  141. The crawlable content does not need to be human readable. It can provide an
  142. index of links to other application states, as we did in the example above. The
  143. links should use the " [literal]#++#!++#" notation, but can not be relative to
  144. avoid having the [literal]#++_escaped_fragment_++# parameter.
  145. You need to use the custom servlet class in the [filename]#web.xml# deployment
  146. descriptor instead of the normal [classname]#VaadinServlet# class, as described
  147. in
  148. <<../application/application-environment#application.environment.web-xml,"Using
  149. a web.xml Deployment Descriptor">>.