采取的是spring action 中的例子
验证一个Student对象
1.implements Validator接口(org.springframework.validation.Validator)
public class StudentValidator implements Validator
{
public boolean supports(Class clazz)//必须实现的方法
{return clazz.equals(Student.class) ;
}
public void validate(Object command,Errors errors)
{
Student student =(Student)command;
// ValidationUtils.rejectIfEmpty (Errors errors, String field, String errorCode, String defaultMessage)
ValidationUtils.rejectIfEmpty(errors,"login","required.login","login is required");
ValidationUtils.rejectIfEmpty(errors,"password","required.password","Password is required");
ValidationUtils.rejectIfEmpty(errors,"firstName","required.firstName","firstNameis required");
ValidationUtils.rejectIfEmpty(errors,"lastName","required.lastName","lastNameis required");
ValidationUtils.rejectIfEmpty(errors,"city","required.login","cityis required");
validateEmail(student .email,errors);
validatePhone(student .phone,errors);
}
private static final String PHONE_REGXP="/(\\({0,1}(\\d{3})))/"
private void validatePhone(String phone, Errors erros)
{
ValidationUtils.rejectIfEmpty(errors,"phone","required.phone","phoneis required");
Perl5Util per5Util=new Perl5Util();
if(!per5Util.math(PHONE_REGXP,phone))
{
errors.reject("invalid.phone","Phone number is invalid");
}
}
private static final String Email_REGXP="/(\\({0,1}(\\d{3})))/"
private void validateEmail(String phone, Errors erros)
{
ValidationUtils.rejectIfEmpty(errors,"phone","required.phone","phoneis required");
Perl5Util per5Util=new Perl5Util();
if(!per5Util.math(Email_REGXP,email))
{
errors.reject("invalid.phone","Phone number is invalid");
}
}
}
2.配置bean
<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName"><value>method</value>
</property>
<property name="defaultMethodName"><value>view</value>
</property>
</bean>
<property name="validator"><bean class="com.StudentValidator "/>
</property>
<property name="defaultMethodName"><value>view</value>
</property>
<bean name="/jsp/test.do" class="org.nightwalker.spaces.web.controller.TestController">
<property name="methodNameResolver">
<ref local="methodNameResolver"/>
</property>
</bean>
查看源代码:
MultiActionController类:
protected void bind(ServletRequest request, Object command) throws Exception {
logger.debug("Binding request parameters onto MultiActionController command");
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
if (this.validators != null) {
for (int i = 0; i < this.validators.length; i++) {
//首先调用supports
if (this.validators[i].supports(command.getClass())) {
ValidationUtils.invokeValidator(this.validators[i], command, binder.getErrors());
}
}
}
binder.closeNoCatch();
}
类ValidationUtils:
public static void invokeValidator(Validator validator, Object obj, Errors errors) {
if (validator != null) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking validator [" + validator + "]");
}
if (obj != null && !validator.supports(obj.getClass())) {
throw new IllegalArgumentException("Validator " + validator.getClass() +
" does not support " + obj.getClass());
}
validator.validate(obj, errors);
if (logger.isDebugEnabled()) {
if (errors.hasErrors()) {
logger.debug("Validator found " + errors.getErrorCount() + " errors");
}
else {
logger.debug("Validator found no errors");
}
}
}
}
posted on 2006-06-21 09:39
Dragonofson 阅读(6006)
评论(1) 编辑 收藏 所属分类:
Spring