attributes of hform tag

attributes of hform tag

The <h:form> tag in JavaServer Faces (JSF) is used to encapsulate a group of UI components within a form for processing. It provides various attributes that control the behavior and appearance of the form. Here’s an explanation followed by a Java example for some commonly used attributes of the <h:form> tag:

attributes of hform tag

Explanation

  • 1.  id Attribute:
    • Specifies the identifier for the form component in the generated HTML output.
    • It must be unique within the context of the page.
  • 2.  style and styleClass Attributes:
    • style: Allows specifying inline CSS styles for the form.
    • styleClass: Allows assigning one or more CSS classes to the form for styling purposes.
    • These attributes help in customizing the appearance of the form components.
  • 3.  prependId Attribute:
    • Determines whether the component identifier (ID) of child components should be prepended with the form’s ID.
    • Useful when managing component IDs within composite components or when needing to ensure uniqueness of IDs.
  • 4.  onsubmit and onreset Attributes:
    • onsubmit: Specifies a JavaScript function to execute when the form is submitted.
    • onreset: Specifies a JavaScript function to execute when the form is reset.
    • These attributes enable client-side scripting to perform actions before form submission or resetting.

Java Example

Here’s an example demonstrating the use of some attributes in the <h:form> tag in JSF:

FormAttributesBean.java
java
package com.example.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class FormAttributesBean {

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void submitForm() {
        System.out.println("Username submitted: " + username);
        // Business logic or data processing can be added here
    }
}

form_attributes.xhtml
xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>JSF Form Attributes Example</title>
</h:head>
<h:body>
    <h:form id="myForm" style="background-color: f0f0f0;" prependId="false">
        <h:outputLabel for="usernameInput" value="Enter Username:" />
        <br/>
        <h:inputText id="usernameInput" value="{formAttributesBean.username}" required="true" />
        <h:message for="usernameInput" style="color: red;" />
        <br/>
        <h:commandButton value="Submit" action="{formAttributesBean.submitForm}" styleClass="btn btn-primary" />
    </h:form>
</h:body>
</html>

Explanation of Example

  • Managed Bean (FormAttributesBean.java):  This managed bean (FormAttributesBean) is request-scoped and includes a property username. It also contains a method submitForm() which is invoked when the <h:commandButton> in form_attributes.xhtml is clicked.
  • JSF Page (form_attributes.xhtml):  This JSF page includes an <h:form> tag (<h:form id=”myForm” style=”background-color: f0f0f0;” prependId=”false”>) encapsulating form components:
    • <h:inputText> for entering a username (usernameInput).
    • <h:commandButton> triggering submitForm() method in FormAttributesBean on click.

Attributes Used:

  • id: Specifies myForm as the identifier for the form.
  • style: Sets the background color of the form to light gray (f0f0f0).
  • prependId=”false”: Disables prepending the form’s ID to child component IDs.
  • styleClass=”btn btn-primary”: Assigns CSS classes to the submit button for styling.

These attributes in <h:form> provide flexibility in managing the appearance, behavior, and functionality of forms in JSF applications. They facilitate customization and client-side scripting while ensuring proper data submission and validation handling.