Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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">
<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:

                         

» View all related tutorials
Related Tags: c ajax flex deployment ide plugins dojo struts io tags servlet annotations help sed release test vi port new tag

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.