2011-07-26 7 views
10

春にJSR-303 bean validationtraditional validationの両方(型に1つのバリデータクラス)を使用することはできますか?もしそうなら、これを設定するためにはどのような設定が必要ですか?JSR-303と従来のBean検証の両方を使用していますか?

私はreferenceの手順を試しました。

@InitBinder 
protected void initBinder(WebDataBinder binder) { 
    binder.setValidator(new DualEntryValidator()); 
} 

@RequestMapping(value="/dualEntry.htm", method = RequestMethod.POST) 
public ModelAndView handlePost(@Valid DualEntryForm form, BindingResult result) { 
    ModelAndView modelAndView = new ModelAndView("dualEntry", getCommonModel()); 

    if (!result.hasErrors()){ 
     //do logic 
     return modelAndView; 

    }else { 
     modelAndView.addObject("dualEntryForm", form); 
     return modelAndView; 
    } 
} 

私はこれが私のカスタムValidatorまたは JSR-303の検証ではなく、両方を使用して取得することができます。この例のようにinitBinderがある場合は、カスタムValidatorを使用します。これを削除すると、JSR-303 bean検証が使用されます。 はどのようにしての両方を使用できますか?

答えて

1

Springは、Beanの検証に3つのハンドルを提供します。

1.abstractクラスAbstractPropertyValidationAnnotationHandler

2.abstractクラスAbstractMethodValidationAnnotationHandler

3.abstractクラスClassValidationAnnotationHandler

この例では、私は、カスタム注釈CustomAnnotationHandle

を実施しています
@Target({ElementType.METHOD, ElementType.TYPE}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
Class CustomAnnotationHandle extends Annotation{ 

    public abstract String value(); 

    } 

プロパティの検証にカスタムアノテーションを実装するには、AbstractPropertyValidationAnnotationHandlerクラスを拡張する必要があります。

AbstractPropertyValidationAnnotationHandlerだからcreateValidationRule抽象メソッド

protected abstract AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s); 

を提供し、拡張クラスは

protected abstract AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s) 
public class CustomPropertyAnnotationHandler extends AbstractPropertyValidationAnnotationHandler 
{ 

    public CustomPropertyAnnotationHandler() 
    { 
     super(new Class[] { 
      XXX.XXX.PackageLevle.CustomAnnotationHandle // as it takes array of custom annotation ,so we can pass more than one 
     // overwriting abstract method 
     protected AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s){ 
      CustomAnnotationHandle value = (CustomAnnotationHandle)annotation; 
      return TestValidationRule(value.getValue()); 

      // as you can see it return AbstractValidationRule.So, we need a class to give our bean specific validation rule.In our case it is 

      //TestValidationRule 
     } 


    } 
} 

public class TestValidationRule extends AbstractValidationRule 
{ 

    public TestValidationRule (String valuetest) 
    { 
    super(); 
this.valuetest = valuetest; 
    } 


Private String valuetest; 


} 

春AnnotationBeanValidationConfigurationLoader Class.Thisクラスはスプリング自身の注釈のために使用される提供の実装を提供しなければなりませんBeanの検証のため。

defaultValidationAnnotationHandlerRegistryクラスがdefaultHandlerRegistryとして使用されます。私たちは私たち自身のannotaionを提供する必要がある場合しかし、その後、私たち

は、メソッドを介して setHandlerRegistry(新しいCustomPropertyAnnotationHandlerを())AnnotationBeanValidationConfigurationLoaderを拡張し、当社の特定handleregistryを設定する必要があります。我々は

に必要なクラスDefaultValidationAnnotationHandlerRegistryは、当社のカスタム注釈のためのSimpleValidationAnnotationHandlerRegistry class.SoのregisterPropertyHandlerメソッドを呼び出す

でBeanを登録validation.It Beanの春独自の注釈を登録するために使用される

registerPropertyHandlerメソッドを呼び出すことによってCustomPropertyAnnotationHandlerを登録SimpleValidationAnnotationHandlerRegistryクラスの

public class OurBeanSpecificValidationLoader extends AnnotationBeanValidationConfigurationLoader 
{ 

    public OurBeanSpecificValidationLoader() 
    { 
super(); 
     setHandlerRegistry(new OurSpecificAnnotationHandleRegistery()); 
    } 


} 

public class OurSpecificAnnotationHandleRegistery extends DefaultValidationAnnotationHandlerRegistry 
{ 

    public OurSpecificAnnotationHandleRegistery() 
    { 
     registerPropertyHandler(new CustomPropertyAnnotationHandler()); 
    } 
} 

Beanの注釈valiation.Eg

@CustomAnnotationHandle(value = "test") 
    private Object test; 
8

私はこれはかなり古いです実現が、私は、これは

変更binder.setValidator(new DualEntryValidator());

@InitBinder 
protected void initBinder(WebDataBinder binder) { 
    binder.addValidators(new DualEntryValidator()); 
} 

に私のコードに最小限の妨害で動作するようになりましたsetValidator()を使用すると、JSR-303バリデーターをあなたのものに置き換えます。 addValidator()では、JSR-303バリデーターが呼び出され、それもあなたのものです。

あなたがそうしないと、重複したエラーメッセージが追加され得るでしょう、あなたのバリデータはあなたのJSR-303 @NotNull@Min@Maxなどの注釈と重複しないことを確認する必要があります。

+1

これは、新しい承認された回答である必要があります。 – shadowhorst

+0

これは私が使用しているものですが、私は 'JSR-303'バリデーターを* BEFORE * my'カスタムバリデーター 'と呼ぶ必要があります。それを達成することは可能ですか? JSR-303の基本的な検証が失敗した場合(@NotNullsなど) – Doug

+0

私のカスタムバリデーターは、JSR-303が手にしていないのでヌルポインタ例外を投げているのでnullですカスタムバリデータが起動される前にバリデーションが行われていない – Doug

関連する問題