validation流程:
在struts-config.xml注册validation
<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里要生成基本的类的时候才可以读取validation插件
2.在validation.xml里写条件
配置文件:
errors.required={0} can not be blank!
errors.minlength={0} can not less than {1} charactors
errors.maxlength={0} can not be greater than {2} characters.
就可以进行变淡验证!
JSP:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>register.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form method="post" action="register.do" name="registerForm">
<p>
姓名:
<input type="text" name="username"><html:errors property="username"/>
</p>
<p>
密码:
<input type="text" name="password"><html:errors property="password"/>
</p>
<p>
重复密码:
<input type="text" name="repassword">
</p>
<p>
年龄:
<input type="text" name="age">
</p>
<p>
<input type="submit" value="register" name="okBtn">
<br>
</p>
</form>
</body>
</html:html>
struts-config.xml: