要用validator验证框架首先要在WEB-INF文件夹下建立一个名为validation.xml(必须为此名)的文件。
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
>
<
global
>
<
constant
>
<
constant-name
>
phone
</
constant-name
>
<
constant-value
>
^/d{8}$
</
constant-value
>
</
constant
>
</
global
>
<
formset
>
<
form
name
=
"userForm"
>
<
field
property
=
"userName"
depends
=
"required"
>
<!-- field
中
depends
表示
validator-rulers.xml
中以定义好的方法
-->
<
msg
name
=
"required"
key
=
"userName"
resource
=
"true"
/>
<!-- msg
中
resource
表示是否从
properties
文件中读取提示信息
-->
</
field
>
<
field
property
=
"userPwd"
depends
=
"required,minlength"
>
<
msg
name
=
"required"
key
=
"mima bu neng wei kong !"
resource
=
"false"
/>
<
msg
name
=
"minlength"
key
=
"chang du bu neng xiao yu 5!"
resource
=
"false"
/>
<
var
>
<
var-name
>
minlength
</
var-name
>
<
var-value
>
5
</
var-value
>
</
var
>
</
field
>
<
field
property
=
"userPwd"
depends
=
"required"
>
<
msg
name
=
"required"
key
=
"mima bu neng wei kong !"
resource
=
"false"
/>
</
field
>
</
form
>
</
formset
>
</
form-validation
>
Jsp页面
<%@
page
language
=
"java"
pageEncoding
=
"gb2312"
%>
<%@
taglib
uri
=
"http://jakarta.apache.org/struts/tags-bean"
prefix
=
"bean"
%>
<%@
taglib
uri
=
"http://jakarta.apache.org/struts/tags-html"
prefix
=
"html"
%>
<
html
>
<
head
>
<
title
>
JSP for UserForm form
</
title
>
</
head
>
<
body
>
<%-- <html:form action="/user">--%>
<%-- userName : <html:text property="userName"/><html:errors property="userName"/><br/>--%>
<%-- userPwd : <html:text property="userPwd"/><html:errors property="userPwd"/><br/>--%>
<%-- <html:submit/><html:cancel/>--%>
<%-- </html:form>--%>
<
html:form
action
=
"/user"
>
userName :
<
html:text
property
=
"userName"
/><
html:errors
property
=
"userName"
/><
br
/>
userPwd :
<
html:text
property
=
"userPwd"
/><
html:errors
property
=
"userPwd"
/><
br
/>
<
html:submit
/><
html:cancel
/>
</
html:form
>
</
body
>
</
html
>
ApplicationResources.properties文件
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project ValidateTest
userName=
yong
hu
ming
bu
neng
wei
kong
!