JSF Tutorial - JSF Link Example








The h:Link tag renders an HTML "anchor" element.

The following JSF tag

<h:link value="Page 1" outcome="page1" />

is rendered into the following HTML code

<a href="/helloworld/page1.jsf">Page 1</a>

Tag Attributes

AttributeDescription
idid for the tag
bindingReference to the component used in a backing bean
renderedA boolean value; false would suppress rendering
styleClassCascading stylesheet (CSS) class name
valuevalue binding
valueChangeListenerA method binding that responds to value changes
converterConverter class name
validatorClass name of a validator attached to the component
requiredA boolean; if true, marks the tag as required
accesskeygives focus to an element
acceptComma-separated list of content types for a form
accept-charsetComma- or space-separated list of character encodings for a form.
altAlternative text for nontextual elements such as images
borderPixel value for an element's border width
charsetCharacter encoding for a linked resource
coordsCoordinates for an element whose shape is a rectangle, circle, or polygon
dirDirection for text. Valid values are ltr (left to right) and rtl (right to left).
hreflangBase language of a resource specified with the href attribute;
langBase language of an element's attributes and text
maxlengthMaximum number of characters for text fields
readonlyRead-only state of an input field
relRelationship between the current page and linked page
revReverse link from the anchor specified with href to the current document.
styleInline style information
tabindexNumerical value specifying a tab index
targetThe name of a frame in which a document is opened
titleA title used for accessibility. Browsers typically create tooltips for the title's value
typeType of a link; for example, stylesheet
widthWidth of an element
onblurEvent handler for losing focus
onchangeEvent handler for value changes
onclickEvent handler for Mouse button clicked over the element
ondblclickEvent handler for Mouse button double-clicked
onfocusEvent handler for element received focus
onkeydownEvent handler for Key pressed
onkeypressEvent handler for Key pressed and released
onkeyupEvent handler for Key released
onmousedownEvent handler for Mouse button pressed
onmousemoveEvent handler for mouse moved
onmouseoutEvent handler for mouse left
onmouseoverEvent handler for mouse moved onto
onmouseupEvent handler for mouse button released
onresetEvent handler for form reset
onselectEvent handler for Text selected




Example

The following code is from demo.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
       <h:link value="Login page" outcome="login" />
      <br/>
      <h:link value="Login page + Param " outcome="login" >
        <f:param name="username" value="mkyong" />
      </h:link>
      <br/>
       <h:link outcome="login" >
         <h:graphicImage library="images" name="myImage.png" />
       </h:link>
      <br/>
       <h:commandLink value="Login page" />
      <br/>
       <h:commandLink action="#{user.goLoginPage}" value="Login page" />
      <br/>
       <h:commandLink action="#{user.goLoginPage}" value="Login page + Param ">
         <f:param name="username" value="mkyong" />
       </h:commandLink>
      <br/>
       <h:commandLink action="#{user.goLoginPage}">
         <h:graphicImage library="images" name="myImage.png" />
       </h:commandLink>
      <br/>
       <h:outputLink>Login page</h:outputLink>
      <br/>
       <h:outputLink value="login.xhtml" >Login page</h:outputLink>
      <br/>
       <h:outputLink value="login.xhtml">
         <h:outputText value="Login page" />
         <f:param name="username" value="java2s.com" />
       </h:outputLink>
      <br/>
       <h:outputLink value="login.xhtml">
         <h:graphicImage library="images" name="myImage.png" />
       </h:outputLink>
      </h:form>
    </h:body>
</html>

The following code is from UserBean.java.

package com.java2s.common;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
  public String goLoginPage(){
    
    return "login";
    
  }
}

The following code is from login.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
 
    <h:body>
      
      <h1>login.xhtml</h1>
      
    </h:body>

</html>


Download Link-commandLink-outputLink.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml