在maping之前,会先自动调用validate方法,你可以在validate方法中加入你的validate field,返回error object但logic 上的validate在logic bean上做,如果只是限于validate length or format,倒不如用strut 1.1中的validate.xml等控制,既easy也快

解决方案 »

  1.   

    使用validator的页面验证
    1. 在struts-config.xml文件中加入validator插件,加入这个插件后你的应用就具别的使用validator的环境,但是,要想真正的使用它还需要其他的配置。如:
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" property="pathnames" />
      </plug-in>
    2. 在struts-config.xml文件中添加ApplicationResources配置文件。如:
    <message-resources parameter="com.dc.sibss.om.struts.ApplicationResources" />
    添加此文件的作用是提供错误信息的非编程定制化和多语言支持。如果我们使用中文平台操作系统,则默认情况下将首先查找ApplicationResource_zh_CN.properties文件,然后是ApplicationResources_zh.properties,如果前两个文件没有被找到则将查找ApplicationResources.properties文件。
    为了能够在页面上显示错误提示信息,我们还需要将以下内容添加到ApplicationResources.properties文件的末尾:
    errors.required={0} is required.
    errors.minlength={0} cannot be less than {1} characters.
    errors.maxlength={0} cannot be greater than {2} characters.
    errors.invalid={0} is invalid.
    errors.byte={0} must be an byte.
    errors.short={0} must be an short.
    errors.integer={0} must be an integer.
    errors.long={0} must be an long.
    errors.float={0} must be an float.
    errors.double={0} must be an double.
    errors.date={0} is not a date.
    errors.range={0} is not in the range {1} through {2}.
    errors.creditcard={0} is not a valid credit card number.
    errors.email={0} is an invalid e-mail address.
    以上仅是struts现在支持的错误类型的错误提示信息,如果你自定义了新类型的错误验证,则还需要在此加上你自己的内容。
    以上内容中的{0}指的是错误提交的参数。比如:当你需要页面上的“用户名”不能为空时(也就是上面的errors.required),这个{0}就代表“用户名”,所以如果你没有填写用户名将抛出如下错误:
    用户名 is required.(你可以根据需要修改称中文)
    我们可能已经注意到了,既然错误提示信息需要配置,那么上例中“用户名”系统是如何得到的呢?没错!也是通过修改此配置文件,内容如下:
    visitCust.error.name.required=<br>用户名
    这样当“用户名”为空时,struts后台程序将联合以上两处定义显示错误信息。
    另外,上面的“visitCust.error.name.required”是在Validation.xml配置验证内容时指定的。具体见以下介绍。
    注意:一般情况下,你的系统只需要一个ApplicationResources文件,所以开发组的成员不要添加自己的resource文件。只有在你的项目分组开发时才需要使用多个ApplicationResources文件,但是,同时你的struts-config.xml文件也会有相同的数量对应。
    3. 创建你的ActionForm并让它继承org.apache.struts.validator.ValidatorForm类。
    4. 创建你的Action实现,并和上面定义的ActionForm关联。这里需要注意的是,在定义此Action时一定将validate属性设置为true,并且在你定义的ActionForm中不要实现它的validate方法――这就意味着你将使用ValidatorForm的validate方法,这样才能保证你的错误验证正常进行。
    5. 配置validation.xml文件。基本内容如下:
    <form-validation>
    <!-- ========== Default Language Form Definitions ===================== -->
        <formset>
    <form name="custGNewForm">需要验证页面上form的名字
    <field  property="certifiCode"需要校验的属性
    depends="required,maxlength">校验内容
    <arg0 key="prompt.certifiCode"/>ApplicationResource文件中对应
    <arg1 key="${var:maxlength}" name="maxlength" resouce="false"/>
    <var>确定最长限制的长度
    <var-name>maxlength</var-name>
    <var-value>20</var-value>
    </var>
    </field>
    注意:此处的arg0和arg1就代表了ApplicationResources文件中使用“{}”括起来的参数。比如:
    errors.range={0} is not in the range {1} through {2}.
    定义了三个参数,所以你这里也要定义<arg0>、<arg1>、<arg2>三个参数才能完整的显示错误信息。
    errors.maxlength={0} cannot be greater than {2} characters.
    定义了0、2两个参数,所以你就需要定义<arg0>和<arg2>两个参数。
    <field  property="userName"
    depends="required,maxlength">
    <arg0 key="prompt.userName"/>
    <arg2 key="${var:maxlength}" name="maxlength" resouce="false"/>
    <var>
    <var-name>maxlength</var-name>
    <var-value>80</var-value>
    </var>
    </field>
    <field  property="email"
                        depends="email">
                    <arg0 key="prompt.email"/>
                </field>
    </form>

    <form name="custGNewCheckForm">
    <field property="certifiCode"
    depends="required">
    <arg0 key="prompt.certifiCode"/>
    </field>
    </form>
        </formset>
    </form-validation>
    6. 在校验页面的<body>前添加如下内容:
    <html:errors/>
    就用这个方法吧。
      

  2.   

    1.02的话,记得在struts-config.xml中将要校验的那个action的validate设置成为true
      

  3.   

    squallzeng(小呆呆) :
    1.0也可以用xml来验证的吗?我记得好象1.1才可以