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/
Problem: Error message is not coming from my property file. Not sure what I am doing wrong
File Structure:
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
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
<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. 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.
This is my 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"
So in my 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 ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.