在本章你将学习在struts2中如何验证你的text area.一个textarea包含1至250个字符,它不支持"null"值.
为了验证你的textarea请遵循下面的步骤.
第一步 : 创建index.jsp页面
这是要添加到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>
</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"> <br>
<br>
<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"> </p>
</body>
</html>
第二步 : 在struts.xml中创建Action映射 :
这是要添加到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>
第三步 : 创建一个包含textarea和submit按钮的JSP页面 :
这是要添加到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>
第四步 : 创建一个Action类 :
将这些代码添加到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(); } }
第五步 : 创建一个JSP包含输入文本的textarea
将这些代码添加到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>
输出 :
当你不输入任何文本并点击"Save"按钮你将得到 :
如果你输入超过250个字符的时候你将得到 :
当你输入1到250个字符的时候你将得到 :
2 楼 zengzitao823 2011-03-16 10:45
1 楼 lijian1228 2009-08-11 15:52