The Java EE 6 Tutorial, Volume I

Using Command Components for Performing Actions and Navigation

In JavaServer Faces applications, the button and hyperlink component tags are used to perform actions, such as submitting a form, and for navigating to another page. They are called command components as they perform an action when activated.

The h:commandButton tag is rendered as a button. The h:commandLink tag is rendered as a hyperlink.

In addition to the tag attributes listed in Common Component Tag Attributes, the h:commandButton and h:commandLink tags can use the following attributes:

See Referencing a Method That Performs Navigation for more information on using the action attribute. See Referencing a Method That Handles an Action Event for details on using the actionListener attribute.

Rendering a Button With the h:commandButton Tag

If you are using a commandButton component, when a user clicks the button, the data from the current page is processed, and the next page is opened. Here is a h:commandButton tag example:

<h:commandButton value="Submit"

Clicking the button will cause the submit method of CashierBean to be invoked because the action attribute references this method. The submit method performs some processing and returns a logical outcome.

The value attribute of the example commandButton tag references the button’s label. For information on how to use the action attribute, see Referencing a Method That Performs Navigation .

Rendering a Hyperlink With the h:commandLink Tag

The h:commandLink tag represents an HTML hyperlink and is rendered as an HTML <a> element. It acts like a form submit button and is used to submit an action event to the application.

A h:commandLink tag must include a nested h:outputText tag, which represents the text that the user clicks to generate the event. Here is an example:

<h:commandLink id="NAmerica" action="bookstore"
     <h:outputText value="#{bundle.English}" />

This tag will render the following HTML:

<a id="_id3:NAmerica" href="#"
     return false;">English</a>

Note –

The h:commandLink tag will render JavaScript. If you use this tag, make sure your browser is enabled for JavaScript.