The checkbox tag

In this section, you will learn about Spring form tag library.

The checkbox tag

The checkbox tag

In this section, you will learn about Spring form tag library.

The checkbox tag of Spring form tag library provides HTML 'input' tag of 'checkbox' type.

To understand it completely, let us take an example :

A user need to fill their details in a registration form. The detail can be fill in the form using input field, checkbox etc.

Given below class Registration :

public class Registration
{
	private String firstName;
	
	private String lastName;
	
	private String category[];
	
	public String getFirstName() {
	return firstName;
	}
	
	public void setFirstName(String firstName) {
	this.firstName = firstName;
	}
	
	public String getLastName() {
	return lastName;
	}
	
	public void setLastName(String lastName) {
	this.lastName = lastName;
	}
	
	public String[] getCategory() {
	return category;
	}
	public void setCategory(String[] category) {
	this.category = category;
	}
}

The form.jsp using above Registration class should be like :

<form:form>
	<table>
		<tr>
			<td>First Name:</td>
			<td><form:input path="registration.firstName" /></td>
			</tr>
		<tr>
			<td>Last Name:</td>
			<td><form:input path="registration.lastName" /></td>
		</tr>
		<tr>
			<td>Category:</td>
			<td>
			Sports: <form:checkbox path="registration.category" value="Sports"/>
			Science: <form:checkbox path="registration.category" value="Science"/>
			Current Affairs: <form:checkbox path="registration.category"
			value="Current Affairs"/>
			</td>
		</tr>
	</table>
</form:form>

The above file will be rendered like this :

<form method="POST">
	<table>
	<tr>
		<td>First Name:</td>
		<td><input name="firstName" type="text" value="Harry"/></td>
	</tr>
	<tr>
		<td>Last Name:</td>
		<td><input name="lastName" type="text" value="Potter"/></td>
	</tr>
	<tr>
		<td>Category:</td>
	<td>
	Sports:<input name="registration.category" type="checkbox" value="Sports"/>
	<input type="hidden" value="1" name="_registration.category"/>
	Science:<input name="preferences.interests" type="checkbox" value="Science"/>
	<input type="hidden" value="1" name="_preferences.interests"/>
	Current Affairs: <input name="registration.category" type="checkbox" value="Current Affairs"/>
	<input type="hidden" value="1" name="_registration.category"/>
	</td>
	</tr>
	</table>
</form>