Bookmarkability via View Parameters feature
Asked Answered
P

1

11

Is bookmarkability achievable though using the includeViewParams=true query parameter with JSF implicit navigation ? If yes, then how ?

Podesta answered 6/11, 2012 at 11:16 Comment(0)
L
21

First we need to understand what exactly "bookmarkability" is and what exactly includeViewParams does. This way the effect of the combination of both can be better understood.

Bookmarkability concerns the HTTP request URL in its exact form as you see in the browser's address bar. It's exactly the URL as the enduser would store in its bookmarks and/or would copypaste as a link elsewhere, such as a forum, a chatbox, some social medium, or just a new browser window/tab, etc. When a link is followed or an URL is copypasted into browser's address bar, then by default a HTTP GET request will be fired. If the result is exactly the same everytime (leaving authorization/authentication and the time-sensitive nature of the page —search results, last news, etc— outside consideration), then we can talk about a bookmarkable URL. The technical term is "an idempotent HTTP request".

If the enduser has however submitted a POST form on that URL beforehand, which hasn't performed a redirect, then the URL is not necessarily bookmarkable. The submitted form data is not reflected in the URL. Copypasting the URL into a new browser window/tab may not necessarily yield exactly the same result as after the form submit. Such an URL is then not bookmarkable. POST is not idempotent. That's why page-to-page navigation by commandlinks is bad.

Bookmarkability is usually achieved by a specific construct of the URL path and/or query parameters. If you look at Google, the search results are bookmarkable thanks to the q query string parameter.

http://google.com/search?q=bookmarkability

In JSF terms, those request parameters can be set (and converted and validated) via <f:viewParam>:

<f:metadata>
    <f:viewParam name="q" value="#{bean.query}" />
    <f:viewAction action="#{bean.search}" />
</f:metadata>

If you need to perform for example pagination, and you'd like to keep the URL bookmarkable, then you could add another request parameter:

http://google.com/search?q=bookmarkability&start=10

with

<f:metadata>
    <f:viewParam name="q" value="#{bean.query}" />
    <f:viewParam name="start" value="#{bean.start}" />
    <f:viewAction action="#{bean.search}" />
</f:metadata>

The includeViewParams="true" basically includes all of those view parameters in the generated GET link. With help of this, the pagination links can then look like this without the need to repeat the q parameter:

<h:link value="1" outcome="search" includeViewParams="true">
    <f:param name="start" value="#{null}" />
</h:link>
<h:link value="2" outcome="search" includeViewParams="true">
    <f:param name="start" value="10" />
</h:link>
<h:link value="3" outcome="search" includeViewParams="true">
    <f:param name="start" value="20" />
</h:link>
...

(of course generated by some <ui:repeat> or so)

When entering the page with q=bookmarkability, this will produce the following links

/search.xhtml?q=bookmarkability
/search.xhtml?start=10&q=bookmarkability
/search.xhtml?start=20&q=bookmarkability

Those are bookmarkable URLs and the includeViewParams made creating them more convenient.

Landman answered 6/11, 2012 at 12:12 Comment(2)
" <f:viewParam name="q" value="#{bean.query}" /> needs to be in from-viewId or to-viewId ?Podesta
It just needs to be placed in the top level view (the XHTML file) which is opened by the GET request. There is no means of any navigation case/rule in the answer as your comment seems to imply.Landman

© 2022 - 2024 — McMap. All rights reserved.