我什么时候应该使用 h:outputLink 而不是 h:commandLink? [英] When should I use h:outputLink instead of h:commandLink?

查看:26
本文介绍了我什么时候应该使用 h:outputLink 而不是 h:commandLink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什么时候应该使用 而不是 ?

我知道 commandLink 会生成一个 HTTP 帖子;我猜 outputLink 会生成 HTTP 获取.也就是说,我读过的大部分 渲染一个完整的 HTML <a> 元素,在 中带有正确的 URL>href 属性,它触发可添加书签的 GET 请求.它不能直接调用托管 bean 操作方法.

链接文本


<h:commandLink> 使用 onclick 脚本呈现 HTML 元素,该脚本提交(隐藏的)POST 表单并可以调用托管 bean 操作方法.它还需要放置在 中.

<h:commandLink 值=链接文本"动作=目的地"/></h:form>

<h:commandLink> 上的 ?faces-redirect=true 参数,它在 POST 之后触发重定向(根据 Post-Redirect-Get 模式),仅在实际点击链接时提高目标页面的书签性(URL 将不再是一个落后"),但它不会将 元素的 href 更改为完整的 URL.它仍然是#.

<h:commandLink 值=链接文本"动作=目的地?faces-redirect=true"/></h:form>


从 JSF 2.0 开始,还有 可以采用视图 ID(导航案例结果)而不是 URL.它将生成一个 HTML <a> 元素以及 href 中的正确 URL.


因此,如果它是用于像 SO 用户名链接这样的可添加书签的纯页面到页面导航,则使用 .这对 SEO 也更好,因为机器人通常不会加密 POST 表单或 JS 代码.此外,用户体验将得到改进,因为页面现在可以添加书签并且 URL 不再落后".没有了.

必要时,您可以在 @RequestScoped@ViewScoped @ 的构造函数或 @PostConstruct 中进行预处理工作ManagedBean 附加到相关目标页面.您可以使用 @ManagedProperty 将 GET 参数设置为 bean 属性.

另见:

When should I use an <h:outputLink> instead of an <h:commandLink>?

I understand that a commandLink generates an HTTP post; I'm guessing that outputLink will generate HTTP gets. That said, most of the JSF tutorial material I've read uses commandLink (almost?) exclusively.

Context: I am implementing a wee little demo project that shows a header link to a user page, much like Stack Overflow's...

...and I am not sure if commandLink (perhaps using ?faces-redirect=true for bookmarkability) or outputLink is the right choice.

解决方案

The <h:outputLink> renders a fullworthy HTML <a> element with the proper URL in the href attribute which fires a bookmarkable GET request. It cannot directly invoke a managed bean action method.

<h:outputLink value="destination.xhtml">link text</h:outputLink>


The <h:commandLink> renders a HTML <a> element with an onclick script which submits a (hidden) POST form and can invoke a managed bean action method. It's also required to be placed inside a <h:form>.

<h:form>
    <h:commandLink value="link text" action="destination" />
</h:form>

The ?faces-redirect=true parameter on the <h:commandLink>, which triggers a redirect after the POST (as per the Post-Redirect-Get pattern), only improves bookmarkability of the target page when the link is actually clicked (the URL won't be "one behind" anymore), but it doesn't change the href of the <a> element to be a fullworthy URL. It still remains #.

<h:form>
    <h:commandLink value="link text" action="destination?faces-redirect=true" />
</h:form>


Since JSF 2.0, there's also the <h:link> which can take a view ID (a navigation case outcome) instead of an URL. It will generate a HTML <a> element as well with the proper URL in href.

<h:link value="link text" outcome="destination" />


So, if it's for pure and bookmarkable page-to-page navigation like the SO username link, then use <h:outputLink> or <h:link>. That's also better for SEO since bots usually doesn't cipher POST forms nor JS code. Also, UX will be improved as the pages are now bookmarkable and the URL is not "one behind" anymore.

When necessary, you can do the preprocessing job in the constructor or @PostConstruct of a @RequestScoped or @ViewScoped @ManagedBean which is attached to the destination page in question. You can make use of @ManagedProperty or <f:viewParam> to set GET parameters as bean properties.

See also:

这篇关于我什么时候应该使用 h:outputLink 而不是 h:commandLink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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