Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  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.

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:

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.