Home Struts Struts2 Validate TextArea
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Validate TextArea

Advertisement
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">
<html>
<head>
<title>RoseIndia.Net Struts 2 Tutorial</title>
</head>

<body>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="400">
<tr>
<td><font color="#000080" size="5"><b>RoseIndia.net Struts 2 Tutorials</b><br>
&nbsp;&nbsp;&nbsp;</font></td>
</tr>
<tr>
<td><font color="#000080"><b>Select the following links to test the
examples</b></font></td>
</tr>
<tr>
<td>

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


</td>
</tr>
<tr>
<td><font color="#000080">&nbsp;<br>
&nbsp;<br>
&nbsp;<br>
Visit <a href="http://www.roseindia.net">http://www.roseindia.net</a>
for latest tutorials</font></td>
</tr>
</table>
</center>
</div>
<p align="center">&nbsp;</p>
</body>
</html>

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" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- 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">
<result>/pages/chatQuestion/limitedCharacter.jsp</result>
</action>

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


</package>


<!-- Add packages here -->

</struts>

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" %>

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

<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" />
</s:form>

</body>
</html>

Step 4: Create an action class:

Here is the code to be added in thelimitedCharacterAction.java:

package net.roseindia;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.PrintStream;
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);
System.out.println("countSummaryText:"+countSummaryText);
if (countSummaryText >0 && countSummaryText < 250){
return SUCCESS;
}
else{
if(getSummary1().equals(""))
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"%>

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

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

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

</html>

Output:

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:

     

Advertisement

Liked it!  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.

Posted on: December 18, 2008

Ask Questions?    Discuss: Validate TextArea  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com