今天調了一整天,主要就是jsp禁用前端驗證後,提交表單,頁面報400錯誤:解決方案,@vaild註解後要緊跟BindingResult,不然就會包400錯誤。它倆是成對出現的,也就是有多少個實體需要驗證就要有多少對。
再就是頁面不報400錯誤了,但是後端驗證始終不好使,也就是bindingResult對象getErrors 始終是false,這個真的好鬱悶。
後來研究了一下springMvc的配置發現要這樣配置:
這是後添加的:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.bdp.common.convertor.DataConvertor">
</bean>
</list>
</property>
</bean>
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"/>
<property name="validator" ref="validator"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="webBindingInitializer"/>
</bean>
這是原來的:
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource" />
</bean>
<mvc:annotation-driven validator="validator" />
最搞笑的是當我把後加上的去掉,也就是恢復配置文件,驗證就好使了。我也不知道是爲什麼。
驗證好使了,但是又有新問題,就是有些字段的驗證是非空的,但是還是不好用。後來如此解決:
後來發現在實體上面的註解,如果是String類型的要用NotEmpty,如果是integer和double用NotNull。