Struts2表单字段验证不起作用

时间:2016-02-29 18:08:15

标签: java validation configuration struts2 struts-validation

我尝试按照here(struts2文档)所述实现验证。我的班级:

public class FatturaAction extends BaseAction {

    private static final long serialVersionUID = 6586322371651933659L;
    private FatturaForm fatturaForm;

    //getter and setter
}

FatturaForm

public class FatturaForm {

    private Date data;

    //getter and setter
}

我的XML(FatturaAction-validaton.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <field name="fatturaForm.data"> 
        <field-validator type="required"> 
            <message>You cannot leave the email address field empty.</message>
        </field-validator>
    </field>
</validators>

即使字段为空,也不会触发验证。我使用<interceptor-ref name="defaultStack" />因此激活了验证拦截器。 我错过了什么?

编辑:

我也试过non field validator

<validators>
    <validator type="required"> 
        <param name="fieldName">data</param> 
        <message>You must enter a value for bar.</message> 
    </validator>
</validators>

它也没有击中。

1 个答案:

答案 0 :(得分:1)

您使用了错误的DTD:

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" 
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">

最新版Struts 2的正确版本是

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
          "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

同时确保您的 FatturaAction-validaton.xml 位于操作文件夹中,而不是 bean 文件夹中(右侧)仅适用于访客验证)