Table of contents
1.
Introduction
2.
h:commandButton
2.1.
Syntax
2.2.
JSF Tag Example
2.2.1.
Rendered Output
3.
Attributes of h:commandButton
4.
Example
4.1.
test.xhtml
4.2.
welcome.xhtml
4.3.
faces-config.xml
4.4.
web.xml
5.
Output
6.
Frequently Asked Questions
6.1.
What is the difference between a Button and a Command Button?
6.2.
What is a JSF tag?
6.3.
What is the JSF form?
6.4.
What is a facet tag in JSF?
7.
Conclusion
Last Updated: Mar 27, 2024

commandButton Tag in JSF

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaServer Faces or JSF is a new Java standard technology for building component-based and event-oriented web interfaces. Similar to JavaServer Pages (JSP), JSF allows server-side access to data and logic.

In this article, we will understand a very vital concept in JSF for web development. While developing any web interface, you will probably need a Command Button to access some information or set of data. Here we will introduce the implementations and attributes of the h:commandButton tag in JSF that will allow you to achieve such a button.

h:commandButton

JSF provides a lot of sophisticated APIs and tag libraries. Tag libraries make it easier to add components on web pages and relate them to backend objects. It has handles for applying component tags.

We use the h:commandButton tag in JSF to render HTML input of the "submit" button type. That means that the h:commandButton tag creates a submit button used to submit an application form.

Syntax

<h:commandButton></h:commandButton> 

 

JSF Tag Example

<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');" />

Attributes of h:commandButton

Each tag comes with multiple attributes. Listed below are the attributes of the JSF h:commandButton tag.

  1. id: The tag’s identifier
  2. rendered: A boolean value where false would suppress the rendering
  3. styleClass: class name of Cascading stylesheet (CSS) 
  4. value: The value binding
  5. valueChangeListener: A method binding that responds to changes in value
  6. required: A boolean; if set to true, marks the tag as needed
  7. coords: An element’s coordinates whose shape is a rectangle, circle, or polygon
  8. dir: Direction for text. ltr (left to right) and rtl (right to the left) are valid values.
  9. disabled: An Input element or button disabled state
  10. style: Inline style info
  11. tabindex: Numerical value to specify a tab index
  12. target: The name of a frame in which the document is opened
  13. title: We can use a title for accessibility. Browsers typically create tooltips for the value of the title
  14. width: An element’s Width
  15. onblur: Event handler for losing focus
  16. onchange: Event handler for changes in value
  17. onclick: Event handler for the click of the Mouse button over the element
  18. ondblclick: Event handler for double-click of the Mouse button
  19. onfocus: Event handler for element received focus
  20. onkeydown: Event handler for Keypress
  21. onkeypress: Event handler for Key press and release
  22. onkeyup: Event handler for Key release
  23. onmousedown: Event handler for Mouse button press
  24. onmousemove: Event handler for mouse movement
  25. onmouseout: Event handler for mouse moved left
  26. onmouseover: Event handler for mouse moved onto
  27. onmouseup: Event handler for mouse button released
  28. onreset: Event handler for form reset
  29. onselect: Event handler for selected Text

Example

Now that we have seen the attributes and the basic syntax of the h.commandButton tag, we can move forward with the implementation using an elementary example.

test.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"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>JSF command button example.</title>
    </h:head>
    <h:body>
      <h3>JSF command button example.</h3>
      <h:form>
         <h:commandButton value="Say Hello" action="welcome" />
      </h:form>
    </h:body>
 
</html>

welcome.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"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>JSF command button example.</title>
    </h:head>
    <h:body>
      <h3>Hello World.</h3>
    </h:body>
 
</html>

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
<navigation-rule>
   <from-view-id>test.xhtml</from-view-id>
   <navigation-case>
      <from-outcome>success</from-outcome>
      <to-view-id>welcome.xhtml</to-view-id>
   </navigation-case>   
</navigation-rule>
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
         javax.faces.webapp.FacesServlet
    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

Output

 

After clicking the Say Hello button, you should see the following page appear.

 

Frequently Asked Questions

What is the difference between a Button and a Command Button?

  • h:button: On clicking the h:button issues GET requests that we can bookmark. 
  • h:commandButton: Instead of a get request, issues a POST request which sends the form data back to the server.

What is a JSF tag?

JSF provides a standard HTML tag library. These tags render into corresponding html output. For these tags, we need to use some namespaces of URI in the html node.

What is the JSF form?

The <h:form> tag in JSF represents an input form with child components that may contain data presented to the user or submitted with the form. It might also include HTML markup laying out the components on the page.

What is a facet tag in JSF?

A facet tag in a project is a specific unit of functionality you can add to a project when you require that functionality. When you add a project facet to a project, it can add nature, classpath entries, builders, and resources to that project, depending on the nature and characteristics.

Conclusion

Throughout this article, we have understood the functionalities, attributes, implementations, and needs of the commandButton Tag in JSF. We hope this article helped you in your journey towards mastering JSF

Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Happy Learning!

Live masterclass