In this example, we will learn how to create a TextArea in Spring MVC using Spring tag library. We will walk-through the usage of <form:textarea> tag. Here we will create a Spring MVC form with a text in which user enters his address and we will add validation support to check if the user enters some text in it.
In Spring MVC we will use <form:textarea> tag to render a textarea
<form:textarea path="address" row="5" col="40"></form:textarea>
Which produces the below HTML code.
<textarea id="address" name="address" col="40" row="5"></textarea>
Folder Structure :
- Create a Dynamic Web Project SpringMVCFormHandling and create a package for our src files “com.javainterviewpoint“
- Place the Spring 3 jar files under WEB-INF/Lib
commons-logging-1.1.1.jar
log4j-1.2.16.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
hibernate-validator-4.2.0.Final.jar
spring-aspects-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar
validation-api-1.1.0.Final.jar
jstl-1.1.2.jar - Create the Java classes TextArea_Controller.java and TextAreaBean.java under com.javainterviewpoint folder.
- Place the SpringConfig-servlet.xml and web.xml under the WEB-INF directory
- View files SpringMVC_TextAreaExample.jsp and textArea_Success.jsp are put under the sub directory under WEB-INF/Jsp
Controller
TextArea_Controller.java
- The DispatcherServlet mapping which we make in the web.xml will delegate the all the request to our TextArea_Controller as we have annotated it with @Controller annotation.
- We use the @RequestMapping annotation to map each of the requests which we get to individual methods. Our controller has two methods initializeForm() and processForm().
- The firstMethod (initializeForm) will take the user to the “SpringMVC_TextAreaExample” which is our view component with form backing object TextAreaBean.
- The Second method (processForm) will get called when the user submits the form. There the TextAreaBean object “ta” will be validated as we have annotated with @Valid annotation and the validation results will be added to the BindingResult. Based on the result we will re-direct the user back to the “SpringMVC_TextAreaExample” or “textArea_Success” page.
import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class TextArea_Controller { @RequestMapping("/TextAreaExample") public ModelAndView initializeForm() { System.out.println("inside"); return new ModelAndView("SpringMVC_TextAreaExample","ta",new TextAreaBean()); } @RequestMapping("/processTextAreaForm") public String processForm(@Valid @ModelAttribute("ta")TextAreaBean ta,BindingResult result) { if(result.hasErrors()) { return "SpringMVC_TextAreaExample"; } else { return "textarea_Success"; } } }
Model
TextAreaBean.java
Here TextAreaBean act as a Model which has an address property. We have added the annotation @NotEmpty to validate if the user has entered a address in it. The custom validation messages are added in the props.properties file.
import org.hibernate.validator.constraints.NotEmpty; public class TextAreaBean { @NotEmpty String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
View
SpringMVC_TextAreaExample.jsp
Our view component has a textarea generated using the Spring form tag library. <form:errors> tag displays the error message which occurs during the validation
<%@ 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>Insert title here</title> <style> .error { color: red; font-weight: bolder; } .commonerrorblock { color: #000; background-color: #ffEEEE; border: 3px solid #ff0000; } </style> </head> <body> <form:form method="post" action="processTextAreaForm" commandName="ta"> <form:errors path="*" element="div" cssClass="commonerrorblock"/> <table> <tr> <td>Address</td> <td> <form:textarea path="address" row="5" col="40"></form:textarea> </td> <td> <form:errors path="address" cssClass="error"/> </td> </tr> <tr> <td></td><td><input type="submit"></td> </tr> </table> </form:form> </body> </html>
props.properties
NotEmpty.ta.address = Please enter your address!!
textArea_Success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> <h2>Address </h2> ${ta.address} </body> </html>
Configurations
web.xml
The web.xml has everything about the application that a server needs to know, which is placed under the WEB-INF directory. <servlet-name> contains the name of the SpringConfiguration, when the DispatcherServlet is initialized the framework will try to load a configuration file “[servlet-name]-servlet.xml” under the WEB-INF directory.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMVCFormHandling</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>SpringConfig</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SpringConfig</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
SpringConfig-servlet.xml
- The SpringConfig-servlet.xml is also placed under the WEB-INF directory.
- <context:component-scan> will let the Spring Container to search for all the annotation under the package “com.javainteriviewpoint”.
- <mvc:annotation-driven/> annotation will activate the @Controller, @RequestMapping, @Valid etc annotations.
- The view is resolved through “org.springframework.web.servlet.view.InternalResourceViewResolver” which searches for the jsp files under the /WEB-INF/Jsp/ directory.
- Resource Bundle is accessed through the “org.springframework.context.support.ResourceBundleMessageSource” through its property “basename” which has the value “props”, and hence our property file should “props.properties”
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.javainterviewpoint" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="props"></property> </bean> </beans>
Lets run our application
Now lets run our application, do a clean build and deploy the application in the Server
Hit on the url “http://localhost:8080/SpringMVCFormHandling/TextAreaExample”
Submit the form without selecting any value
Upon successful validation, success page will be rendered to the user
Leave a Reply