0

我创建表单和控制器,这个表单使用 Hibernate 验证器有一些验证约束。我在开始测试验证约束时遇到问题,但是我得到了带有拒绝的属性模型的蓝色异常页面。

这个配置

    @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();
}
    }

和这里的控制器

    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";

注意:我使用 springMVC、spring security、tile 和 hibernate 验证器

我将 SpringMVC 与带有 XML 配置和门户环境的休眠验证器一起使用,并且工作正常我不知道这里出了什么问题!

4

1 回答 1

0

我发现了问题!方法控制器的签名应该是这样的

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

正如我在 sprinsource 论坛中所读到的,BindingResult 应该遵循 modelAttribute 并找到工作。我没有找到任何官方文档,但它现在可以工作了。

要查看 springsource 论坛的主题,请查看此链接http://forum.springsource.org/showthread.php?85815-BindException-Thrown-on-bind-errors-(instead-of-returning-errors-to-controller-method

于 2012-05-13T21:25:53.083 回答