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.

Note
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 :

Note
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&amp;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&amp;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&amp;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&amp;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 :

  1. 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.
  2. 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.
  3. 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“.

Download Source Code

Reference

  1. JSF <h:link /> JavaDoc
  2. JSF <h:commandLink /> JavaDoc
  3. JSF <h:outputLink /> JavaDoc
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: pay per day loans plan()

  • Pingback: YouTube likes kopen()

  • Pingback: sms laan nu()

  • Pingback: water ionizer machines()

  • Pingback: stop parking()

  • Pingback: hurtigt laan penge nu()

  • Pingback: 3gp mobile porn()

  • Pingback: Russian redhead mother tempt her son()

  • Pingback: Blue Coaster33()

  • Luis

    Muchas gracias por el post, me ha sido de mucha utilidad…. Saludos desde Ecuador

  • http://storify.com/sarahvpi/pathmark-coupons-coupons-available-now henry

    Thanks designed for sharing such a good thinking, piece of
    writing is good, thats why i have read it fully

  • http://www.slideshare.net/leahedwvm/agloves-coupon-code-great-discount mark

    Hello There. I found your blog using msn.
    This is a very well written article. I will be sure to bookmark it and
    return to read more of your useful info. Thanks for the
    post. I’ll definitely comeback.

  • Joao Paulo Bossoni

    thank you very much!!!

  • nnnn

    jhkjhkhjk

  • http://plus.google.com/b/106292507931356082011/106292507931356082011http://www.youtube.com/user/sobeauty21http://www.youtube.com/watch?v=YqRhg2NVTS4&feature=plcphttp://twitter.com/#!/sobeauty21htt WemnPneulkLem

    I do believe all the ideas you’ve introduced on your post. They are very convincing and will definitely work. Nonetheless, the posts are too brief for beginners. May you please lengthen them a little from subsequent time? Thank you for the post.

  • vaibhav

    i want to intergrate jsf with struts2 please help me i am beginner in jsf

  • Martin

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

  • Robert

    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.

  • Vikas Kumar

    Very good stuff

  • Pingback: JSF 2.0 Tutorials()

  • Pingback: JSF 2 dataTable sorting example()