JSF 2 link, commandLink and outputLink example

本文详细介绍了JavaServer Faces (JSF) 中的三种链接标签:h:link、h:commandLink 和 h:outputLink 的使用方法及它们之间的区别。通过多个实例展示了如何使用这些标签来创建不同类型的HTML锚元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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 :

//JSF
<h:link value="Login page" outcome="login" />
//HTML output
<a href="/JavaServerFaces/faces/login.xhtml">Login page</a>
//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>
//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>

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.

//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.

//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.

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

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 :

//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.

//JSF
<h:outputLink value="login.xhtml" >
    Login page
</h:outputLink>
//HTML output
<a href="login.xhtml">
    Login page
</a>
//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>
//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“.

转载于:https://www.cnblogs.com/ghgyj/p/4765399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值