浏览 1908 次
锁定老帖子 主题:Struts2中的collection验证
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-30
webwork数组形式提交表单及validation研究.但文中的collection是xwork1的验证器,在xwork2中并不存在,于是想到把它从xwork1中转移过来.并修改了少许,使之可以支持多个验证器.
Struts2提供了Collection的转换器,可以直接把页面传递过来的参数转换成List Set类型,使用这个转换器能为我们节省不少手指运动量,但Collection的验证就成了一个问题.google之后,找到了这个资料
有2个类,CollectionFieldValidator和DefaultActionValidatorManager,因为都是xwork1的类,所以我们还要稍微改动一下. CollectionFieldValidator.java //propertyname,e.g:persons.name private String property; public String getProperty() { return this.property; } public void setProperty(String collection) { this.property = collection; } //all validatorRef split with "," private String validatorRefs; public String getValidatorRefs() { return this.validatorRefs; } public void setValidatorRefs(String validatorRefs) { this.validatorRefs = validatorRefs; } //a validator's param key start with it's ref private Map validatorParams = new LinkedHashMap(); public void setValidatorParams(Map validatorParams) { this.validatorParams = validatorParams; } public Map getValidatorParams() { return validatorParams; } //get all validator and set them then validate public void validate(Object object) throws ValidationException {...} //find all propertyname and put them into result (a recursion method) protected void populateValue(Object obj, CloneableIterator iterator, List result, String overallPropertyName) throws OgnlException, CloneNotSupportedException {...} } 因为CollectionFieldValidator.java中调用了xwork1的DefaultActionValidatorManager.java中的validate(Object object, List validators, ValidatorContext validatorContext)方法,但该方法在xwork2中不存在,甚至ActionValidatorManager中不存在这个方法接口,只能自己动手了. Xwork1DefaultActionValidatorManager.java //继承了xwork2的DefaultActionValidatorManager public class Xwork1DefaultActionValidatorManager extends DefaultActionValidatorManager{ //仅仅从xwork1的DefaultActionValidatorManager.java中拷贝过来 public void validate(Object object, List validators, ValidatorContext validatorContext) throws ValidationException {...} } 使用方法如下: validators.xml <validators> <validator name="collection" class="com.ddl.tools.CollectionFieldValidator"/> </validators> ActionName-validattion.xml <validators> <!-- Action 中 Field 名--> <field name="sentForm"> <field-validator type="collection"> <!-- 需要验证的属性,其中details是collection--> <param name="property">sentForm.details.requestQty</param> <!-- 以逗号分隔 --> <param name="validatorRefs">required,int</param> <!-- 验证器引用名_参数名--> <param name="validatorParams['required_messageKey']">receivedocument.requestQty.validate.required</param> <param name="validatorParams['int_messageKey']">receivedocument.requestQty.validate.int</param> <message>...</message> </field-validator> </field> <field name="sentForm"> <field-validator type="collection"> <param name="property">sentForm.details.item</param> <param name="validatorRefs">requiredstring</param> <param name="validatorParams['requiredstring_messageKey']">receivedocument.item.validate.required</param> <message>...</message> </field-validator> </field> </validators> 可能有人注意到使用方法和上面提供的链接中稍有不同,这是因为原来的collection验证不能有多验证器. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |