花费时间找,但找到的几个地方的资料讲解均不是很系统,因此在这里大略介绍struts validator的使用.我所使用的是struts 1.3.8
首先要配置好struts-config.xml,如下:
上面59行至64行,增加validator的功能.第12行,设置form,第36行至45行,设置action,由于validator主要的使用不涉及form,action,因此这里不做详细介绍,要提的是form需要继承org.apache.struts.validator.ValidatorForm,
/WEB-INF/validation.xml文件介绍,这个文件主是要针对每个字段设置验证规则,
容易理解的不做介绍,第11行depends=required的意思当然是该字段是必需的,不能为空,所在资源文件里的相应的错误信息是这样的"errors.required={0} is required.",接下来的第12行,就是定义{0}参数是用什么替代,用的是也是资源文件里的"prompt.username=\u5e10\u53f7"
资源文件
最后,要在jsp里引用错误提示信息:
将这一段代码放在body后面即可.
参考资源:
http://struts.apache.org/1.3.8/faqs/validator.html
http://struts.apache.org/1.3.8/userGuide/building_view.html#validator
struts-examples-1.3.8示例里有validator的例子
首先要配置好struts-config.xml,如下:
xml 代码
- xml version="1.0" encoding="ISO-8859-1" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://struts.apache.org/dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- <form-bean name="validationForm" type="com.ibm.struts.validation.ValidationForm"/>
- <form-beans>
- <global-forwards>
- <forward
- name="welcome"
- path="/Welcome.do"/>
- global-forwards>
- <action-mappings>
- <action
- path="/Welcome"
- forward="/pages/Welcome.jsp"/>
- <action path="/TestSimpleValidation"
- forward="/pages/test_validation.jsp" />
- <action path="/SubmitValid"
- type="com.ibm.struts.validation.ValidationAction"
- name="validationForm"
- scope="request"
- validate="true"
- input="/pages/test_validation.jsp">
- <forward name="success" path="/pages/success.jsp" redirect="true"/>
- <forward name="failure" path="/pages/test_validation.jsp"
- redirect="true" />
- action>
- action-mappings>
- <message-resources parameter="MessageResources" >
- message-resources>
- <message-resources key="zh_CN" parameter="MessageResources_zh_CN" />
- <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
- <set-property
- property="pathnames"
- value="/org/apache/struts/validator/validator-rules.xml,
- /WEB-INF/validation.xml"/>
- plug-in>
- struts-config>
/WEB-INF/validation.xml文件介绍,这个文件主是要针对每个字段设置验证规则,
xml 代码
- xml version="1.0" encoding="ISO-8859-1" ?>
- "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
- "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
- <form-validation>
- <formset>
- <form name="validationForm">
- <field property="username" depends="required">
- <arg key="prompt.username"/>
- field>
- <field
- property="password"
- depends="required,mask">
- <arg key="prompt.password"/>
- <var>
- <var-name>maskvar-name>
- <var-value>^t[0-9]n$var-value>
- var>
- field>
- form>
- formset>
- form-validation>
资源文件
xml 代码
- # -- standard errors --
- errors.header=validation error message
- errors.prefix=<LI>
- errors.suffix=LI>
- errors.footer=UL>
- # -- validator --
- errors.invalid={0} is invalid.
- errors.maxlength={0} can not be greater than {1} characters.
- errors.minlength={0} can not be less than {1} characters.
- errors.range={0} is not in the range {1} through {2}.
- errors.required={0} is required.
- errors.byte={0} must be an byte.
- errors.date={0} is not a date.
- errors.double={0} must be an double.
- errors.float={0} must be an float.
- errors.integer={0} must be an integer.
- errors.long={0} must be an long.
- errors.short={0} must be an short.
- errors.creditcard={0} is not a valid credit card number.
- errors.email={0} is an invalid e-mail address.
- # -- other --
- errors.cancel=Operation cancelled.
- errors.detail={0}
- errors.general=The process did not complete. Details should follow.
- errors.token=Request could not be completed. Operation is not in sequence.
- # -- welcome --
- welcome.title=Struts Blank Application
- welcome.heading=Welcome!
- welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the MessageResources.properties file with this message in the /WEB-INF/src folder.)
- valid.title=\u7b80\u5355\u9a8c\u8bc1
- prompt.username=\u5e10\u53f7
- prompt.password=\u5bc6\u7801
- prompt.phone=\u7535\u8bdd\u53f7\u7801
- prompt.email=E-Mail
- prompt.url=URL
最后,要在jsp里引用错误提示信息:
xml 代码
- <logic:messagesPresent>
- <bean:message key="errors.header"/>
- <ul>
- <html:messages id="error">
- <li><bean:write name="error"/></li>
- </html:messages>
- </ul><hr />
- </logic:messagesPresent>
参考资源:
http://struts.apache.org/1.3.8/faqs/validator.html
http://struts.apache.org/1.3.8/userGuide/building_view.html#validator
struts-examples-1.3.8示例里有validator的例子