フォームと注釈の制約の両方のバリデーターを持つことは可能ですか?
たとえば、フォームオブジェクトにこのフィールドを含めるには、次のようにします。
@NotEmpty
private String date;
ただし、バリデーターで日付のパターンを検証します。
パターンアノテーションがあることは知っていますが、両方のタイプの検証を使用できるかどうかを確認したいだけです。
フォームと注釈の制約の両方のバリデーターを持つことは可能ですか?
たとえば、フォームオブジェクトにこのフィールドを含めるには、次のようにします。
@NotEmpty
private String date;
ただし、バリデーターで日付のパターンを検証します。
パターンアノテーションがあることは知っていますが、両方のタイプの検証を使用できるかどうかを確認したいだけです。
これは、 JSR -303バリデーターとスプリングバリデーターを組み合わせる方法が説明されている非常に優れたサイトへのリンクです。
次に、機能するソリューションを紹介します。それが役に立てば幸い。
私の抽象的なバリデーター:
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.validation.Errors;
public abstract class AbstractValidator implements org.springframework.validation.Validator, ApplicationContextAware,
ConstraintValidatorFactory {
@Autowired
private Validator validator;
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
Map<String, T> beansByNames = applicationContext.getBeansOfType(key);
if (beansByNames.isEmpty()) {
try {
return key.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Could not instantiate constraint validator class '" + key.getName() + "'", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Could not instantiate constraint validator class '" + key.getName() + "'", e);
}
}
if (beansByNames.size() > 1) {
throw new RuntimeException("Only one bean of type '" + key.getName() + "' is allowed in the application context");
}
return (T) beansByNames.values().iterator().next();
}
public boolean supports(Class<?> c) {
return true;
}
public void validate(Object objectForm, Errors errors) {
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(objectForm);
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
String propertyPath = constraintViolation.getPropertyPath().toString();
String message = constraintViolation.getMessage();
errors.rejectValue(propertyPath, "", message);
}
addExtraValidation(objectForm, errors);
}
protected abstract void addExtraValidation(Object objectForm, Errors errors);
}
実際のバリデーター:
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import ro.scorpionsoftware.demo3.dao.AbstractValidator;
@Component(value="doctorValidator")
public class DoctorValidator extends AbstractValidator {
@Override
protected void addExtraValidation(Object objectForm, Errors errors) {
//perform typical validation
//can autowire to context
}
}
コントローラー:(最後に、@ Validとバリデーターのバインドです)
@Controller
public class DoctorEditController {
@Autowired
private DoctorValidator doctorValidator;
@RequestMapping(value = "/doctorEdit", method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("doctorForm") @Valid DoctorForm df,
BindingResult result,
ModelMap model) {
...
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(doctorValidator);
}
}
コンテキストで、JSR-303バリデーターを宣言します。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
このアプローチでは、実際のバリデーターと実装したい他のカスタムアノテーションの両方でコンテキストを取得できます。
アノテーションを組み合わせて複数のバリデーターを使用できるので、次のようになります。
@NotEmpty
@Pattern("") // not sure of syntax here
@Target(ElementType.METHOD)
@Retention( RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface DatePattern {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}