JSF Form Components Example Tutorial

Filed Under: JSF

JSF Form component is a collection of fields along with the data and submit functionality to be sent to the model implementing the business scenario.

JSF Form

To use the form in your JSF page include the following namespace;

xmlns:h="http://java.sun.com/jsf/html"

Some of the important form tag attributes are;

  1. id: This is the unique identifier used to identify a component.
  2. onclick: invokes the javascript function to be called when a button is clicked next to an element.
  3. onsubmit: The javascript function to be called on click of form by a submit button.
  4. onreset: Javascript to be invoked on the reset of the elements in a form.
  5. rendered: flag indicating whether a component should be rendered or still processed.
  6. binding: value of the expression linked to a property in a backing bean.
  7. lang: the language used by the components in the form.
  8. target: Name of the frame where the resource retrieved is to be displayed.
  9. accept: the contents list that the form can handle.
  10. ondblclick: Javascript code to be executed when the mouse is double clicked over a field in a form.
  11. onmouseup: Javascript code to be executed when the mouse button is released over a component.
  12. onmousedown: Javascript code to be executed when the mouse pointer is clicked down over this element.
  13. acceptCharSet: defines the list of character encoding that the form will accept.
  14. style: The CSS style definitions that can be applied for the form
  15. prependId: flag that indicates whether id should be prepended to the form
  16. dir: Overrides default text functionality for this component.
  17. title: A title for an element of the form used as tooltip.

JSF Form Example

Consider an example to demonstrate the usage of the JSF form. A minimalistic form contains at least some text fields, labels and a submit button to generate a post request. This example demonstrates a minimal JSF form page.

Create a page called cardetails.xhtml as shown below.


<?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:head>
	<title>Adding a Form</title>
</h:head>
<h:body>

	<h:form>
		<h3>Adding Form Components</h3>
		<h:panelGrid columns="3">
			<h:outputLabel for="cname">Car Name:</h:outputLabel>
			<h:inputText value="#{car.cname}" id="cname"></h:inputText>
			<br>
			<br>

			<h:outputLabel for="Id">Car Id:</h:outputLabel>
			<h:inputText value="#{car.id}"></h:inputText>
			<br>
			<br>

			<h:outputLabel for="color">Color:</h:outputLabel>
			<h:inputText value="#{car.color}"></h:inputText>
			<br>
			<br>

			<h:outputLabel for="model">Model:</h:outputLabel>
			<h:inputText value="#{car.model}"></h:inputText>
			<br>
			<br>

			<h:outputLabel for="regno">Registration Number:</h:outputLabel>
			<h:inputText value="#{car.regno}"></h:inputText>
			<br>
			<br>
			<h:commandButton action="viewdetails" value="Submit"></h:commandButton>

		</h:panelGrid>
	</h:form>
</h:body>
</html>

In the JSF page above, we declared a <h:form> tag that contains the fields – name, id, model, color and registration number pertaining to the car object. All these fields together constitute a Car form wherein a user can enter the details and click the submit button to post the details.

Upon click of the form submit button, viewdetails.xhtml page is called that prints the data entered by the user.

Let’s now create the viewdetails.xhtml page that displays all the car details entered by the user.


<?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:head>
	<title>Car Details</title>
</h:head>
<h:body>
       Car Id:#{car.id}
        <br>
        <br>
       Car Name:#{car.cname}
        <br>
	<br>
       Car color:#{car.color}
        <br>
	<br>
       Car Model:#{car.model}
        <br>
	<br>
       Car Registration Number:#{car.regno}
    </h:body>
</html>

This view page fetches all the details from the Car bean through getter and setter methods.

Below is the code for Car managed bean.


package com.journaldev.jsf.beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Car implements Serializable {

	private static final long serialVersionUID = 1715935052239888761L;
	private String cname;
	private String color;
	private String Id;
	private String model;
	private String regno;

	public Car() {
	}

	public Car(String cname, String color, String Id, String model, String regno) {
		this.cname = cname;
		this.color = color;
		this.Id = Id;
		this.model = model;
		this.regno = regno;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getCname() {

		System.out.println("car name is" + cname);
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getRegno() {
		return regno;
	}

	public void setRegno(String regno) {
		this.regno = regno;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getId() {
		return Id;
	}

	public void setId(String Id) {
		this.Id = Id;
	}

}

The Car class contains the getter and setter methods for all the fields.The @ManagedBean annotation indicates that the car is a managed bean and @SessionScoped indicates that the bean is valid for the session.

Below image shows the final project structure of the project.

JSF Form Example

JSF Form Example Test

Just build the project and run the application, you should get below response in the browser.

JSF Form Submit

JSF Form Example

That’s all for the JSF form example and the interaction between the managed beans. You can download final project from below link and play around with it to learn more.

Comments

  1. Deepak Goswami says:

    Very good. You have not mentioned the IDE used here; probably either NetBeans or Eclipse is being used.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages