Spring <form:textarea> Tag

In this Spring example, you will learn how to create a HTML textarea with Spring <form:textarea> tag. For Example :

<form:textarea path="message" rows="5" cols="30" />

In order to use the Spring Form Tags you need to include the following taglib directive in the jsp page.

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>3.1.0.RELEASE</spring.version>
</properties>
 
<dependencies>
 
    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
</dependencies>

1. Model

Following is the Person class.

package com.kruders.domain;

public class Person {
	String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

2. Controller

In order to handle spring forms, you need your controller to extend SimpleFormController. Following class shows you how to extend SimpleFormController

PersonController.java

package com.kruders.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.kruders.domain.Person;

public class PersonController extends SimpleFormController{
	public PersonController(){
		setCommandClass(Person.class);
		setCommandName("personForm");
	}
	
	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		Person person = (Person) command;
		return new ModelAndView("personSuccess","person",person);
	}
}

3. Form Validation

To validate the form fields we have a seperate PersonValidator class that implements the Validator interface, override the validate() method perform all the validations.

Following in the PersonValidator class.

package com.kruders.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.kruders.domain.Person;

public class PersonValidator implements Validator{
	@Override
	public boolean supports(Class<?> clazz) {
		return Person.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors,"message", "message.required");
	}
}

4. Properties File

A properties file to store all the error messages. Here we have the error messages in a seperate properties file so we add the error code, you can even add the error messages directly.

messages.properties

message.required = Enter some message

5. View

Following is the person form that contains Spring’s Form Tags and display error message if any.

form:errors tag to display errors in jsp files.

personForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring &lt;form:textarea &gt; Textarea</title>
<style>
.error {
	color: red;
}
 
</style>
</head>
<body>
<form:form method="POST" commandName="personForm">
	<table>
		<tr>
			<td>Message :</td>
			<td>
				<form:textarea path="message" rows="5" cols="30" />
			</td>
			<td><form:errors path="message" cssClass="error" /></td>
		</tr>
	</table>
	<tr>
			<td colspan="3"><input type="submit" value="Submit"></td>
		</tr>
</form:form>
</body>
</html>

On successful submission of personform, personSuccess.jsp page will be displayed.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Output</title>
</head>
<body>
Message : ${person.message}
</body>
</html>

6. Spring Bean Configuration

We now declare the controller and validator for the personform Spring Bean Configuration file.

	<bean class="com.kruders.controller.PersonController">
		<property name="formView" value="personForm" />
		<property name="successView" value="personSuccess" />
	 
		<!-- Map a validator -->
		<property name="validator">
			<bean class="com.kruders.validator.PersonValidator" />
		</property>
 	</bean>

Also add the properties file.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />

7. Run

Now when you run the project, following screen will be displayed as in Figure 66.1

Figure 66.1 Figure 66.1


Now when you write some text in textarea and click submit button, following screen will be displayed as shown in Figure 66.2

Figure 66.2 Figure 66.2

You can download the source code of this example here.

,

No comments yet.

Leave a Reply