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

JSF 2 link, commandLink and outputLink example的更多相关文章

  1. Building Applications with Force.com and VisualForce (DEV401) (二三):Visualforce Componets (Tags) Library Part III

    Dev401-024:Visualforce Pages: Visualforce Componets (Tags) Library Part IIIStatic Resources1.Static ...

  2. Top 40 Static Code Analysis Tools

    https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...

  3. JSF 与 HTML 标签的联系

    *页面的开头 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ t ...

  4. JSF标签大全详解

    1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...

  5. JSF-使用JSF标记

    使用JSF标记 基于Facelets技术的JSF页面是一个 XHTML页面,文件扩展名为 .xhtml 1)JSF页面可用html标记,但必须满足: ①所有标记都必须闭合.如<p>开始,& ...

  6. JSF action actionListner 详解

    https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener   actionLi ...

  7. Parameter Passing / Request Parameters in JSF 2.0 (转)

    This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +) (1)  f:vi ...

  8. 【转】JSF中的三大核心组件 UI标签的详细介绍和使用举例

    JSF提供了大量的UI标签来简化创建视图.这些UI标签类似于ASP.NET中的服务器组件.使用这些标签,可以通过其value,binding,action,actionListener等属性直接绑定到 ...

  9. JSF request参数传递

    转载自:http://blog.csdn.net/duankaige/article/details/6711044 1:JSF页面之间传参 方法1: <h:outputLink value=& ...

随机推荐

  1. Qt之QTableView显示富文本

    简述 对于QTableView中的显示,我们前面介绍过很多种,其中包括:文本.进度条.复选框等,今天我们介绍一下关于富文本的显示. 可能绝大多数小伙伴会通过QAbstractTableModel中的d ...

  2. MySQL server has gone away 的解决方法

    原文引用至:http://www.jb51.net/article/23781.htm,感谢! 可能原因:1.发送的SQL语句太长,以致超过了max_allowed_packet的大小,如果是这种原因 ...

  3. 【转】Core Bluetooth框架之一:Central与Peripheral

    原文网址:http://southpeak.github.io/blog/2014/07/29/core-bluetoothkuang-jia-zhi-%5B%3F%5D-:centralyu-per ...

  4. springmvc 入门级教程

    1.先看下目录结构 2.引用所需的jar文件 3.web.xml 配置如下 <?xml version="1.0" encoding="UTF-8"?&g ...

  5. myeclipse 打开xml jsp页面慢 有时候会自动退出

    Myeclipse默认打开文件的方式是 jsp design,每次双击或者使用Ctrl+Shift+R打开 就会用这个打开 ,太慢了而且多次导致Myeclipse挂掉.可以通过以下的方式转化成你想要的 ...

  6. Linux makefile教程之隐含规则九[转]

    隐含规则 ———— 在 我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o] 文件,Windows下是[.o ...

  7. MIME邮件格式

    转自:http://kptu.iteye.com/blog/890180 排版做了调整. Q.什么是MIME?什么是MIME邮件? A. MIME, 全称为"Multipurpose Int ...

  8. Asp.Net 自定义控件实现图片的上传,浏览,删除功能

    4月的时候公司比较闲,就想着自己做点东西,其实主要是为了更加熟悉.Net,毕竟接触的时间不长,趁着有时间想提高提高.不过当我做到图片上传这个功能的时候,就有些停滞不前了,连续写了两天也达不到自己想要的 ...

  9. Thread .join 的用法一例

    在使用身份证读卡器时,要求 1. 身份证读到身份证 就 停止线程. 2. 关闭界面时会 自动停止调用读身份证的线程.这时候就需要用到 Thead.join 例子如下: Thread thread; p ...

  10. OpenGl从零开始之坐标变换(上)

    坐标变换是深入理解三维世界的基础,非常重要.学习这部分首先要清楚几个概念:视点变换.模型变换.投影变换.视口变换. 在现实世界中,所有的物体都具有三维特征,但计算机本身只能处理数字,显示二维的图形,因 ...