Struts2 自定义validator 防止页面提交的list值重复

最近做小项目的时候,由于页面上有许多list,提交上来后,要将list中所有对象都保存至数据库。如果list中对象主键有重复的话,那么主键冲突就保存不了,因此对这种情况应该进行验证。

struts2的验证框架可谓是非常优雅,但无奈默认提供的validators中没有满足这种需求的validator,因此需要进行扩展。

对于扩展struts2 validator框架的介绍有很多,例如:
http://developer.51cto.com/art/201103/251944.htm
在此就不重复介绍了


首先是我们自定义的validator

package com.zyl.eduInfoStat.validator;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
import com.zyl.eduInfoStat.model.help.ListValidateAble;

public class ListRepeatValidator extends FieldValidatorSupport {

@SuppressWarnings("unchecked")
@Override
public void validate(Object o) throws ValidationException {
// TODO Auto-generated method stub
String listName = getFieldName();
List<ListValidateAble> list = (List<ListValidateAble>) this
.getFieldValue(listName, o);
Set<String> set = new HashSet<String>();
for (ListValidateAble lva : list) {
if (set.contains(lva.getStrForValidate())) {
addFieldError(listName, o);
return;
}
set.add(lva.getStrForValidate());
}
}

}


这段代码就是验证list内容重不重复的主要逻辑,非常简便,其中ListValidateAble是一个接口,所有需要这个验证器验证的对象都需要implements这个接口。这个接口定义非常简单:

package com.zyl.eduInfoStat.model.help;

public interface ListValidateAble {
public String getStrForValidate();

}


之所以定义这个接口,是为了将自定义的ListRepeatValidator具有普适性.只要list中的对象是扩展了这个接口的,都可以用ListRepeatValidator验证。
如:

package com.zyl.eduInfoStat.model;

import java.io.Serializable;

import com.zyl.eduInfoStat.model.help.ListValidateAble;

public class EquipInfo_trainRoom implements Serializable ,ListValidateAble{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 在表中的序号,用于保持显示顺序
*/
private int i;
private String schoolName;
private String roomName;


public String getSchoolName() {
return schoolName;
}

public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}

public String getRoomName() {
return roomName;
}

public void setRoomName(String roomName) {
this.roomName = roomName;
}

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}

@Override
public String getStrForValidate() {
// TODO Auto-generated method stub
return schoolName;
}
}




接下来是validators.xml,我们需要将这个文件放到classpath的根目录下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator Definition 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">

<!-- START SNIPPET: validators-default -->
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>


<!-- 自定义的验证器 -->
<validator name="listRepeat" class="com.zyl.eduInfoStat.validator.ListRepeatValidator"/>
</validators>
<!-- END SNIPPET: validators-default -->


我们自定义的ListRepeatValidator在最下方,上方是系统默认提供的验证器。

最后是action的配置,如EquipInfoSubmitAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators >
<field name="equipInfo_trainRoom_list">
<field-validator type ="listRepeat" >
<message>值不能重复!</message>
</field-validator >
</field>
</validators>



如此便大功告成了!


PS.
写代码一有问题,便是上网搜,解决了无数的问题。想着咱也必须贡献一点力啊~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值