springMVC 与jsr 303 结合后端验证问题


今天调了一整天,主要就是jsp禁用前端验证后,提交表单,页面报400错误:解决方案,@vaild注解后要紧跟BindingResult,不然就会包400错误。它俩是成对出现的,也就是有多少个实体需要验证就要有多少对。

再就是页面不报400错误了,但是后端验证始终不好使,也就是bindingResult对象getErrors 始终是false,这个真的好郁闷。

后来研究了一下springMvc的配置发现要这样配置:

 

[html]  view plain copy print ?
  1. 这是后添加的:   
[html]  view plain copy print ?
  1. <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">   
  2.          <property name="converters">    
  3.             <list>    
  4.                  <bean class="org.bdp.common.convertor.DataConvertor">    
  5.                  </bean>    
  6.              </list>    
  7.          </property>    
  8.       </bean>    
  9.         
  10.      <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">    
  11.          <property name="conversionService" ref="conversionService"/>   
  12.           <property name="validator" ref="validator"/>     
  13.      </bean>    
  14.    
  15.      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">    
  16.           <property name="webBindingInitializer" ref="webBindingInitializer"/>    
  17.      </bean>   
[html]  view plain copy print ?
  1. 这是原来的:  
[html]  view plain copy print ?
  1.  <bean id="validator"  
  2.   class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
  3.   <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />  
  4.   <property name="validationMessageSource" ref="messageSource" />  
  5.  </bean>  
[html]  view plain copy print ?
  1. <mvc:annotation-driven validator="validator" />  
[html]  view plain copy print ?
  1.    

最搞笑的是当我把后加上的去掉,也就是恢复配置文件,验证就好使了。我也不知道是为什么。

验证好使了,但是又有新问题,就是有些字段的验证是非空的,但是还是不好用。后来如此解决:

后来发现在实体上面的注解,如果是String类型的要用NotEmpty,如果是integer和double用NotNull。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值