0
点赞
收藏
分享

微信扫一扫

struts1.x简单的数据验证(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方法的定义如下:



public



 

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

【第1步】建立JSP页面

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



 <% @ page pageEncoding = " GBK " %>
   <% @ taglib uri = " http://struts.apache.org/tags-html "  prefix = " html " %>
   < html >
       < head >
           < title >注册信息(测试简单验证) </ title >
           < style  type ="text/css" >
  .text  {
      height :  20px ;
      width :  160px ;
   }
   </ style >
       </ head >
       < body >
           < html:form  action ="simpleValidation" >
               < table  width ="100%" >
                   < tr >
                       < td  align ="right"  width ="45%" > 用户名: </ td >
                       < td  width ="55%" >
                           < html:text  property ="user"  styleClass ="text"   />
                           < font  color ="red" >< html:errors  property ="errorUser"   /></ font >
                       </ td >
                   </ tr >< tr  />< tr  />
                   < tr >
                       < td  align ="right" >登录密码: </ td >
                       < td >
                           < html:password  property ="password"  styleClass ="text"   />
                           < font  color ="red" >< html:errors  property ="errorPassword"   /></ font >
                       </ td >
                   </ tr >< tr  />< tr  />
                   < tr >
                       < td  align ="right" >重复登录密码: </ td >
                       < td >
                           < html:password  property ="password1"  styleClass ="text"   />
                           < font  color ="red" >< html:errors  property ="errorPassword1"   /></ font >
                       </ td >
                   </ tr >< tr  />< tr  />
                   < tr >
                       < td  align ="right" >电子邮件: </ td >
                       < td >
                           < html:text  property ="email"  styleClass ="text"   />
                           < font  color ="red" >< html:errors  property ="errorEmail"   /></ font >
                       </ td >
                   </ tr >< tr  />< tr  />
                   < tr >
                       < td  align ="right" >  < br > ${requestScope.success }  </ td >
                       < td  align ="left" >  < br >  < html:submit  value =" 提交 "   />  </ td >
                   </ tr >
               </ table >
           </ html:form >
       </ body >
   </ html >


 

 在启动Tomcat后,在IE的地址栏中输入如下的URL:

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

当通过上面的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.       跳转到目标页。

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


package action;
  
  
  
  
  import javax.servlet.http.*;
  
  
  import org.apache.struts.action.*;
  
  
  
  
  public 
  class SimpleValidationAction 
  extends Action
  
  {
  
      
  public ActionForward execute(ActionMapping mapping, ActionForm form,
  
              HttpServletRequest request, HttpServletResponse response)
  
              
  throws Exception
  
      {        
  
          request.setAttribute("success", "提交成功!");  
  //
   设置在目标页中显示的信息字符串
  
          
  return mapping.findForward("simple");  
  //
   跳转到目录页(simple所指的JSP页)
  
      }
  
  }


【第3步】建立ActionForm类
    在这一步我们来建立一个用于接收有户的提交信息的ActionForm类:SimpleValidationForm。这个类从org.apache.struts.action.ActionForm类继承。在<samples工程目录>"src"actionform目录中建立一个SimpleValidationForm.java文件,代码如下:

package actionform;
  
  
  
  
  import javax.servlet.http.HttpServletRequest;
  
  
  import org.apache.struts.action.*;
  

  
  public 
  class SimpleValidationForm 
  extends ActionForm
  
  {
  
      
  //
   以下四个变量分别对应于simpleValidation.jsp中的四个文本框中的值。
  
      
  private String user;    
  
      
  private String password;
  
      
  private String password1;
  
      
  private String email;
  
  
  
      
  public String getUser()  
  
      {
  
          
  return user;
  
      }
  
      
  public 
  void setUser(String user)
  
      {
  
          
  this.user = user;
  
      }
  
      
  public String getPassword()
  
      {
  
          
  return password;
  
      }
  
      
  public 
  void setPassword(String password)
  
      {
  
          
  this.password = password;
  
      }
  
      
  public String getPassword1()
  
      {
  
          
  return password1;
  
      }  
  
      
  public 
  void setPassword1(String password1)
  
      {
  
          
  this.password1 = password1;
  
      }
  
      
  public String getEmail()
  
      {
  
          
  return email;
  
      }
  
      
  public 
  void setEmail(String email)
  
      {
  
          
  this.email = email;
  
      }  
  
      
  //
   开始验证用户提交的信息 
  
      
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
  
      {
  
          ActionErrors error = 
  new ActionErrors();
  
          
  if (user.equals(""))   
  //
   必须输入用户名
  
              error.add("errorUser", 
  new ActionError("error.user.blank"));
  
          
  if (password.equals(""))  
  //
   必须输入密码
  
              error.add("errorPassword", 
  new ActionError("error.password.blank"));
  
          
  else 
  if (!password.equals(password1))  
  //
   两个登录密码必须一致
  
              error.add("errorPassword1", 
  new ActionError("error.password1.confirmation"));
  
          
  if (email.equals(""))  
  //
   必须输入email
  
              error.add("errorEmail", 
  new ActionError("error.email.blank"));
  
          
  else 
  if (!email.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+"))  
  //
   验证email的格式是否正确
  
              error.add("errorEmail", 
  new ActionMessage("error.email.invalid"));
  
  
  
           //
   返回错误信息,如果error中没有错误信息,

             //
   就会调用SimpleValidationAction类的对象实例来执行execute方法。
  
          
  return error;        
  
      }
  
  }

 

在编写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方法的第二个参数request的getParameter方法直接获得客户端提交的数据来进行验证。
    7. 如果ActionErrors对象中有错误信息,在JSP中需要使用<html:errors>标签显示错误信息。
   8. Struts实际上是将ActionErrors对象以org.apache.struts.action.ERROR作为键值保存在了request的属性中。因此,<html:errors>标签实际上是从request的属性中获得的错误信息描述。如我们也可以通过如下的Java代码来获得produceID属性的错误描述信息:



<
  % 
    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));
  
  %>



 

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

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


error.user.blank = User can't be null.
  
  error.password.blank = Password can't be null.
  
  error.password1.confirmation = Password doesn't match confirmation.
  
  error.email.blank = Email can't be null.
  
  error.email.invalid = It is not a valid email address.

 

 

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

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

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



<
  form-bean 
  name
  ="simpleValidationForm"
   type
  ="actionform.SimpleValidationForm"
   
  />


 

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

<
  action 
  name
  ="simpleValidationForm"
   path
  ="/simpleValidation"
   scope
  ="request"
   type
  ="action.SimpleValidationAction"
  
               input
  ="simpleValidation.jsp"
  >
  
      <
  forward 
  name
  ="simple"
   path
  ="simpleValidation.jsp"
   
  />
  
  </
  action
  >


 

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

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

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

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

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

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

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

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

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

2.       配置<message-resources>标签

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



<
  message-resources 
  parameter
  ="struts.ErrorDescription"
   
  />



 

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

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

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

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




struts1.x简单的数据验证(2)_数据



图1


 在不输入任何信息的情况下,点击“确定”按钮后的界面如图2所示。



struts1.x简单的数据验证(2)_数据_02


举报

相关推荐

0 条评论