经过这两天的摸索,对验证框架还是有了一定的认识,纠正一下之前的错误,不管验证正确与否,都一定会执行action中的validate函数,把完整的后台验证用户注册贴出来了:
在项目根目录,也就是struts.xml相同的位置添加一个validators.xml,用来添加自定义规则.我只添加了一条自定义的规则,用来验证密码提示问题是否存在与相关数据库中,避免恶意提交(虽然我觉得也可能没有必要,但为了学一下怎么自定义验证规则,我就多此一举了):
自定义规则的配置文件:validators.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"> <validators> <validator name="valueintable" class="com.validators.ValueInSpecificTable"/> </validators>
相应的类:ValueInSpecificTable
package com.validators; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.opensymphony.xwork2.validator.ValidationException; import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; public class ValueInSpecificTable extends FieldValidatorSupport{ private String table; public String getTable() { return table; } public void setTable(String table) { this.table = table; } public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); String value = (String)this.getFieldValue(fieldName, object); SessionFactory factory = new Configuration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); Query query = session.createQuery("from " + table + " where problemContent = '" + value + "'"); transaction.commit(); if(query.list().isEmpty()) { System.out.println("WARN: " + value + "is not in table: " + table); addFieldError(fieldName, object); } session.close(); } }
Action的验证配置文件(和Action放在相同目录下):UserRegisterSaveAction-vaidation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphonyGroup//XWorkValidator1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="register.user_id"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">16</param> <message>用户名长度应在4到16之间</message> </field-validator> </field> <field name="register.password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>密码不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">18</param> <message>密码长度应在6到18个字符之间</message> </field-validator> </field> <field name="register.pwd_repeat"> <field-validator type="fieldexpression"> <param name="fieldName">register.password</param> <param name="fieldName">register.pwd_repeat</param> <param name="expression"><![CDATA[register.password == register.pwd_repeat]]></param> <message>密码必须和确认密码相等</message> </field-validator> </field> <field name="register.problem_content"> <field-validator type="valueintable"> <param name="table">PromptProblem</param> <message>请输入信息</message> </field-validator> </field> <field name="register.answer"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>密码提示答案不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">10</param> <message>密码提示答案长度应在2到10之间</message> </field-validator> </field> <field name="register.nickname"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>昵称不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">10</param> <message>昵称长度应在2到10之间</message> </field-validator> </field> <field name="register.phone"> <field-validator type="regex"> <param name="fieldName">register.phone</param> <param name="trim">true</param> <param name="expression"><![CDATA[(((13[0-9]{1})|159|186|188|189|(15[0-9]{1}))+\d{8})]]></param> <message>请输入正确的手机号码</message> </field-validator> </field> <field name="register.email"> <field-validator type="email"> <message>请输入正确的邮箱地址</message> </field-validator> </field> </validators>
感觉Struts2的验证框架还是蛮好用的,就是刚开始学的时候会比较烦一点,网上很多Struts1框架的配置方法堂而皇之地冠以Struts2的名号,相当无语~~~