JSF - h:commandButton



The h:commandButton tag renders an HTML input element of the type "submit".

JSF Tag

<h:commandButton value = "Click Me!" onclick = "alert('Hello World!');" /> 

Rendered Output

<input type = "submit" name = "j_idt10:j_idt13" value = "Click Me!" 
   onclick = "alert('Hello World!');" />

Tag Attributes

S.No Attribute & Description
1

id

Identifier for a component

2

rendered

A boolean; false suppresses rendering

3

value

A component’s value, typically a value binding

4

valueChangeListener

A method binding to a method that responds to value changes

5

coords

Coordinates for an element whose shape is a rectangle, circle, or polygon

6

dir

Direction for text. Valid values are ltr (left to right) and rtl (right to left)

7

disabled

Disabled state of an input element or button

8

tabindex

Numerical value specifying a tab index

9

target

The name of a frame in which a document is opened

10

title

A title, used for accessibility, that describes an element. Visual browsers typically create tooltips for the title’s value

11

width

Width of an element

12

onblur

Element loses focus

13

onchange

Element’s value changes

14

onclick

Mouse button is clicked over the element

15

ondblclick

Mouse button is double-clicked over the element

16

onfocus

Element receives focus

17

onkeydown

Key is pressed

18

onkeypress

Key is pressed and subsequently released

19

onkeyup

Key is released

20

onmousedown

Mouse button is pressed over the element

21

onmousemove

Mouse moves over the element

22

onmouseout

Mouse leaves the element’s area

23

onmouseover

Mouse moves onto an element

24

onmouseup

Mouse button is released

25

onreset

Form is reset

26

onselect

Text is selected in an input field

Example Application

Let us create a test JSF application to test the above tag.

Step Description
1 Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2 Modify home.xhtml as explained below. Keep rest of the files unchanged.
3 Compile and run the application to make sure business logic is working as per the requirements.
4 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
5 Launch your web application using appropriate URL as explained below in the last step.

home.xhtml

<!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">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   
   <body>
      <h2>h:commandButton example</h2>
      <hr />
      
      <h:form>
         <h:commandButton value = "Click Me!" onclick = "alert('Hello World!');" />
      </h:form>
   </body>
</html>

Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce the following result.

JSF h:commandButton
jsf_basic_tags.htm
Advertisements