struts2校验

1.      校验器分为两种方式:

A>编码的校验方式(重写validate方法)

B>校验框架(配置文件Action名-validation.xml)

2. 编码的校验方式(重写validate方法)

A>首先进行类型转换

B>.然后进行校验(执行validate方法)

C>.如果在上述过程中出现了任何错误,都不会执行execute方法,会转向struts.xml中该action的名为input的result所对应的页面。

 D>当调用getActionErrors()方法返回Action级别的错误信息列表时,返回的的实际上是集合的一个副本而不是集合本身。

因此对集合副本调用clear()方法清除的依旧是副本中的元素而非原集合中的元素,此时原集合中的元素没有受到任何影响。

换句话说,Action级别的错误信息列表对开发者来说是只读的。

 E>Field Error级别的错误信息底层是用LinkedHashMap实现的,该Map的Key是String类型,value是List<String>类型.

 这就表示一个Field Name可以对应多条错误信息,这些错误信息都放置在List<String>集合当中。

 F>Action中自定义方法的输入校验,对于通过Action的method属性所指定的自定义方法

其自定义的输入校验方法名为validateMyExecute(假设自定义的方法名为myExecute)。底层是通过反射来调用的

G>当在Action中指定了自定义的execute方法时,首先会执行自定义的execute方法所对应的输入校验方法,然后再去执行validate方法。

执行完毕后如果出现了任何错误都不会再去执行自定义的execute方法,流程转向了input这个名字所对应的页面上

H>自定义Field级别的错误提示消息。

       新建一个以Action命名的properties文件,如RegisterAction.properties

       然后在该属性文件中指定每一个出错字段的错误信息

       invalid.fieldvalue. birthday=birthday invalidate!

3.例:  对页面的输入字段进行校验。

a)      登录界面register.jsp.

<s:actionerrorcssStyle="color:red"/>

 ---------------------------------------------

<s:fielderror cssStyle="color:blue"/><br/>

       <s:formaction="registerAction.action" theme="simple">

             username:<s:textareaname="username"label="username"></s:textarea><br/>

             password:<s:passwordname="password"label="password"></s:password><br/>

             repassword:<s:passwordname="repassword"label="repassword"></s:password><br/>

             age:<s:textareaname="age" label="age"></s:textarea><br/>

             birthday:<s:textareaname="birthday"label="birthday"></s:textarea><br/>

             graduation:<s:textareaname="graduation" label="graduation"></s:textarea><br/>

             <s:submitname="submit" label="submit"></s:submit>

       </s:form>

b)      登录界面 校验RegisterAction.java(继承ActionSupport类重写validate方法)

public void validate() {

             if(null == username ||username.length() < 4 || username.length() > 6){

                    this.addActionError("yourusername is invalidate,please enter your username's length between 4 and6");

//                 this.addFieldError("useranme"," field error invoked!");

             }

             if(null == password ||password.length() < 4 || password.length() > 6){

                    this.addActionError("yourpassword is invalidate,please enter your password's length between 4 and6");

             }

             else if(null == repassword ||!password.equals(repassword)){

                    this.addActionError("yourpassword and your repassword are not the same!");

             }

             if(age > 130 || age < 0){

                    this.addActionError("agemust bewtween 0 and 130");

             }

             if(null == birthday){

                    this.addActionError("yourbirthday or graduation is invalidate,please enter your birthday orgrudation");

             }

             if(null == graduation){

                    this.addActionError("yourbirthday or graduation is invalidate,please enter your birthday orgrudation");

             }

             if(null != birthday && null!= graduation){

                    Calendar c1 =Calendar.getInstance();

                    c1.setTime(birthday);

                    Calendar c2 =Calendar.getInstance();

                    c2.setTime(graduation);

                    if(c1.after(c2)){

                           this.addActionMessage("yourgraduation muse afer your birthday");

                    }

             }

 //下面这这两个方法不能清空错误信息getActionErrors返回的是ArrayList类型的副本,getFieldErrors返回的是LinkedMap

//          this.getActionErrors().clear();

//          this.getFieldErrors().clear();

 //下面这两个方法能清空错误信息。

//          this.clearActionErrors();

//          this.clearFieldErrors();

      }

c)      自定义校验方法

publicString myExecute(){}

publicvoid validateMyExecute(){}

d)      自定义Field级别的错误提示信息RegisterAction.properties

invalid.fieldvalue.graduation=invalidate   graduation

invalid.fieldvalue.graduation=invalidategraduation

e)      Unicode字符集 native2ascii

使用unicode编码实现中文的报错信息。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值