Unlike <h:commandLink/> the <h:outputLink/> doesn’t generate JavaScript to make the link act like a submit button. The <h:outputLink/> component generates an HTML anchor element and its value attribute used for the anchor’s href attribute, and the contents of the <h:outputLink/> body are used to populate the body of the anchor element.
The using of <h:outputLink/> component does has the following uses:
- Use the <h:outputLink/> component to create an anchor link for linking an external address.
- Use the <h:outputLink/> component to create an anchor link for linking a parameterized external address.
- Use the <h:outputLink/> component to create an anchor link for linking an internal resources (images,JavaScript, etc ..)
- Use the <h:outputLink/> component to create an anchor link for linking an internal pages.
- Use the <h:outputlink/> component to create an anchor link for internal link (#Identifier).
Also Read:
1. The View
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core"> <h:form prependId="false"> <h1> <h:outputText value="JavaBeat JSF 2.2 Examples" /> </h1> <h2> <h:outputText value="OutputLink Example" /> </h2> <table> <tr> <td> Go To JavaBeat Site </td> <td colspan="2"> <h:outputLink value="https://javabeat.net" > <h:outputText value="Visit JavaBeat Site"></h:outputText> </h:outputLink> </td> </tr> <tr> <td> Search About JSF Posts Inside JavaBeat </td> <td colspan="2"> <h:outputLink value="https://javabeat.net" > <h:outputText value="Visit JavaBeat Site"/> <f:param value="JSF" name="s"/> </h:outputLink> </td> </tr> <tr> <td> Navigate Into Internal Address Page </td> <td colspan="2"> <h:outputLink value="internalAddress.xhtml" > <h:outputText value="Visit Internal Address"></h:outputText> </h:outputLink> </td> </tr> <tr> <td> Access The Image </td> <td colspan="2"> <h:outputLink value="resources/images/JSF-Thumbnail.png" > <h:outputText value="See JSF Image"></h:outputText> </h:outputLink> </td> </tr> <tr> <td> Go To Internal Link OutputText </td> <td colspan="2"> <h:outputLink value="#internalLink" > <h:outputText value="Go To The Internal Index At The Same Page"></h:outputText> </h:outputLink> </td> </tr> </table> <c:forEach var="i" begin="1" end="100"> <br/> </c:forEach> <h:outputText id="internalLink" value="Internal Link"/> </h:form> </html>
internalAddress.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h1> <h:outputText value="JavaBeat JSF 2.2 Examples" /> </h1> <h2> <h:outputText value="OutputLink Example" /> </h2> <h:outputText value="This is an Internal Address"/> </html>
2. The Deployment Descriptor (web.xml)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
3. Directory Structure
The directory structure for the deployed application has the form that explained at the following snapshot.
4. JSF 2 OutputLink Demo
The below snapshot will demonstrate the all possibilities using of <h:outputLink/>.
- The first link should lead us for https://javabeat.net
- The second link should lead us for https://javabeat.net/?s=JSF
- The third link should lead us for internalAddress.xhtml deployed page.
- The fourth link should lead us for a deployed image.
- The fifth link should lead us for internal link located on the same page.