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

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

问题描述

何时应使用<h:outputLink>代替<h:commandLink>?

我了解到commandLink会生成HTTP帖子;我猜想outputLink会生成HTTP获取.就是说,我阅读的大多数 JSF教程材料都使用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.

上下文:我正在实施一个小的演示项目,该项目显示了指向用户页面的标题链接,就像Stack Overflow的...

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

...而且我不确定commandLink(也许使用?faces-redirect=true作为书签功能)还是outputLink是正确的选择.

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


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


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>

<h:commandLink>上的?faces-redirect=true参数,它在POST之后触发重定向(按照<a>元素的href是完整的URL.它仍然保留为#.

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>


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


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用户名链接),请使用<h:outputLink><h:link>.这对于SEO也更好,因为机器人通常不加密POST形式或JS代码.另外,由于页面现在可以添加书签并且URL不再落后",因此UX将会得到改善.


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.

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

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.

  • ViewParam vs @ManagedProperty(value = "#{param.id}")
  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
  • Bookmarkability via View Parameters feature
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)

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

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