尝试在属性中包含所有错误消息。
以下教程:https://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/
问题:错误消息不是来自我的属性文件。不确定我做错了什么
文件结构:
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-调度-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>
我做错了什么?非常感谢您的额外关注。
谢谢
答案 0 :(得分:1)
我认为你的messageSource配置错误。尝试这样的事情:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:window</value>
<value>classpath:exception</value>
</list>
</property>
</bean>
答案 1 :(得分:0)
事实证明exception_en_US.properties
不应该引用类和字段,而是形成&amp;路径名,表单名,由commandName或modelAttribute给出。
这是我的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>
注意:modelAttribute="formBook"
所以在我的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.
希望这有助于其他人......太过我了......