(一).xwork jar包:com.opensymphony.xwork2.validator.validators 下有一个default.xml文件,里面躺着些默认的校验器。
(二).验证规则指定
文件名称有两种指定方式:
ActionName-validation.xml
ActionName-alias-validation.xml
ActionName就是类名,比如有个LoginAction.java文件,那么ActionName就是LoginAction。
alias就是配置在struts.xml文件中,咱们指定的那个action的name。
比如struts.xml中我配置了个
<action name="login" class="com.trendcom.upload.action.LoginAction">
<result name="success">/tutorial/login.jsp</result>
<result name="error">/tutorial/error.jsp</result>
<result name="input">/input.jsp</result>
</action>
alias就是 login。说alias是方法名,其实是不准确的。原文这么说的:
In this context, "Action Alias" refers to the action name as given in the Struts configuration. Often, the name attribute matches the method name, but they may also differ.
两种取名的方式:
1.ActionName-validation.xml
这种取名方式会作用在LoginAction 中所有的action方法中,
比如:我在struts.xml配置了两个action
<action name="login" class="com.trendcom.upload.action.LoginAction">
<result name="success">/tutorial/login.jsp</result>
<result name="error">/tutorial/error.jsp</result>
<result name="input">/input.jsp</result>
</action>
<action name="logout" class="com.trendcom.upload.action.LoginAction" method="logout">
<result name="success">/tutorial/login.jsp</result>
<result name="error">/tutorial/error.jsp</result>
<result name="input">/input.jsp</result>
</action>
ActionName-validation.xml这个文件会作用在这两个action上。
2.ActionName-alias-validation.xml
这种取名方式就只会作用在指定的action 方法上。比如LoginAction-login-validation.xml就只会作用在login这个action对应的method上。
注1:不管是取的哪个名字,这个validation文件要放在ActionName所在包路径下。
注2:在struts.xml文件配置中一定要配置个name为input的result。当验证失败时,将返回input。如果没有指定,将得到一个错误提示: "No result defined for action 。。。。。and result input"