JSF 2 link, commandLink and outputLink example
In JSF, <h:link />, <h:commandLink /> and <h:outputLink /> tags are used to render a HTML “a” anchor element, see below examples to understand the different among them.
In below examples, assume “/JavaServerFaces/” is the root of your project context URL.
1. JSF h:link example
The “h:link” tag is a new tag in JSF 2.0, the “value” attribute is rendered as the anchor text, “outcome” attribute is determined the target URL of the HTML “href” attribute. See examples :
1. link + “outcome”
//JSF <h:link value="Login page" outcome="login" /> //HTML output <a href="/JavaServerFaces/faces/login.xhtml">Login page</a>
2. link + “outcome” + parammeter
//JSF <h:link value="Login page + Param " outcome="login" > <f:param name="username" value="mkyong" /> </h:link> //HTML output <a href="/JavaServerFaces/faces/login.xhtml?username=mkyong">Login page + Param</a>
3. link + “outcome” + image
//JSF <h:link outcome="login" > <h:graphicImage library="images" name="sofa.png" /> </h:link> //HTML output <a href="/JavaServerFaces/faces/login.xhtml"> <img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?ln=images" /> </a>
2. JSF h:commandLink example
The “h:commandLink” tag is released since JSF 1.x, which is generate a link act like a submit button when clicked. The “value” attribute is rendered as the anchor text, “action” attribute is determined the target URL of the HTML “href” attribute. In addition, the “h:commandLink” will include a “jsf.js” file in the page and attached an “onclick” event to the generated link, see examples :
The “j_idtx” is a random value generated by JSF.
1. commandLink
//JSF <h:commandLink value="Login page" /> //HTML output <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development"> </script> <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt6'), {'j_idt6:j_idt16':'j_idt6:j_idt16'},''); return false"> Login page </a>
P.S if the “action” attribute is omitted, it will reload current page while the button is clicked.
2. commandLink + action
//JSF <h:commandLink action="#{user.goLoginPage}" value="Login page" /> //HTML output <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development"> </script> <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt6'), {'j_idt6:j_idt18':'j_idt6:j_idt18'},''); return false"> Login page </a>
P.S You can’t even find the action value in the HTML output, only JSF will know where it goes.
3. commandLink + action + parameter
//JSF <h:commandLink action="#{user.goLoginPage}" value="Login page + Param "> <f:param name="username" value="mkyong" /> </h:commandLink> //HTML output <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development"> </script> <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt6'), {'j_idt6:j_idt20':'j_idt6:j_idt20','username':'mkyong'},''); return false"> Login page + Param </a>
4. commandLink + action + image
//JSF <h:commandLink action="#{user.goLoginPage}"> <h:graphicImage library="images" name="sofa.png" /> </h:commandLink> //HTML output <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development"> </script> <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt6'), {'j_idt6:j_idt23':'j_idt6:j_idt23'},''); return false"> <img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?ln=images" /> </a>
3. JSF h:outputLink example
The “h:outputLink” tag is released in JSF 1.x, the body of the tag is rendered as the anchor text, “value” attribute is rendered as the value of the HTML “href” attribute directly, see examples :
1. outputLink
//JSF <h:outputLink>Login page</h:outputLink> //HTML output <a href="currentpage.xhtml">Login page</a>
P.S if the “value” attribute is omitted, it will put the current page URL as the value of the “href” attribute.
2. outputLink + “value”
//JSF <h:outputLink value="login.xhtml" > Login page </h:outputLink> //HTML output <a href="login.xhtml"> Login page </a>
3. outputLink + “value” + outputText + parameter
//JSF <h:outputLink value="login.xhtml"> <h:outputText value="Login page" /> <f:param name="username" value="mkyong" /> </h:outputLink> //HTML output <a href="login.xhtml?username=mkyong">Login page</a>
4. outputLink + “value’ + outputText + image
//JSF <h:outputLink value="login.xhtml"> <h:graphicImage library="images" name="sofa.png" /> </h:outputLink> //HTML output <a href="login.xhtml"> <img src="/JavaServerFaces/faces/javax.faces.resource/sofa.png?ln=images" /> </a>
My thought…
Some review of above three link tags :
- The “h:link” tag is useful to generate a link which requires to interact with the JSF “outcome” , but lack of “action” support make it hard to generate a dynamic outcome.
- The “h:commandLink” tag is suck, the generated JavaScript is really scary! Not recommend to use this tag, unless you have a solid reason to support. But it supports the “action” attribute, which is what “h:link” lack of.
- The “h:outputLink” is useful to generate a link which does not require to interact with the JSF program itself.
At last, it will be perfect if the “action” attribute is added into the “h:link“.








as I can make a link to a page within a folder ie / WEB-INF/front/example.xhtml
but does not work
thank you very much!!!
Hey guys.
Im making a web 100% based on JSF and i found a problem when i want to do this:
My index:
**
Parallelized Bioinformatics Algorithms built on Multicore Processors
#{controlWeb.getContenido()}
**
All zones except one are always the same. The zone (id = content) I want that when you click a link in any parts of the website all links back to the index.xhtml and in that area the name change to the appropiate webpage. My idea was create a ManagedBean with a String and if you click on a link, that link modify somehow the variable of ManagedBean and when the link return to index that will read from the variable and it will change the web.
The problem is i cant use a setter function called #{controlWeb.setContenido(“STRING”)} because that command is executed in compile time and doesnt work.
I need one solution or another idea for make that please.
Sorry bad comment.
Here is the link to the question:
http://javanullpointer.com/questions/129/jsf-20-need-idea-for
thanks.
Very good stuff
[...] JSF 2 link, commandLink and outputLink example <h:link>, <h:commandLink> and <h:outputLink> example. [...]
[...] JSF 2 link, commandLink and outputLink example [...]