Share on Google+Share on Google+

Validate TextArea

In this section, you will learn how to validate your text area in struts 2. A text area contains 1 to 250 characters. It cann't support "null" value.

Validate TextArea


In this section, you will learn how to validate your text area in struts 2. A text area contains 1 to 250 characters. It cann't support "null" value.

For validating your text are in your application follows the certain steps:

Step 1: Create index.jsp page




Here is the code to be added in the index.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<title>RoseIndia.Net Struts 2 Tutorial</title>

<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="400">
<td><font color="#000080" size="5"><b> Struts 2 Tutorials</b><br>
<td><font color="#000080"><b>Select the following links to test the

<li><a href="roseindia/characterLimit.action">Characters and limiting Example</a></li>

<td><font color="#000080">&nbsp;<br>
Visit <a href=""></a>
for latest tutorials</font></td>
<p align="center">&nbsp;</p>

Step 2: Create an action mapping in the struts.xml file. 

Here is the code to be added in the struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<!-- Rose India Struts 2 Tutorials -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="roseindia" namespace="/roseindia" extends="struts-default">

<action name="characterLimit">

<action name="characterLimit1" class="net.roseindia.limitedCharacterAction">
<result name="error">/pages/chatQuestion/limitedCharacter.jsp</result>
<result name="success">/pages/chatQuestion/limitedCharacterSuccess.jsp</result>


<!-- Add packages here -->


Step 3: Create a JSP page that contains text area and submit button:

Here is the code to be added in the limitedCharacter.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>

<title>Limited Characters in Text Area</title>
<s:head theme="ajax" />

<s:form action="characterLimit1" method="POST" validate="true">
<s:textarea name="summary1" label="Brief Summary" rows="6" cols="40"/>
<s:submit value="Save" align="center" />


Step 4: Create an action class:

Here is the code to be added in

package net.roseindia;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.*;

public class limitedCharacterAction extends ActionSupport{
private String summary1;

public String getSummary1() {
return summary1;

public void setSummary1(String summary1) {
this.summary1 = summary1;

public String execute() throws Exception{
String summaryText = getSummary1();
long countSummaryText = summaryTextcount(summaryText);
if (countSummaryText >0 && countSummaryText < 250){
return SUCCESS;
addFieldError("summary1","Brief Summary is required.");
if((countSummaryText > 250))
addFieldError("summary1","Brief Summary must be 1 to 250 Charaters");
return ERROR;

private static long summaryTextcount(String str){
return str.length();

Step 5: Create a JSP page that contains the inputted text in the textarea:

Here is the code to be added in limitedCharacterSuccess.jsp:

<%@ taglib prefix="s" uri="/struts-tags"%>

<title>Limited Characters in Text Area</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"

<b>Brief Summary: </b><s:property value="summary1" /><br>



When you click the "Save" command button with out any data then you get:

If you enter more than 250 characters then you get :

If you enter your text between 1 to 250 characters then you get:



Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Validate TextArea  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image