我知道这是一个多么重复的问题,但我遵循了这个领域的每一条建议,并且无法得出结论为什么它不起作用:
这是我的控制器代码
@RequestMapping("/submitForm")
String submitForm(
@Valid
@ModelAttribute("student")
StudentRepository student, BindingResult bindingResult)
{ ...
这是bean的定义:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="error-messges"/>
</bean>
My Java Object / Bean参数验证约束:
public class StudentRepository {
@Size(min = 1,max = 3)
private String firstName;
....
并且,这是应该包含错误消息的属性文件:
#typeMismatch.studentRepository.age= Invalid Number
#org.hibernate.validator.constraints.NotEmpty.firstName = First Name Shouldn't Be Empty
Size.student.firstName = Too long text
JSP视图文件:
First Name: <form:input type="text" path="firstName" /> <br/>
<form:errors path="firstName" cssClass="error"/>
此设置可能出现什么问题?
修改
验证工作正常,与验证注释内联的验证消息也正常工作,即:@Size(min=1,message"error")
我可以使用它的是我们使用
填充属性文件的错误消息编辑2:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.myCode.springdemo"/>
<!--
Step 4: Add support for conversion, formatting and validation support
-->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Loading propeties file to be injected in Beans and used in views-->
<util:properties id="countryOptions" location="classpath:error-messges.properties" />
<!-- Loading error messages from properties file-->
<!-- Register the messages properties-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="error-messges"/>
</bean>
</beans>