中的“3.9.3 结合commons-validator.jar”一节的内容
书写validation.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="tsetForm">
<field property="test" depends="required">
<arg key="testForm.test" position="0" />
</field>
</form>
</formset>
</form-validation>
struts-config.xml文件追加以下内容
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml" />
<set-property property="stopOnFirstError" value="true" />
</plug-in>
添加applicationResources.properties文件内容如下
testForm.test=test
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
jsp页面添加以下内容
<logic:messagesPresent>
<ul>
<html:messages id="error">
<li>
<bean:write name="error"/>
</li>
</html:messages>
</ul>
</logic:messagesPresent>
对应的ActionForm也已经修改为了validatorForm
public class TestForm extends ValidatorForm {
private String test;
}
另外对应actionmapping中的form也已经添加了validate="true"的项目
以上这些步骤完全参照了书上的6个步骤
但是运行没有报错
下面的是我的action的execute方法
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestForm testForm = (TestForm) form;
String test = testForm.getTest();
if(testDAO.isValidUser(test)){
return mapping.findForward("backToindex");
}else{
return mapping.getInputForward();
}
我单步跟了一下运行到String test = testForm.getTest();时并没有停下来
而且testForm的validatorResults的值是null
请问一下我是不是那里配置错了?
万分感谢