Struts1.2中的validation验证框架的实现步骤(转帖)
步骤:
1环境配置:首先要为struts加入Validator支持
lib包:加入validator所需的jar包
配置文件: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" /> </plug-in>
这里所需的validator-rules.xml和validation.xml也需要放到/WEB-INF/下
2配置actionForm
2.1修改Form基类:将actionForm继承的基类改为ValidatorForm
2.2配置struts-config.xml文件:把要验证的form的validator属性设为true
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="userForm" type="com.satesoft.test.UserForm" /> </form-beans> <action-mappings> <action input="/index.jsp" name="userForm" path="/show" scope="request" type="com.satesoft.test.ShowAction" validate="true"> <forward name="forward" path="/show.jsp" /> </action> </action-mappings> <message-resources parameter="ApplicationResources" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> </plug-in> </struts-config>
注意:一定要配置对应的Action里的input属性,否则struts找不到要返回的路径
2.3配置validator.xml文件:在validation.xml里加入对要验证的actionform的定义
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd"> <form-validation> <formset> <form name="userForm"> <field property="name" depends="required"> <arg0 key="user.name.required"/> </field> </form> </formset> </form-validation>
这里同时要配置的还有消息资源。在资源文件中加入上述配置文件中所需的消息
errors.header=<H3><font color="blue">errors</font><br> errors.footer=<p></p> errors.required={0} is required. errors.minlength={0} cannot be less than {1} characters. errors.maxlength={0} cannot 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 not a valid credit card number. errors.email={0} is an invalid email address user.name.required=user name
2.4配置作为input的jsp页面:
<%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html> <head> <title> index </title> </head> <body bgcolor="#ffffff"> <html:form action="show.do" method="post"> <html:text property="name"/> <html:submit title="确定"/> <html:cancel title="取消"> </html:cancel> </html:form> <html:errors/> </body> </html>
如果需要加入javascript支持,则:
<%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html> <head> <title> index </title> </head> <body bgcolor="#ffffff"> <html:form action="show.do" method="post" onsubmit="return validateUserForm(this);"> <html:text property="name"/> <html:submit title="确定"/> <html:cancel title="取消"> </html:cancel> </html:form> <html:javascript formName="userForm"/> </body> </html>
注意:onsubmit里的validatorUserForm要和下面的formName里的名字一致,只不过首字母大写
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01