Monday, March 11, 2013

JSF 2.x Tip of the Day: What is <h:link /> and Why Do I Care?

One of the JSF Components that is new in JSF 2.1 is the <h:link /> component. This component represents an HTML <a /> (anchor) tag. In most cases, I would actually recommend using an <a /> tag over this component. The anchor tag can use Expression Language (EL) which is what most people are after. The <h:link /> is more overkill than most people need. However, there are a couple of cases where the <h:link /> really does shine.

  1. disabled="true" attribute renders a <span /> that can serve as a space holder.
  2. The rendered attribute can be programmatically controlled using EL, or via binding of the component. You don't need to use JS, or a JS Framework like jquery to hide it. It simply does not render.
  3. The component can be programatically created using a binding <h:link binding="#{}" />.
  4. Built-in support for JSF navigation using the outcome attribute.
  5. Supports passing JSF ViewParameters through the use of the includeviewParams attribute.

The real significant disadvantage is that you can't use it like a normal anchor tag and direct the href to another site without some tricks. In that case you are better off using a <h:commandLink/> or a plain <a/> tag.



Project Files

Here is the NetBeans 7.3 Maven Project: jsf-link-example