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&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的更多相关文章
- 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 ...
- Top 40 Static Code Analysis Tools
https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...
- JSF 与 HTML 标签的联系
*页面的开头 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ t ...
- JSF标签大全详解
1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...
- JSF-使用JSF标记
使用JSF标记 基于Facelets技术的JSF页面是一个 XHTML页面,文件扩展名为 .xhtml 1)JSF页面可用html标记,但必须满足: ①所有标记都必须闭合.如<p>开始,& ...
- JSF action actionListner 详解
https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener actionLi ...
- 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 ...
- 【转】JSF中的三大核心组件 UI标签的详细介绍和使用举例
JSF提供了大量的UI标签来简化创建视图.这些UI标签类似于ASP.NET中的服务器组件.使用这些标签,可以通过其value,binding,action,actionListener等属性直接绑定到 ...
- JSF request参数传递
转载自:http://blog.csdn.net/duankaige/article/details/6711044 1:JSF页面之间传参 方法1: <h:outputLink value=& ...
随机推荐
- jQuery 停止动画、jQuery Callback 函数、jQuery - Chaining
一.jQuery 停止动画 jQuery stop() 方法用于在动画或效果完成前对它们进行停止. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. $(sele ...
- 编写jquery插件的分享
一.类级别($.extend) 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.extend(o ...
- 51nod1403 有趣的堆栈
看成括号序列的话第二种方法其实就是左括号和右括号之间有多少对完整的括号. #include<cstdio> #include<cstring> #include<ccty ...
- Asp.Net验证码2
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- 14.Object-C--浅谈Foundation框架字符串NSString 与NSMutableString
OC的字符串时经常使用到的,今天我对于OC字符串做一个简单的总结,如果有错误之处,麻烦留言指正.感谢! NSString是一个不可变长度的字符串对象.表示它初始化以后,你不能改变该变量所分配的内存中的 ...
- 关于UNION ALL与 UNION 用法和区别
(转自:http://www.cnblogs.com/EricaMIN1987_IT/archive/2011/01/20/1940188.html) UNION指令的目的是将两个SQL语句的结果合并 ...
- scala学习笔记(1):基本语法与容器
1 var 可变,val 不可变 var (a,b) = (10,20) 分别对a,b赋值 a=10, b=20 var a,b = (10,20)则 是a=(10,20) b=(10,20) 2 L ...
- 最大熵模型 Maximum Entropy Model
熵的概念在统计学习与机器学习中真是很重要,熵的介绍在这里:信息熵 Information Theory .今天的主题是最大熵模型(Maximum Entropy Model,以下简称MaxEnt),M ...
- 【原创】牛顿法和拟牛顿法 -- BFGS, L-BFGS, OWL-QN
数据.特征和数值优化算法是机器学习的核心,而牛顿法及其改良(拟牛顿法)是机器最常用的一类数字优化算法,今天就从牛顿法开始,介绍几个拟牛顿法算法.本博文只介绍算法的思想,具体的数学推导过程不做介绍. 1 ...
- ORACLE RAC集群硬件资源管理与单节点的区别
硬件资源是由OS kernel管理的,应用软件是不能直接访问硬件的,必须通过OS kernel提供的API接口间接访问,OS kernel 除了要完成用户的请求,还通过进程调度等机制来控制多进程对资源 ...