日期:2014-05-16 浏览次数:20495 次
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency>
<mvc:annotation-driven validator="validator"/> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
package com.wicresoft.jpo.demo.validator; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD}) @Constraint(validatedBy = {BetweenImplForDate.class}) public @interface Between { public String message(); public String startDate(); public String endDate(); public String format() default "yyyy-MM-dd"; public Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
package com.wicresoft.jpo.demo.validator; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class BetweenImplForDate implements ConstraintValidator<Between, Date> { private static final Logger LOGGER = LoggerFactory.getLogger(BetweenImplForDate.class); private Date startDate; private Date endDate; private DateFormat dateFormat; public void initialize(Between annotation) { this.dateFormat = new SimpleDateFormat(annotation.format()); try { this.startDate = dateFormat.parse(annotation.startDate()); this.endDate = dateFormat.parse(annotation.endDate()); } catch (ParseException e) { throw new IllegalArgumentException(e.getMessage(), e); } } public boolean isValid(Date value, ConstraintValidatorContext context) { LOGGER.debug("value = {}", dateFormat.format(value)); return (startDate.getTime() <= value.getTime()) && (value.getTime() < endDate.getTime()); } }
v.login.birthday=生日不合法,生日应在{startDate}与{endDate}之间。
<mvc:annotation-driven conversion-service="conversion-service" validator="validator"> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="com.wicresoft.jpo.demo.http.converter.UTF8StringHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="validationMessageSource" ref="messageSource" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <p