通过查看参数功能的书签能力 [英] Bookmarkability via View Parameters feature

查看:20
本文介绍了通过查看参数功能的书签能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 JSF 隐式导航使用 includeViewParams=true 查询参数是否可以实现书签功能?如果是,那么如何?

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

推荐答案

首先我们需要了解书签性"究竟是什么,以及 includeViewParams 究竟是做什么的.这样可以更好地理解两者结合的效果.

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.

书签性与您在浏览器地址栏中看到的完全一致的 HTTP 请求 URL 相关.这正是最终用户将存储在其书签中和/或将作为链接复制粘贴到其他地方的 URL,例如论坛、聊天框、某些社交媒体或只是一个新的浏览器窗口/选项卡等.或者将 URL 复制粘贴到浏览器的地址栏中,然后默认情况下将触发 HTTP GET 请求.如果结果每次都完全相同(离开授权/身份验证和页面的时间敏感特性——搜索结果、最新消息等——外部考虑),那么我们可以谈论书签网址.技术术语是一个 幂等 HTTP 请求".

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".

如果最终用户事先在该 URL 上提交了一个 POST 表单,但没有执行重定向,则该 URL 不一定可以加入书签.提交的表单数据不会反映在 URL 中.将 URL 复制粘贴到新的浏览器窗口/选项卡中可能不一定会产生与表单提交后完全相同的结果.这样的 URL 是不可添加书签的.POST 不是幂等的.这就是通过命令链接进行页面到页面导航很糟糕的原因.

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.

书签性通常是通过 URL 路径和/或查询参数的特定构造来实现的.如果您查看 Google,由于 q 查询字符串参数,搜索结果可以添加书签.

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

在 JSF 术语中,可以通过 :

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>

如果您需要执行例如分页,并且您希望将 URL 设为书签,那么您可以添加另一个请求参数:

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

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

includeViewParams="true" 在生成的 GET 链接中基本上包含所有这些视图参数.有了这个帮助,分页链接可以看起来像这样,而无需重复 q 参数:

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)

当进入带有q=bookmarkability的页面时,会产生以下链接

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

那些是可添加书签的 URL,includeViewParams 使创建它们变得更加方便.

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

这篇关于通过查看参数功能的书签能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆