Search code examples
spring-mvcspring-securityhibernate-validatortiles2

Spring MVC doesn't handle the error came from hibernate validator


I create form and controller this form have some validation constrains using Hibernate validator. I face problem when starting test the validation constrains but I got Blue Exception page with the attributemodel with the rejected.

This the configuration

    @Configuration
    @ComponentScan(basePackages = {"com.whatever.core.web"})
    @EnableWebMvc
    public class WebMvcConfig extends WebMvcConfigurationSupport {

private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
private static final String TILES = "/WEB-INF/tiles/tiles.xml";
private static final String VIEWS = "/WEB-INF/views/**/views.xml";

private static final String RESOURCES_HANDLER = "/resources/";
private static final String RESOURCES_LOCATION = RESOURCES_HANDLER + "**";



@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping();
    requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
    requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
    return requestMappingHandlerMapping;
}

public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJacksonHttpMessageConverter());
}

@Bean(name = "messageSource")
public MessageSource configureMessageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename(MESSAGE_SOURCE);
    messageSource.setCacheSeconds(5);
    return messageSource;
}

@Bean
public TilesViewResolver configureTilesViewResolver() {
    return new TilesViewResolver();
}

@Bean
public TilesConfigurer configureTilesConfigurer() {
    TilesConfigurer configurer = new TilesConfigurer();
    configurer.setDefinitions(new String[] {TILES, VIEWS});
    return configurer;
}

@Override
public Validator getValidator() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource(configureMessageSource());
    return validator;
}



@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(RESOURCES_LOCATION);
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
    }

and the controller here

    if(result.hasErrors()){
        return null; OR "view name"
    }
    User user = new User();
    user.setUsername(userModel.getUsername());        
    user.setFirstName(userModel.getFirstName());
    user.setLastName(userModel.getLastName());
    user.setGender(userModel.getGender());
    user.setLocation(userModel.getLocation());
    user.setPassword(passwordEncoder.encodePassword(userModel.getPassword(),null));

    userRepository.save(user);
    doAutoLogin(userModel.getUsername(),userModel.getPassword(),request);
    return "redirect:/home";

NOTE: I use springMVC, spring security, tiles, and hibernate validator

I used SpringMVC with hibernate validator with XML configuration and portal environment and work fine I don't know what the wrong here!!


Solution

  • I Found the issue! the signature of the method controller should be like this

        public String signup(@ModelAttribute("userModel") @Valid SignupForm userModel,BindingResult result,HttpServletRequest request,HttpServletResponse response,ModelMap model)
    

    as what I read in sprinsource forum, the BindingResult should follow the modelAttribute and work find. I didn't find any official documentation for this but its work now.

    to see the thread of springsource forum check this link http://forum.springsource.org/showthread.php?85815-BindException-Thrown-on-bind-errors-(instead-of-returning-errors-to-controller-method