Announcement Announcement Module
Collapse
No announcement yet.
Validator messageSource help Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validator messageSource help

    Version: Spring 2.5.6

    Hello. I am new to Spring (and I haven't used Java in a while), and I have inherited an Spring application that I need to fix. It seems that no matter what I do, the validation errors never see my messageSource and always use the default values. I've read the online documentation and is says that if the messageSource is registered it should automatically be found, but that doesn't seem to be happening.

    Here is the applicationContext.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
        </bean>
    
        <bean id="localeResolver" class="org.openads.oac.web.common.i18n.LocaleResolver">
            <property name="defaultLocale" value="en" />
        </bean>
    
        <import resource="classpath:accountBackendContext.xml" />
    
        <bean id="hibernateTransactionInterceptor"
       class="org.openads.oac.web.hibernate.HibernateTransactionPerRequestHandlerInterceptor">
            <property name="transactionHelper" ref="transactionHelper" />
        </bean>
    </beans>
    Here is the validation class:

    Code:
    public class SignupValidator implements Validator {
        
        private UserDao userService;
        
        @Override
        public boolean supports(Class aClass) {
            return aClass.equals(Signup.class);
        }
    
        @Override
        public void validate(Object obj, Errors errors) {
            Signup s = (Signup) obj;
            if (s == null) {
                errors.rejectValue("userName", "error.not-specified", null, "Username not specified!");
            } else {
                // ...other validation
                // Check password is filled
                if (s.getPassword() == null || s.getPassword().equals("")) {
                    errors.rejectValue("password", "required.password",
                            null, "default");
                } else {    
                    // Check passwords match
                    if (!s.getPassword().equals(s.getConfirmPassword())) {
                        errors.rejectValue("confirmPassword", "error.password-mismatch",
                                null, "default");
                    }
                    // Check the password is strong enough
                    if (!ValidationUtils.isValidPassword(s.getPassword())) {
                        errors.rejectValue("password", "error.password-invalid",
                                null, "default");
                    }
                }
            }
        }
    
        public UserDao getUserService() {
            return userService;
        }
    
        public void setUserService(UserDao userService) {
            this.userService = userService;
        }
        
    }
    Here is the messages.properties file:

    Code:
    #Signup Error Messages
    error.password-mismatch=The passwords provided do not match
    error.password-invalid=The password is invalid (must be six to twenty-six characters)
    Please let me know what I am doing wrong. Thanks!

  • #2
    Probably not the right way to fix it, but I had my validators implement MessageSourceAware, then used this.messageSource.getMessage for the default error message for the validation errors. The errors never got to the messageSource on their own.

    Comment

    Working...
    X