annotation based Spring bean validation

天大地大妈咪最大 提交于 2019-12-02 19:30:03

I took a quick look at the BeanValidator API, and it looks like you might want to try the errorCodeConverter property.

You would need to implement your own ErrorCodeConverter, or use one of the provided implementations?

....
<bean id="validator" class="org.springmodules.validation.bean.BeanValidator"
    p:configurationLoader-ref="configurationLoader"
    p:errorCodeConverter-ref="errorCodeConverter" />

<bean id="errorCodeConverter" class="contact.MyErrorCodeConverter" />
....

Note: configurationLoader is another bean defined in the config XML used in the tutorial

Example converter:

package contact;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springmodules.validation.bean.converter.ErrorCodeConverter;

public class MyErrorCodeConverter implements ErrorCodeConverter {

    private Log log = LogFactory.getLog(MyErrorCodeConverter.class);

    @Override
    public String convertPropertyErrorCode(String errorCode, Class clazz, String property) {
        log.error(String.format("Property %s %s %s", errorCode, clazz.getClass().getName(), property));
        return errorCode;  // <------ use the errorCode only
    }

    @Override
    public String convertGlobalErrorCode(String errorCode, Class clazz) {
        log.error(String.format("Global %s %s", errorCode, clazz.getClass().getName()));
        return errorCode;
    }
}

Now the properties should work:

MyEmailErrorCode=Bad email

class Foo {
    @Email(errorCode="MyEmailErrorCode")
    String email
}

Spring validation does have an ErrorCodeConverter that does this:

org.springmodules.validation.bean.converter.KeepAsIsErrorCodeConverter

When this is used, the resource bundle will be checked for the following codes:

[errorCode.commandBeanName.fieldName, errorCode.fieldName, errorCode.fieldClassName, errorCode]

  • errorCode is the actual validation errorCode eg. not.blank, email.
  • commandBeanName is the same as the model key name that references the form backing bean.
  • fieldName is the name of the field.
  • fieldClassName is the field class name eg. java.lang.String, java.lang.Integer

So for instance if I have a bean that is referenced in the model by the key "formBean" and the field emailAddress of type java.lang.String does not contain an email address, which causes the errorCode email. The validation framework will attempt to resolve the following message codes:

[email.formBean.emailAddress, email.emailAddress, email.java.lang.String, email]

If the errorCode is replaced by the errorCode "badEmail" like this:

@Email(errorCode="badEmail")

The messages codes that the framework will try resolve will be:

[badEmail.formBean.emailAddress, badEmail.emailAddress, badEmail.java.lang.String, badEmail]

I would suggest keeping the errodCode the same. Thus one message can be used for all fields that have that errorCode associated with them. If you need to be more specific with the message for a certain field you can add a message to the resource bundles with the code errorCode.commandBeanName.field.

Add the following beans in your applicationContext.xml file.

<bean id="configurationLoader"
    class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader" />

<!-- Use the error codes as is. Don't convert them to <Bean class name>.<bean field being validated>[errorCode]. -->    
<bean id="errorCodeConverter"
    class="org.springmodules.validation.bean.converter.KeepAsIsErrorCodeConverter"/>

 <!-- shortCircuitFieldValidation = true ==> If the first rule fails on a field, no need to check 
    other rules for that field -->   
<bean id="validator" class="org.springmodules.validation.bean.BeanValidator"
    p:configurationLoader-ref="configurationLoader"
    p:shortCircuitFieldValidation="true" 
    p:errorCodeConverter-ref="errorCodeConverter"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!