zoukankan      html  css  js  c++  java
  • 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.

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

  • 相关阅读:
    ajax、json一些整理(2)
    ajax、json一些整理(1)
    C# DllImport的用法
    asp.net 获取当前项目路径
    C# 中关闭当前线程的四种方式 .
    DataGridView自定义RichTextBox列
    C#winform的datagridview设置选中行
    Other Linker flags 添加 -Objc导致包冲突
    nat打洞原理和实现
    成为顶尖自由职业者必备的七个软技能之四:如何成为销售之王(转)
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4765399.html
Copyright © 2011-2022 走看看