`
seara
  • 浏览: 625214 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Struts1.x系列教程(2):简单的数据验证

阅读更多
本文为原创,如需转载,请注明作者和出处,谢谢!

Struts 2系列教程》

简单验证从本质上说就是在服务端来验证客户端提交的form中的数据。这种验证只是对form中的数据规则进行检查,如必须输入用户ID,价格不能小于0或是对email格式的验证。在这个验证过程中,并不需要访问数据库。因此,简单验证需要在用户提交form后,并且在服务器处理form中的数据之前进行。

在进行完简单验证后,如果form中的数据不合法,程序就会forward到指定的JSP页(一般是包含form的页面),并显示相应的错误信息。如果form中的数据完全正确,程序就会继续执行。

一、在validate方法中进行简单验证


在上一篇文章中我们知道,Struts1.x通过ActionForm的子类来封装了客户端提交的form中的数据。而服务端程序只需要通过ActionForm的子类的对象实例就可以访问form中的数据,而如果不使用ActionForm类,就必须通过request对象来获得form中的数据。通过这种封装机制可以使代码更容易理解。然而,ActionForm类不仅可以封装form中的数据,还可以通过ActionForm类的validate方法来验证form中的数据。validate方法的定义如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->publicActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest)

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

当客户端向服务端提交form后,Servlet引擎首先通过ActionForm的子类的对象实例装载form中的数据,然后再调用validate方法进行验证。validate方法返回了一个ActionErrors对象。这个对象相当于一个Map,如果ActionErrors中没有错误信息,Servlet引擎就认为form中的数据是正确的,这时服务端程序就会继续执行。如果ActionErrors中有错误信息,程序就会跳转到指定的错误页面。下面让我们通过一个完整的例子来演示一下如何通过validate方法来验证form中的数据。实现这个例子需要如下五步:

【第1步】建立JSP页面

在这一步将建立一个叫simpleValidation.jsp的页面,这个JSP页面用于采集用户的输入信息。在<samples工程目录>中建立一个simpleValidation.jsp文件,并编写如下的代码:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><%@pagepageEncoding="GBK"%>
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<html>
<head>
<title>注册信息(测试简单验证)</title>
<styletype="text/css">
.text
{
height
:20px;
width
:160px;
}
</style>
</head>
<body>
<html:formaction="simpleValidation">
<tablewidth="100%">
<tr>
<tdalign="right"width="45%">用户名:</td>
<tdwidth="55%">
<html:textproperty="user"styleClass="text"/>
<fontcolor="red"><html:errorsproperty="errorUser"/></font>
</td>
</tr><tr/><tr/>
<tr>
<tdalign="right">登录密码:</td>
<td>
<html:passwordproperty="password"styleClass="text"/>
<fontcolor="red"><html:errorsproperty="errorPassword"/></font>
</td>
</tr><tr/><tr/>
<tr>
<tdalign="right">重复登录密码:</td>
<td>
<html:passwordproperty="password1"styleClass="text"/>
<fontcolor="red"><html:errorsproperty="errorPassword1"/></font>
</td>
</tr><tr/><tr/>
<tr>
<tdalign="right">电子邮件:</td>
<td>
<html:textproperty="email"styleClass="text"/>
<fontcolor="red"><html:errorsproperty="errorEmail"/></font>
</td>
</tr><tr/><tr/>
<tr>
<tdalign="right"><br>${requestScope.success}</td>
<tdalign="left"><br><html:submitvalue="提交"/></td>
</tr>
</table>
</html:form>
</body>
</html>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->在启动Tomcat后,在IE的地址栏中输入如下的URL

http://localhost:8080/samples/simpleValidation.jsp

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

当通过上面的URL访问simpleValidation.jsp时,并不能正确显示用户信息采集界面。原因是<html:form>标签使用了一个simpleValidation,当JSP转换成Servlet时,这个动作必须在struts-config.xml文件中正确定义,否则将抛出一个javax.servlet.jsp.JspException异常。

【第2步】建立simpleValidation动作

由于本例的着重点是简单验证,因此,simpleValidation动作并不需要处理更多的工作。一个动作对应于一个动作类,这个动作类一般是org.apache.struts.action.Action类的子类。simpleValidation动作只做如下两项工作:

1. 设置验证成功后,在目标页中显示的信息字符串(保存在request的属性中)。

2. 跳转到目标页。

simpleValidation动作对应的动作类是SimpleValidationAction,在<samples工程目录>"src"action目录中建立一个SimpleValidationAction.java文件,并输入如下的代码:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->packageaction;

importjavax.servlet.http.*;
importorg.apache.struts.action.*;

publicclassSimpleValidationActionextendsAction
{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException
{
request.setAttribute(
"success","提交成功!");//设置在目标页中显示的信息字符串
returnmapping.findForward("simple");//跳转到目录页(simple所指的JSP页)
}
}

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]-->【第3步】建立ActionForm类
在这一步我们来建立一个用于接收有户的提交信息的ActionForm类:SimpleValidationForm。这个类从 org.apache.struts.action.ActionForm类继承。在<samples工程目录> "src"actionform目录中建立一个SimpleValidationForm.java文件,代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->packageactionform;

importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.*;

publicclassSimpleValidationFormextendsActionForm
{
//以下四个变量分别对应于simpleValidation.jsp中的四个文本框中的值。
privateStringuser;
privateStringpassword;
privateStringpassword1;
privateStringemail;

publicStringgetUser()
{
returnuser;
}
publicvoidsetUser(Stringuser)
{
this.user=user;
}
publicStringgetPassword()
{
returnpassword;
}
publicvoidsetPassword(Stringpassword)
{
this.password=password;
}
publicStringgetPassword1()
{
returnpassword1;
}
publicvoidsetPassword1(Stringpassword1)
{
this.password1=password1;
}
publicStringgetEmail()
{
returnemail;
}
publicvoidsetEmail(Stringemail)
{
this.email=email;
}
//开始验证用户提交的信息
publicActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest)
{
ActionErrorserror
=newActionErrors();
if(user.equals(""))//必须输入用户名
error.add("errorUser",newActionMessage("error.user.blank"));
if(password.equals(""))//必须输入密码
error.add("errorPassword",newActionMessage("error.password.blank"));
elseif(!password.equals(password1))//两个登录密码必须一致
error.add("errorPassword1",newActionMessage("error.password1.confirmation"));
if(email.equals(""))//必须输入email
error.add("errorEmail",newActionMessage("error.email.blank"));
elseif(!email.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+"))//验证email的格式是否正确
error.add("errorEmail",newActionMessage("error.email.invalid"));

//返回错误信息,如果error中没有错误信息,
//就会调用SimpleValidationAction类的对象实例来执行execute方法。
returnerror;
}
}

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

在编写SimpleValidationAction类时应注意如下八点:
1.
要想在ActionForm类中进行验证,必须在ActionForm类的子类中覆盖validate方法。
2.validate方法在ActionForm类的对象实例装载完用户提交的数据后调用,因此,在调用validate方法时,ActionForm类的属性值已经是用户提交的信息了。所以可以直接使用这些属性值进行验证。
3.validate方法中验证用户提交的数据时,要使用ActionErrors类的实例对象返回错误信息
4.ActionErrors类的构造方法的第二个参是一个ActionMessage类的对象实例,而不是错误描述信息。
5.ActionMessage类的构造方法的参数并不是错误描述信息,而是错误描述信息的key,具体的信息在Java属性文件中(将在下一步实现)。

6.使用ActionForm的属性可以非常好地验证字符串类型,但对于其他的数据类型(如整型)的某些验证却不太适合。如当用户提交数据时,本该提交一个整数,但用户却提交了一个非整数信息。对于这种情况,在ActionForm类的对象实例中这个用户提交的数据的值为0。虽然使用ActionForm类的属性无法准确验证这种情况,但我们可以使用validate方法的第二个参数requestgetParameter方法直接获得客户端提交的数据来进行验证。
7.如果ActionErrors对象中有错误信息,在JSP中需要使用<html:errors>标签显示错误信息。
8. Struts实际上是将ActionErrors对象以org.apache.struts.action.ERROR作为键值保存在了request的 属性中。因此,<html:errors>标签实际上是从request的属性中获得的错误信息描述。如我们也可以通过如下的Java代码来 获得produceID属性的错误描述信息:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><%
java
.util.Iterator<org.apache.struts.action.ActionMessage>it=
((org.apache.struts.action.ActionErrors)request
.getAttribute("org.apache.struts.action.ERROR")).get("productID");
out.println(((org.apache.struts.util.PropertyMessageResources)request
.getAttribute("org.apache.struts.action.MESSAGE")).getMessage("error.productID.blank",null));
%>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

【第4步】建立Java属性文件

Java属性文件相当于资源文件,以key = value形式保存了在程序中需要的字符串信息。Java属性文件的扩展名为properties。在<samples工程目录>"src目录中建立一个struts目录,在struts目录中建立一个ErrorDescription.properties文件,并输入如下的内容:

ErrorDescription.properties

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->error.user.blank=Usercan'tbenull.
error.password.blank
=Passwordcan'tbenull.
error.password1.confirmation
=Passworddoesn'tmatchconfirmation.
error.email.blank
=Emailcan'tbenull.
error.email.invalid
=Itisnotavalidemailaddress.

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

【第5步】配置struts-config.xml文件

在本例中需要配置struts-config.xml文件的三个标签:<form-bean><action><message-resources>
1.
配置<form-bean>标签

这个标签用来定义ActionForm。在<form-beans>标签中加入如下所示的<form-bean>标签:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><form-beanname="simpleValidationForm"type="actionform.SimpleValidationForm"/>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->2.配置<action>标签

这个标签用来定义Struts中的动作类。在<action-mappings>标签中加入如下所示的<action>标签:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><actionname="simpleValidationForm"path="/simpleValidation" scope="request"type="action.SimpleValidationAction"
input
="simpleValidation.jsp">
<forwardname="simple"path="simpleValidation.jsp"/>
</action>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

<action>标签中的属性含义描述如下:

1. name:表示ActionForm的名称。也就是<form-bean>标签中的name属性的值。

2. path:表示Struts动作,必须以“/”开头。

3. scope:表示ActionForm类的对象实例(在本例中是SimpleValidationForm类的对

象实例)保存的范围。这个属性值只能取requestsession。默认值是session。如果scope的值为request,表示将SimpleValidationForm类的对象实例以simpleValidationForm作为键值保存到了request的属性中。如果scope的值为session,表示不将SimpleValidationForm类的对象实例保存到request的属性中。但不管scope的值是request还是sessionStruts都会将SimpleValidationForm类的对象实例保存到session的属性中。

4. type:表示SimpleValidationAction类的全名。

5. input:表示如果客户端提交的数据未通过简单验证后要跳转到的页面,也就是在

SimpleValidationForm类的validate方法中返回的ActionErrors对象中含有错误描述信息。Struts会自动跳转到input属性所指的JSP页面。

<action>标签中的子标签<forward>定义了可以在动作类(在本例中就是SimpleValidationAction类)中读取的forward页的URL

2. 配置<message-resources>标签

这个标签用来定义程序中所使用的属性文件。在struts-config.xml文件的<struts-config>根节点中加入如下内容:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><message-resourcesparameter="struts.ErrorDescription"/>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

其中parameter属性表示属性文件的路径,在本例中属性文件ErrorDescription.properties位于struts目录中,因此,parameter属性的值为struts.ErrorDescription。扩展名properties不用写。其中“.”也可以使用“/”或“"”代替。

下面我们测试一下这个例子程序。首先启动Tomcat,然后在IE中输入如下的URL

http://localhost:8080/samples/simpleValidation.jsp

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

访问上面的URL后的界面如图1所示。



图1
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->在不输入任何信息的情况下,点击“确定”按钮后的界面如图2所示。



图2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics