Struts2基于注解的验证框架

Struts2对数据的验证方式有好几种,可以简单的分为三类:

1.编程 2.xml配置 3.注解

一般应该是xml配置用得较多,但对比一下果然还是注解相对简单

要使用Struts2验证框架,我们需要在页面使用Struts2的各种标签,首先要引用标签库

<%@taglib uri="/struts-tags" prefix="s"%>
下面是要验证的输入框:
<s:form action="register" method="post">
                <table>
                      <tr>
                           <td>用户名:</td>
                           <s:textfield type="text" id="name" name="userName"></s:textfield>
                      </tr>
                      <tr>
                           <td><label>密码:</label></td>
                           <td><s:textfield type="password" id="password" name="password" /></td>
                      </tr>
                      <tr>
                           <td><label>确认密码:</label></td>
                           <td><s:textfield type="password" id="repassword" name="repassword" /></td>
                      </tr>
                      <tr>
                           <td>邮箱:</td>
                           <td><s:textfield type="text" id="email" name="email" /></td>
                      </tr>
                      <tr>
                           <td>电话:</td>
                           <td><s:textfield type="text" id="phone" name="phone" /></td>
                      </tr>
                      <tr height="10">
                           <td><button type="submit">注册</button></td>
                           <td><button type="reset">重置</button></td>
                           <td><button type="button"
                                      οnclick="javascript:history.back()">返回</button></td>
                      </tr>
                </table>
           </s:form>
然后是 Action 类上的注解,写在要验证的方法上面(虽然作用于全体。。):
@Validations(
                requiredStrings={
                       @RequiredStringValidator(type=ValidatorType.SIMPLE,trim=true,fieldName="userName",message="请输入用户名"),
                       @RequiredStringValidator(type=ValidatorType.SIMPLE,trim=true,fieldName="password",message="请输入密码"),
                       @RequiredStringValidator(type=ValidatorType.SIMPLE,trim=true,fieldName="phone",message="请输入电话"),
                },stringLengthFields={
                      @StringLengthFieldValidator(type=ValidatorType.SIMPLE,fieldName="userName",maxLength="8",minLength="4",message="用户名长度为4-8位"),
                       @StringLengthFieldValidator(type=ValidatorType.SIMPLE,fieldName="password",maxLength="20",minLength="6",message="密码长度为6-20位"),
                },regexFields={
                       @RegexFieldValidator(fieldName="phone",regexExpression="^(\\+86|0|1)\\d{10,11}$", 
                                      message="电话号码格式不正确!"),
                },fieldExpressions={
                       @FieldExpressionValidator(fieldName="repassword",key="password",expression="repassword eq key",message="确认密码错误")
                },emails={
                           @EmailValidator(fieldName="email",message="邮箱格式错误")
                }
                )
需要注意的是验证注解会影响类里的所有方法,如果其他方法不需要注解,可以使用
@SkipValidation

跳过验证,如果需要开启方法级别的验证,可以通过配置文件配置。

当验证不通过时会返回input视图,我这边是直接返回到注册界面:

@Results({@Result(name = "input", location =
"/register.jsp") })
你也可以通过
@InputConfig(resultName="error")

自己配置要返回到的界面

 

上面注解的功能可以说很见名知意了,不过我在这里简单说说验证密码那个:

@FieldExpressionValidator
我在使用这个的时候模仿着xml配置的写法写了好几种验证方式,结果是行不通的,最后我发现了属性key,然后把密码的值传给key值与待确认密码比较,这样才能实现密码校对功能。

本文章来自我的个人博客:http://www.jimblog.site/blog/articles/87.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值