1 hello world
struts-config.xml
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >
< struts-config >
< data-sources />
< form-beans >
< form-bean name ="loginForm" type ="com.ergal.struts.form.LoginForm" />
</ form-beans >
< global-exceptions />
< global-forwards />
< action-mappings >
< action attribute ="loginForm"
path ="/login"
input ="/login.jsp"
name ="loginForm"
scope ="request"
validate ="true"
type ="com.ergal.struts.action.LoginAction" >
< forward
name ="success"
path ="/haha.jsp" />
</ action >
</ action-mappings >
< message-resources parameter ="com.ergal.struts.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 >
login.jsp
<% ... @ taglib uri="/tags/struts-html" prefix="html" %>
<% ... @ taglib uri="/tags/struts-bean" prefix="bean" %>
< html >
< head >
< title > My JSP 'login.jsp' starting page </ title >
</ head >
< body >
< html:errors />
This is my JSP page. < br >
< html:form action ="/login" method ="post" >
username: < html:text property ="username" ></ html:text >
< html:submit > submit </ html:submit >
</ html:form >
</ body >
</ html >
LoginForm.java
package com.ergal.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class LoginForm extends ValidatorForm
... {
/**//*
* Generated Methods
*/
private String username;
public String getUsername()
...{
return username;
}
public void setUsername(String username)
...{
this.username = username;
}
public void reset(ActionMapping mapping, HttpServletRequest request)
...{
}
}
LoginAction.java
......
validation.xml
<! DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >
< form-validation >
< formset >
< form name ="loginForm" >
< field property ="username" depends ="required" >
< arg key ="label.username" position ="0" />
</ field >
</ form >
</ formset >
</ form-validation >
validator-rules.xml
不变 用现成的
ApplicationResoures.properties
# Project P/validatortest
label.username=Your name
errors.required= {...} {0} is required.
2 配置元素
因为后面还要学习自定义rules所以先不管validator-rules.xml
先看validattion.xml
global 和 constant 结合可以定义一些全局变量
如
< constant >
< constant-name > zip </ constant-name >
< constant-value > ^d{5}d*$ </ constant-value >
</ constant >
</ global >
然后在var里就可以直接这么引用了
< var-name > mask </ var-name >
< var-value > ${zip} </ var-value >
</ var >
formset
有属性language 和 country
元素constant 和 form
里面也可以用constant来像前面那样来声明变量 但是这个变量是局部变量
form
form的name属性 就是名字要和struts的form的bean的定义名字一样
有元素field
field
属性 depends 对应rules property 对应表单的某个属性的名字和ActionForm的变量对应 page对应跨页表单
有元素arg msg
arg
属性name 对应depends里的rules key 对应消息文本 bundle resource是否用原始的定义文本 默认true
position 对应资源文件里的序列{*}的值
msg
属性name 对应depends里的rules key 对应消息文本 bundle resource是否用原始的定义文本 默认true
var
比较简单就是var-name 和var-value 用来设定参数值或者规则的表达式
现在添加一个来测试
首先改成
< form name ="loginForm" >
< field property ="username"
depends ="required,integer,intRange" >
< arg key ="errors.test" />
</ field >
</ form >
</ formset >
什么都不写就写一个arg
在validation.xml里页就只写
errors.test=asdasd
这样输入空的时候显示asdasd is required.
输入不合理的数字就会什么都没有 因为违反了integer和Intrang的规则 但是在这两个规则里的msg的对应的 资源并没有定义 所以会这样
然后加上默认的
errors.integer=it must be integer
这时再测试
输入空显示asdasd is required.
输入字母显示it must be integer
输入其他数字显示int must between {1}and {2}
原因是没有定义var
现在加上 然后改成
< form name ="loginForm" >
< field property ="username"
depends ="required,integer,intRange" >
< arg key ="errors.test" />
< arg name ="required" key ="label.username" position ="0" />
< arg name ="intRange" key ="${var:min}" resource ="false" position ="1" />
< arg name ="intRange" key ="${var:max}" resource ="false" position ="2" />
< var >
< var-name > min </ var-name >
< var-value > 10 </ var-value >
</ var >
< var >
< var-name > max </ var-name >
< var-value > 20 </ var-value >
</ var >
</ field >
</ form >
</ formset >
对应的资源文件为
errors.required= {...} {0} is required.
errors.range=int must between {...} {1} and {...} {2}
errors.integer=it must be integer
errors.test=asdasd
再次测试
所有的都正常了 但是那个<arg key="errors.test" />现在其不到任何作用了 原来他是自动填充arg里的元素的
再做一下测试 看看是不是这么回事
资源文件这么写
errors.required= {...} {0} is required {...} {1} so was it
errors.test=asdasd
errors.testwo=two
验证文件这么写
depends ="required,integer,intRange" >
< arg key ="errors.test" />
< arg key ="errors.testwo" />
</ field >
结果却是two is required {1} so was it. 这样的话并没有完全自动正确填充 而且发生错误
3 validwhen
现在加入稍微复杂一点的 validwhen是用来检验前后关联的属性的
比如这个例子 用户注册的时候两次输入的密码必须相同
那么资源里加上
validation.xml的第二个password里加上
< var >
< var-name > test </ var-name >
< var-value > (*this* == password) </ var-value >
</ var >
validwhen只接受test这一个参数
有一点注意 就是 resource 这个属性是指 是否用变量 如定义的最小值和最大值这样的validator.xml里定义的变量
要么就会显示一个null如果你在intRange定义了动态变量的话
4 结合复杂例子
先看效果图
首先页面文件
register.jsp
<% ... @ taglib uri="/tags/struts-html" prefix="html" %>
<% ... @ taglib uri="/tags/struts-bean" prefix="bean" %>
<% ... @ taglib uri="/tags/c.tld" prefix="c" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > register </ title >
</ head >
< body >
< table >
< html:errors />
< html:form action ="/register" method ="post" >
< tr >
< td width ="60" >
< bean:message key ="register.user.username" />
</ td >
< td width ="60" >
< html:text property ="username" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.password" />
</ td >
< td width ="60" >
< html:password property ="password" ></ html:password >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.confirmPassword" />
</ td >
< td width ="60" >
< html:password property ="confirmPassword" ></ html:password >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.nickname" />
</ td >
< td width ="60" >
< html:text property ="nickname" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.sex" />
</ td >
< td width ="60" >
< html:radio property ="radio" value ="0" />< bean:message key ="register.user.male" />
< html:radio property ="radio" value ="1" />< bean:message key ="register.user.female" />
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.email" />
</ td >
< td width ="60" >
< html:text property ="email" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.birthday" />
</ td >
< td width ="60" >
< html:text property ="birthday" ></ html:text >
</ td >
< td >
< html:select property ="month" >
< option selected value > month </ option >
< c:forEach var ="m" begin ="1" end ="12" step ="1" >
< option value ="<c:out value=" ${m}" /> "> < c:out value ="${m}" /></ option >
</ c:forEach >
</ html:select >
< bean:message key ="register.user.month" />
< html:select property ="day" >
< option selected value > day </ option >
< c:forEach var ="d" begin ="1" end ="31" step ="1" >
< option value ="<c:out value=" ${d}" /> "> < c:out value ="${d}" /></ option >
</ c:forEach >
</ html:select >
< bean:message key ="register.user.day" />
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.telnum" />
</ td >
< td width ="60" >
< html:text property ="telnum" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.qq" />
</ td >
< td width ="60" >
< html:text property ="qq" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.msn" />
</ td >
< td width ="60" >
< html:text property ="msn" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.perweb" />
</ td >
< td width ="60" >
< html:text property ="perweb" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.blog" />
</ td >
< td width ="60" >
< html:text property ="blog" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="60" >
< bean:message key ="register.user.intro" />
</ td >
< td width ="250" >
< html:textarea property ="intro" ></ html:textarea >
</ td >
</ tr >
< tr >
< td width ="60" >
< html:submit ></ html:submit >
</ td >
< td width ="60" >
< html:reset ></ html:reset >
</ td >
</ tr >
</ html:form >
</ table >
</ body >
</ html >
validator.xml
<! DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >
< form-validation >
< formset >
< form name ="registerForm" >
< field property ="username" depends ="required,minlength" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.username" position ="0" />
< arg name ="minlength" key ="register.user.username" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 3 </ var-value >
</ var >
</ field >
< field property ="password" depends ="required,minlength" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.password" position ="0" />
< arg name ="minlength" key ="register.user.password" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 8 </ var-value >
</ var >
</ field >
< field property ="confirmPassword" depends ="required,validwhen" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.confirmPassword" position ="0" />
< msg name ="validwhen" key ="errors.register.thesamepassword" />
< arg name ="minlength" key ="register.user.confirmPassword" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 8 </ var-value >
</ var >
< var >
< var-name > test </ var-name >
< var-value > (*this* == password) </ var-value >
</ var >
</ field >
< field property ="nickname" depends ="required,minlength,mask" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.nickname" position ="0" />
< arg name ="mask" key ="register.user.nickname" position ="0" />
< arg name ="mask" key ="errors.register.nickmask" position ="1" />
< arg name ="minlength" key ="register.user.nickname" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 3 </ var-value >
</ var >
< var >
< var-name > mask </ var-name >
< var-value > ^[a-zA-Z]*$ </ var-value >
</ var >
</ field >
< field property ="email" depends ="required,email" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.email" position ="0" />
< arg name ="email" key ="register.user.email" position ="0" />
</ field >
< field property ="qq" depends ="integer" >
< arg name ="integer" key ="register.user.qq" position ="0" />
</ field >
< field property ="msn" depends ="email" >
< arg name ="email" key ="register.user.msn" position ="0" />
</ field >
< field property ="blog" depends ="url" >
< arg name ="url" key ="register.user.blog" position ="0" />
< var >
< var-name > nofragments </ var-name >
< var-value > false </ var-value >
</ var >
< var >
< var-name > allowallschemes </ var-name >
< var-value > true </ var-value >
</ var >
</ field >
< field property ="birthday" depends ="required,integer,intRange" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.birthday" position ="0" />
< arg name ="integer" key ="register.user.year" position ="0" />
< arg name ="intRange" key ="register.user.birthday" position ="0" />
< arg name ="intRange" key ="register.user.year" position ="1" />
< arg name ="intRange" key ="${var:min}" resource ="false" position ="2" />
< arg name ="intRange" key ="${var:max}" resource ="false" position ="3" />
< var >
< var-name > min </ var-name >
< var-value > 1900 </ var-value >
</ var >
< var >
< var-name > max </ var-name >
< var-value > 2008 </ var-value >
</ var >
</ field >
< field property ="day" depends ="required,integer,intRange" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.day" position ="0" />
< arg name ="integer" key ="register.user.day" position ="0" />
< arg name ="intRange" key ="register.user.birthday" position ="0" />
< arg name ="intRange" key ="register.user.day" position ="1" />
< arg name ="intRange" key ="${var:min}" resource ="false" position ="2" />
< arg name ="intRange" key ="${var:max}" resource ="false" position ="3" />
< var >
< var-name > min </ var-name >
< var-value > 1 </ var-value >
</ var >
< var >
< var-name > max </ var-name >
< var-value > 31 </ var-value >
</ var >
</ field >
< field property ="month" depends ="required,integer,intRange" >
< msg name ="required" key ="errors.register.required" />
< arg name ="required" key ="register.user.month" position ="0" />
< arg name ="integer" key ="register.user.month" position ="0" />
< arg name ="intRange" key ="register.user.birthday" position ="0" />
< arg name ="intRange" key ="register.user.month" position ="1" />
< arg name ="intRange" key ="${var:min}" resource ="false" position ="2" />
< arg name ="intRange" key ="${var:max}" resource ="false" position ="3" />
< var >
< var-name > min </ var-name >
< var-value > 1 </ var-value >
</ var >
< var >
< var-name > max </ var-name >
< var-value > 12 </ var-value >
</ var >
</ field >
</ form >
</ formset >
</ form-validation >
applicationResource.properties
# Project P/datahunt
register.user.username=username
register.user.password=password
register.user.confirmPassword=Confirm Password
register.user.nickname=nickname
register.user.sex=sex
register.user.male=male
register.user.female=female
register.user.email=EMAIL
register.user.birthday=birthday
register.user.birthday.tip=if your web browse not support JavaScript please enter it yourself like YYYY-MM-dd
register.user.telnum=phone num
register.user.qq=QQ
register.user.msn=MSN
register.user.perweb=web
register.user.blog=blog
register.user.intro=Intro
register.user.year=year
register.user.month=month
register.user.day=day
errors.register.required=The {...} {0} is required <br>
errors.register.thesamepassword=You must enter the same password <br>
errors.invalid=The {...} {0} must {...} {1}
errors.minlength=your {...} {0} must more than {...} {1} <br>
errors.maxlength=your {...} {0} must less than {...} {1} <br>
errors.register.nickmask=between a-z or A-Z <br>
errors.email=the {...} {0} is invalid <br>
errors.date=the {...} {0} is invalid <br>
errors.integer=your {...} {0} must be integer <br>
errors.url=your {...} {0} is invalid <br>
errors.range= {...} {0} {...} {1} must between {...} {2} and {...} {3} <br>
除了required 和 validwhen 没有用原有的定义(好像validwhen没有errors定义) 而用<msg>来重新定义了以外
其他的都只是把原有的errors在资源文件里写了一下而已 没有修改key