validation 校验框架(一)

(一).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"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值