08-01
28
struts validatior实现、问题及解决办法总结
作者:Java伴侣 日期:2008-01-28
在register模块中加入validator,没想到这个本以为简单的玩意,竟然出现问题,而且几经挫折,才搞好。估计大家也会遇到类似的问题,故总结一下遇到的问题及自己的解决办法。
先谈大致的实现过程
1、在web-inf目录下添加validation.xml和validator-rule.xml
validation.xml文件如下:
validator-rule.xml文件去网上下一个就可以了。
2、修改struts-config.xml,在最后面加入plug-in
3、修改struts-config.xml中的form-bean registerForm的type为org.apache.struts.validator.DynaValidatorForm
<form-beans>
<form-bean name="userInfoForm" type="forms.UserInfoForm"/>
<form-bean name="registerForm" type="forms.RegisterForm">
</form-bean>
</form-beans>
修改为
<form-beans>
<form-bean name="userInfoForm" type="forms.UserInfoForm"/>
<form-bean name="registerForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
<form-property name="email" type="java.lang.String"/>
<form-property name="question" type="java.lang.String"/>
<form-property name="answer" type="java.lang.String"/>
</form-bean>
</form-beans>
4、修改aCTion的validate参数为true
<action attribute="registerForm"
path="/register"
input="/register.jsp"
type="org.springframework.web.struts.DelegatingActionProxy"
name="registerForm"
scope="session"
validate="true">
<forward name="success" path="/success.html"/>
</action>
5、修改registerForm.java
原来
public class RegisterForm extends ActionForm
修改为
public class RegisterForm extends DynaValidatorForm
再总结问题及解决办法
1、问题1——Document root element "form-validation", must match DOCTYPE root "null"
2007-12-1 15:15:11 org.apache.commons.digester.Digester error
严重: Parse Error at line 39 column 17: Document root element "form-validation", must match DOCTYPE root "null".
org.xml.sax.SAXParseException: Document root element "form-validation", must match DOCTYPE root "null".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
原因: web-inf下的validation.xml文件头部没有文档说明,就是下面这一段:
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd>
加上这一段就可以解决这个问题。
我使用的是struts-2.0.11,这里的validator_1_0.dtd可以在strut的lib目录下commons-validator-1.3.0.jar中找到,将commons-validator-1.3.0.jar解压缩,commons-validator-1.3.0\org\apache\commons\validator\resources中可以找到strut支持的所有dtd的版本号。
我这里有如下的版本号:
validator_1_0.dtd
validator_1_0_1.dtd
validator_1_1.dtd
validator_1_1_3.dtd
validator_1_2_0.dtd
validator_1_3_0.dtd
每个版本的dtd有所不同。
2、问题2——The content of element type "field" must match "(msg|arg|var)*".
运行后,eclipse的console中报错:
2007-12-1 15:15:11 org.apache.commons.digester.Digester error
严重: Parse Error at line 39 column 17: Document is invalid: no grammar found.
org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
2007-12-1 15:33:13 org.apache.commons.digester.Digester error
严重: Parse Error at line 18 column 33: Element type "arg0" must be declared.
org.xml.sax.SAXParseException: Element type "arg0" must be declared.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
2007-12-1 15:33:13 org.apache.commons.digester.Digester error
严重: Parse Error at line 28 column 11: The content of element type "field" must match "(msg|arg|var)*".
org.xml.sax.SAXParseException: The content of element type "field" must match "(msg|arg|var)*".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
原因:这3个错误其实是因为一个原因引起的,那就是arg0的问题,不是所有的dtd版本都支持arg0参数。
仔细研究commons-validator-1.3.0\org\apache\commons\validator\resources中下的所有dtd的版本号,会发现,各版本关于filed中参数的定义不同。
在validator_1_1_3.dtd之前的版本中,如下:
<!ELEMENT field (msg|arg|arg0|arg1|arg2|arg3|var)*>
<!ATTLIST field property CDATA #REQUIRED>
<!ATTLIST field depends CDATA #IMPLIED>
<!ATTLIST field page CDATA #IMPLIED>
<!ATTLIST field indexedListProperty CDATA #IMPLIED>
在之后的版本如下:
<!ELEMENT field (msg|arg|var)*>
<!ATTLIST field property CDATA #REQUIRED>
<!ATTLIST field depends CDATA #IMPLIED>
<!ATTLIST field page CDATA #IMPLIED>
<!ATTLIST field indexedListProperty CDATA #IMPLIED>
可见不是所有的dtd版本都支持arg0参数的。
而且仔细阅读validator_1_1_3.dtd发现,里面有关于这个的说明:
Since 1.2.0 position is ignored for ''default'' arguments (i.e. those with no ''name'' supplied) - it is calculated automatically from the order the arguments are added in.
For ''overriden'' arguments (i.e. those with a ''name'') it is optional. If ''position'' is not supplied it will use the position of the ''default'' argument it follows.
让人奇怪的是:即使采用支持arg0参数的validator_1_1_3.dtd或是之前的版本,仍然报此错误。不知为何。先不管了,将validation.xml中的arg0,arg1统一改为arg。
先谈大致的实现过程
1、在web-inf目录下添加validation.xml和validator-rule.xml
validation.xml文件如下:
复制内容到剪贴板 程序代码
<form-validation>
<global>
<constant>
<constant-name>name_common</constant-name>
<constant-value>^[0-9a-zA-Z]*$</constant-value>
</constant>
</global>
<formset>
<form name="registerForm">
<field property="username" depends="required,mask,maxlength">
<arg0 key="username" resource="false" />
<arg1 name="maxlength" key="${var:maxlength}" resource="false" />
<var>
<var-name>mask</var-name>
<var-value>${name_common}</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
</field>
<field property="password" depends="required,mask,maxlength">
<arg0 key="password" resource="false" />
<arg1 name="maxlength" key="${var:maxlength}" resource="false" />
<var>
<var-name>mask</var-name>
<var-value>${name_common}</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
<global>
<constant>
<constant-name>name_common</constant-name>
<constant-value>^[0-9a-zA-Z]*$</constant-value>
</constant>
</global>
<formset>
<form name="registerForm">
<field property="username" depends="required,mask,maxlength">
<arg0 key="username" resource="false" />
<arg1 name="maxlength" key="${var:maxlength}" resource="false" />
<var>
<var-name>mask</var-name>
<var-value>${name_common}</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
</field>
<field property="password" depends="required,mask,maxlength">
<arg0 key="password" resource="false" />
<arg1 name="maxlength" key="${var:maxlength}" resource="false" />
<var>
<var-name>mask</var-name>
<var-value>${name_common}</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
validator-rule.xml文件去网上下一个就可以了。
2、修改struts-config.xml,在最后面加入plug-in
复制内容到剪贴板 程序代码
<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="false"/>
</plug-in>
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="false"/>
</plug-in>
3、修改struts-config.xml中的form-bean registerForm的type为org.apache.struts.validator.DynaValidatorForm
<form-beans>
<form-bean name="userInfoForm" type="forms.UserInfoForm"/>
<form-bean name="registerForm" type="forms.RegisterForm">
</form-bean>
</form-beans>
修改为
<form-beans>
<form-bean name="userInfoForm" type="forms.UserInfoForm"/>
<form-bean name="registerForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
<form-property name="email" type="java.lang.String"/>
<form-property name="question" type="java.lang.String"/>
<form-property name="answer" type="java.lang.String"/>
</form-bean>
</form-beans>
4、修改aCTion的validate参数为true
<action attribute="registerForm"
path="/register"
input="/register.jsp"
type="org.springframework.web.struts.DelegatingActionProxy"
name="registerForm"
scope="session"
validate="true">
<forward name="success" path="/success.html"/>
</action>
5、修改registerForm.java
原来
public class RegisterForm extends ActionForm
修改为
public class RegisterForm extends DynaValidatorForm
再总结问题及解决办法
1、问题1——Document root element "form-validation", must match DOCTYPE root "null"
2007-12-1 15:15:11 org.apache.commons.digester.Digester error
严重: Parse Error at line 39 column 17: Document root element "form-validation", must match DOCTYPE root "null".
org.xml.sax.SAXParseException: Document root element "form-validation", must match DOCTYPE root "null".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
原因: web-inf下的validation.xml文件头部没有文档说明,就是下面这一段:
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd>
加上这一段就可以解决这个问题。
我使用的是struts-2.0.11,这里的validator_1_0.dtd可以在strut的lib目录下commons-validator-1.3.0.jar中找到,将commons-validator-1.3.0.jar解压缩,commons-validator-1.3.0\org\apache\commons\validator\resources中可以找到strut支持的所有dtd的版本号。
我这里有如下的版本号:
validator_1_0.dtd
validator_1_0_1.dtd
validator_1_1.dtd
validator_1_1_3.dtd
validator_1_2_0.dtd
validator_1_3_0.dtd
每个版本的dtd有所不同。
2、问题2——The content of element type "field" must match "(msg|arg|var)*".
运行后,eclipse的console中报错:
2007-12-1 15:15:11 org.apache.commons.digester.Digester error
严重: Parse Error at line 39 column 17: Document is invalid: no grammar found.
org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
2007-12-1 15:33:13 org.apache.commons.digester.Digester error
严重: Parse Error at line 18 column 33: Element type "arg0" must be declared.
org.xml.sax.SAXParseException: Element type "arg0" must be declared.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
2007-12-1 15:33:13 org.apache.commons.digester.Digester error
严重: Parse Error at line 28 column 11: The content of element type "field" must match "(msg|arg|var)*".
org.xml.sax.SAXParseException: The content of element type "field" must match "(msg|arg|var)*".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
原因:这3个错误其实是因为一个原因引起的,那就是arg0的问题,不是所有的dtd版本都支持arg0参数。
仔细研究commons-validator-1.3.0\org\apache\commons\validator\resources中下的所有dtd的版本号,会发现,各版本关于filed中参数的定义不同。
在validator_1_1_3.dtd之前的版本中,如下:
<!ELEMENT field (msg|arg|arg0|arg1|arg2|arg3|var)*>
<!ATTLIST field property CDATA #REQUIRED>
<!ATTLIST field depends CDATA #IMPLIED>
<!ATTLIST field page CDATA #IMPLIED>
<!ATTLIST field indexedListProperty CDATA #IMPLIED>
在之后的版本如下:
<!ELEMENT field (msg|arg|var)*>
<!ATTLIST field property CDATA #REQUIRED>
<!ATTLIST field depends CDATA #IMPLIED>
<!ATTLIST field page CDATA #IMPLIED>
<!ATTLIST field indexedListProperty CDATA #IMPLIED>
可见不是所有的dtd版本都支持arg0参数的。
而且仔细阅读validator_1_1_3.dtd发现,里面有关于这个的说明:
Since 1.2.0 position is ignored for ''default'' arguments (i.e. those with no ''name'' supplied) - it is calculated automatically from the order the arguments are added in.
For ''overriden'' arguments (i.e. those with a ''name'') it is optional. If ''position'' is not supplied it will use the position of the ''default'' argument it follows.
让人奇怪的是:即使采用支持arg0参数的validator_1_1_3.dtd或是之前的版本,仍然报此错误。不知为何。先不管了,将validation.xml中的arg0,arg1统一改为arg。
评论: 1 | 引用: 0 | 查看次数: 1649
回复唏嘘一世[2010-06-24 01:16 PM | ]
可能要下载最新的validate
发表评论