[英]Spring 4 MVC JSR303 @Valid error message to property
Trying to have all of my error message in a property. 试图将我所有的错误消息都放在一个属性中。
Following tutorial: https://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/ 遵循教程: https : //www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/
Problem: Error message is not coming from my property file. 问题:错误消息不是来自我的属性文件。 Not sure what I am doing wrong
不知道我在做什么错
File Structure: 档案结构:
POJO POJO
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
@Entity
@Table(name="books")
public class Book implements Serializable{
private static final long serialVersionUID = -2042607611480064259L;
@Id
@GeneratedValue
private int id;
@NotNull
@Size(min=7)
private String name;
@NotNull
@Size(min=2, max=13)
private String ispn;
@DecimalMin(value = "0.01")
private double price;
public Book(){}
// Setter & getters
}
exception_en_US.properties exception_zh_CN.properties
NotNull.book.name = Book name must not be blank, Please insert valid book name.
Size.book.name = Book name should have more than 7 characters.
NotNull.book.ispn = Must enter valid ISPN code.
Size.book.ispn = Standard ISPN code should have 10, 13 characters.
DecimalMin.book.price = Price of the book must be greater than 0. And can not be Negative number.
app-dispatcher-servlet.xml app-dispatcher-servlet.xml
<context:component-scan base-package="com.app.controller" />
<mvc:annotation-driven/>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
<!-- Binding properties to context -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com.app.properties.windows</value>
<value>com.app.properties.exceptions</value>
</list>
</property>
</bean>
What am I doing wrong? 我究竟做错了什么? Your extra eye is much appreciated.
非常感谢您的关注。
Thank you 谢谢
I think your messageSource configuration is wrong. 我认为您的messageSource配置错误。 Try something like this:
尝试这样的事情:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:window</value>
<value>classpath:exception</value>
</list>
</property>
</bean>
As it turns out exception_en_US.properties
shouldn't reference the class and field but form & path name, form name, which is given by commandName or modelAttribute. 事实证明,
exception_en_US.properties
不应引用类和字段,而应引用由commandName或modelAttribute给出的表单和路径名,表单名。
This is my HTML 这是我的HTML
<sf:form action="/newbook" modelAttribute="formBook" method="POST">
<table>
<tr>
<td>Book Name:</td>
<td>
<sf:input type='text' name='name' path="name"/><br/>
<sf:errors path="name"></sf:errors>
</td>
</tr>
<tr>
<td>ispn:</td>
<td>
<sf:input type='text' name='ispn' path="ispn"/><br/>
<sf:errors path="ispn"></sf:errors>
</td>
</tr>
<tr>
<td>Price:</td>
<td>
<sf:input type='text' name='price' path="price"/><br/>
<sf:errors path="price"></sf:errors>
</td>
</tr>
<tr><td colspan='2'><input name="submit" type="submit" value="submit"/></td></tr>
</table>
</sf:form>
Notice: modelAttribute="formBook"
注意:
modelAttribute="formBook"
So in my exception_en_US.properties
所以在我的
exception_en_US.properties
NotNull.formBook.name = Book name must not be blank, Please insert valid book name.
Size.formBook.name = Book name should have more than 7 characters.
NotNull.formBook.ispn = Must enter valid ISPN code.
Size.formBook.ispn = Standard ISPN code should have 10, 13 characters.
Hope this helps others... took me soo sooo long ... 希望这可以帮助其他人...让我这么久...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.